Linked List crashes all the time

So I am tryng to figure out Linked Lists.
Explanation of code:
gen_arr function generated random array
generatePP generated linked list with elements from array
del deletes head after executing main function before return
search is looking for elemenbt 0 that doesnt exists in linked list so the function goes through every element in list


If you guys can help me out and how to implement search function. Thank you guys :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include <iostream>
  #include<stdlib.h>
  #include<time.h>
  using namespace std;

  //ARRAY GENERATOR
  int *gen_arr(int V[], int n){
	srand(41);
	for(int i=0; i<n; i++){
		V[i]=1+rand() % n;
	}
	return V;
  }

  struct node{
	int data;
	node* next;
  };

  void generatePP(struct node **head, int num, int V[]);
  void del(struct node **head);
  int main(){
	int n;
	struct node *head = NULL;
	int* V=NULL;   
	V = new int[n]; 
	
	cout<<"Unesite broj elemenata niza/popisa ";
	cin>>n;	
	gen_arr(V, n);
	for(int i=0;i<n;i++){
		cout<<V[i]<< " ";
	}
	
	generatePP(&head,n,V);
	del(&head);
	return 0;
  }

  void generatePP(struct node **head, int num, int V[]){
	int i;
	struct node *temp;
	for(i=0; i<num; i++){
		temp = new node;
        temp->data = V[i];
        if(*head == NULL){
        	*head = temp;
        	temp->next = NULL;
		}
		else{
			temp->next = *head;
			*head = temp;
		}
		cout<<temp->data<<" ";
	}
  }

  void search(struct node *head){
	int seek = 0;
	while(head !=NULL){
		if(head->data == seek){
			cout<<"Pronadeno"<<endl;
			return;
		}
		head= head->next;
	}
	cout<<"Element ne postoji"<<endl;
  }

  void del(struct node **head){
	struct node *temp;
	while (*head != NULL){
		temp = *head;
		*head = (*head)->next;
		free(temp);
	}
  }
Using new and free is never a good idea.

Also have a look here: https://www.youtube.com/watch?v=YQs6IC-vgmo
good catch, adding to it...
new and delete go together.
malloc & free go together. Mixing them is unstable and crashing is not unusual.

malloc and free are C, and are only used in old c++ rarely to tap realloc.
Also, use nullptr instead of NULL.

nullptr was invented to get over problems associated with using NULL with pointers.
Line 26: n is uninitialized. You should move this line to right after line 29.

You don't need to if inside generatePP. In all cases, you can just execute
1
2
temp->next = *head;
*head = temp;


search() should take value you're searching for as a parameter:
void search(struct node *head, int value)


Some suggestions:
- instead of passing the address of head to generatePP and del, pass a reference to it.
- Use for loops to walk the list. This will help separate the "walking the list" code from the "doing something with each item" code. For example:
1
2
3
4
5
6
7
8
9
10
11
12
void
search(struct node *head)
{
    int seek = 0;
    for (; head; head = head->next) {
        if (head->data == seek) {
            cout << "Pronadeno" << endl;
            return;
        }
    }
    cout << "Element ne postoji" << endl;
}

Topic archived. No new replies allowed.