Reputation: 551
I cannot figure it out why am getting a 0 when trying to delete the first element of the list? I have inserted 3 elements (7,8,9),when am deleting 8 or 9 its working fine for me ,but when am trying to delete 7(.i.e the first element) irrespective of the no of elements present in the list am only getting a val of 0,screenshot attached in the end.
This is how the code works
Here is the code
# include <iostream>
using namespace std;
class node{
private:
int info;
class node *ptr;
public:
class node * insertNode(class node * head);
void traverseList(class node * head);
class node * deleteNode(class node * head);
};
class node * node::insertNode(class node * head){
node * newNode=new node();
node * curNode=new node();
int position,srchPos=0;
if(newNode==NULL)
cout<<"Node Creation Failed"<<endl;
else{
cout<<"Enter data to be stored"<<endl;
cin>>newNode->info;
newNode->ptr=NULL;
cout<<"To Add in First Position Press 1 else 2\n";
cin>>position;
if(position==1){
newNode->ptr=head;
head=newNode;
}
else{
curNode=head;
cout<<"Enter the data after which you want to insert\n";
cin>>srchPos;
while((curNode->info!=srchPos)&&(curNode->ptr!=NULL)){
curNode=curNode->ptr;
}
if(curNode==NULL){
cout<<"Data not Found"<<endl;
}
else{
newNode->ptr=curNode->ptr;
curNode->ptr=newNode;
}
}
}
return head;
}
void node::traverseList(class node * head){
node * curNode=new node();
curNode=head;
cout<<"Data Present in the List ::\n";
while(curNode!=NULL){
cout<<curNode->info<<"\t";
curNode=curNode->ptr;
}
}
class node * node::deleteNode(class node * head){
node * curNode=new node();
node * prevNode=new node();
int data;
if(head==NULL)
cout<<"List Empty\n";
cout<<"Enter Data to be deleted\n";
cin>>data;
curNode=head;
while(curNode!=NULL){
if(curNode->info==data){
if(curNode==head){ //This part where my code fails
head=head->ptr;
cout<<"val of head at:"<<head->info;
delete (curNode);
return head;
}
else{
prevNode->ptr=curNode->ptr;
delete(curNode);
return head;
}
}
else{
prevNode=curNode;
curNode=curNode->ptr;
}
}
cout<<"Node not found\n" ;
return head;
}
int main()
{
node *head =NULL;
node *n1=new node();
int choice;
while(1){
cout<<"\n1:: Insert Node\t2:: Traversal\t3:: Delete\n";
cout<<"Enter your Choice\n";
cin>>choice;
switch(choice){
case 1: head=n1->insertNode(head);
break;
case 2: n1->traverseList(head);
break;
case 3 :n1->deleteNode(head);
break;
default: cout<<"Invalid Choice\n";
break;
}
}
return 0;
}
Can somebody help me out what i am missing
This is the snapshot of my execution
Upvotes: 0
Views: 59
Reputation: 5593
There's not just the head problem here, but a memory leak as well in the deleteNode method. In the beginning of deleteNode you assign curNode and prevNode to a new node:
node * curNode=new node();
node * prevNode=new node();
Then you reassign both later on in the method without deleting this allocations you created.
Then for head not changing as expected, you can modify the address that head points to, but not change the address head points using a single pointer. You should reassign head in your main function to the value that is returned by deleteNode, or pass head to deleteNode as a double pointer so you can change the object it points to.
Upvotes: 1
Reputation: 3223
The problem is that you are shifting the head
to head->ptr
but you the return value (address) is not captured by you while calling the delete function.
case 3 :n1->deleteNode(head);
change it to:
case 3 : head = n1->deleteNode(head);
Upvotes: 3