void linked_list::create(node *newnode)
{
int x;char place;
if(head==NULL && tail==NULL)
{
cout<<"Enter a number\n";
cin>>x;
newnode->value = x;
newnode->next = NULL;
head = newnode;
tail = newnode;
cout<<"Node Inserted";
}
else
{
cout<<"Enter a number\n";
cin>>x;
cout<<"Enter where do you want to insert... H for Head and T for Tail\n";
cin>>place;
if(place == 'H')
{
newnode->value = x;
newnode->next = head;
newnode = head;
cout<<"Node Inserted";
}
else
{
if(place == 'T')
{
newnode->value = x;
newnode->next = NULL;
tail->next = newnode;
newnode = tail;
cout<<"Node Inserted";
}
else
{
cout<<"please enter a valid place to insert... H for Head and T for Tail\n";
create(newnode);
}
}
}
display(head);
}
void linked_list::deletenode(node *newnode)
{
int x;
node *prev;
head = NULL;
tail = NULL;
cout<<"enter the value you want to delete\n";
cin>>x;
prev = head;
newnode = head->next;
if(head->value == x)
{
head = head->next;
display(head);
}
else
{
if(newnode->value==x)
{
prev->next = newnode->next;
display(head);
}
else
{
newnode = newnode->next;
prev = prev->next;
deletenode(newnode);
}
}
}
1. You can use the Edit button
2. That indentation... :/
3.
ne555 wrote:
backtrace
4. At which point does it crash?
5. l.create(newnode);
newnode is undefined. you should use new node(); instead, or avoid using pointers while you're learning program basics.
3. Pl help me with backtrace... can i get more info on it !
4.It crashes when i type in a value for insertion/deletion. I think the function call works fine cause the first cout commands are being executed !
5.isnt node *newnode; defining the newnode ?
It's declaring it.
But not defining its content.
Its content is not a regular pointer, it's a random value.
You use new node(); to request a pointer to a node.
But you must delete (newnode); to free it.
$ g++ main.cpp -ggdb
$ gdb a.out
> run
(Seg fault)
> backtrace
#0 0x0000000000400a00 in linked_list::create (this=0x7fffffffe2c0, newnode=0x4008c0 <_start>) at main.cpp:33
#1 0x0000000000400d4d in main () at main.cpp:132
there you see the exact line where it crashes, /*33*/ newnode->value = x; probably because you never initialized its value