linked list pass by address error
Jul 24, 2014 at 8:41am UTC
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 79 80 81 82 83 84 85
#include <iostream>
using namespace std;
struct node
{
int data;
node*next;
};
void add(node**List, int number);
void display(node*List);
int main()
{
cout<<"\n1. Add\n2.Search\n3.Delete\n4.Display\nWhat do you want to do? \n" ;
int choice;
cin>>choice;
node*List = NULL;
switch (choice)
{
case 1:
cout<<"Enter number to add! :" ;
int number;
cin>>number;
add(&List,number);
main();
break ;
case 2:
cout<<"Enter number to search! :" ;
cin>>number;
//search(List, number);
main();
break ;
case 3:
cout<<"Enter number to delete! :" ;
cin>>number;
//delete(&List, number);
main();
break ;
case 4:
display(List);
main();
break ;
}
}
void add(node**List, int number)
{
if ((*List) == NULL)
{
(*List) -> data = number;
(*List) -> next = NULL;
cout<<(*List)->data;
}
else
{
node*List2 = *List;
while (List2->next != NULL)
{
List2 = List2->next;
}
List2->next = new node;
List2->next->data = number;
List2->next->next = NULL;
}
}
void display(node*List)
{
cout<<"The contents of the linked list are: \n" ;
while (List != NULL)
{
cout<<List->data<<endl;
List=List->next;
}
}
How is this not working? just the add & display function.
Last edited on Jul 24, 2014 at 8:41am UTC
Jul 24, 2014 at 8:50am UTC
Is there any specific reason why you are complicating your code by using pointer to pointer instead of a simple pointer to node?
Also, one thing that I note is that you are not allocating memory for the first node.
1 2 3 4 5 6 7 8
if ((*List) == NULL)
{
// *List = new node;
(*List) -> data = number;
(*List) -> next = NULL;
cout<<(*List)->data;
}
Don't forget to
delete
your 'newed' nodes at the end of your program.
Jul 24, 2014 at 8:57am UTC
1 2 3 4 5 6 7 8
if ((*List) == NULL)
{
// *List = new node;
(*List) -> data = number;
(*List) -> next = NULL;
cout<<(*List)->data;
}
Thanks for this.
But still, the values added to the linked list is not displayed with the display function (or even in the main function if tried).
Last edited on Jul 24, 2014 at 8:57am UTC
Jul 24, 2014 at 9:03am UTC
Don't forget to delete your 'newed' nodes at the end of your program.
How do i do this?
Jul 24, 2014 at 9:10am UTC
You shouldn't call many main function, it's no point.
It'll be like you're starting new program with old GLOBAL VARIABLES but new ALL MAIN'S LOCAL VARIABLES.(And it make your List = NULL every times)
If you want to run as loop, use loop.
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 79 80 81 82 83 84 85 86 87 88 89 90
#include <iostream>
using namespace std;
struct node
{
int data;
node*next;
};
void add(node**List, int number);
void display(node*List);
int main()
{
node*List = NULL;
while (true ){
cout<<"\n1. Add\n2.Search\n3.Delete\n4.Display\nWhat do you want to do? \n" ;
int choice;
cin>>choice;
switch (choice)
{
case 1:
cout<<"Enter number to add! :" ;
int number;
cin>>number;
add(&List,number);
//main();
break ;
case 2:
cout<<"Enter number to search! :" ;
cin>>number;
//search(List, number);
//main();
break ;
case 3:
cout<<"Enter number to delete! :" ;
cin>>number;
//delete(&List, number);
//main();
break ;
case 4:
display(List);
//main();
break ;
default :
break ;
}
}
}
void add(node**List, int number)
{
if ((*List) == NULL)
{
(*List) = new node;
(*List) -> data = number;
(*List) -> next = NULL;
cout<<(*List)->data;
}
else
{
node*List2 = *List;
while (List2->next != NULL)
{
List2 = List2->next;
}
List2->next = new node;
List2->next->data = number;
List2->next->next = NULL;
}
}
void display(node*List)
{
cout<<"The contents of the linked list are: \n" ;
while (List != NULL)
{
cout<<List->data<<endl;
List=List->next;
}
}
Last edited on Jul 24, 2014 at 9:14am UTC
Jul 24, 2014 at 9:15am UTC
To delete all nodes you created.
Loop by your List and use
delete
from tail.
1 2 3
node *nn;
nn = new node;
delete nn;
Do it for preventing memory leak.
Last edited on Jul 24, 2014 at 9:16am UTC
Topic archived. No new replies allowed.