Jul 16, 2010 at 1:05pm UTC
Hi . i Want to Delete first Node From Linked List , but when I Delete Node From
First, Program terminated Automatically
#include<iostream>
using namespace std;
class Node
{
public:
Node();
void beginInsert(Node*&);
void endInsert(Node*&);
void ShowDisplay(Node*);
void DeleteNode(Node*&, int);
private:
int data;
Node* next;
};
int main()
{
Node* p = new Node;
Node* head = NULL;
int num;
p->beginInsert(head);
for ( int i = 0 ; i < 5 ; i++)
p->endInsert(head);
p->ShowDisplay(head);
cout << " Please Enter Number for Delete : ";
cin >> num;
p->DeleteNode(head,num);
p->ShowDisplay(head);
return 0;
}
Node::Node() : data(0),next(NULL)
{
}
void Node::beginInsert(Node*& head)
{
Node* newPtr = new Node;
head = NULL;
cout << " please Enter Your Data : " ;
cin >> newPtr->data;
newPtr->next = NULL;
head = newPtr;
}
void Node::endInsert(Node * & head)
{
Node* newPtr = new Node;
Node* cur = head;
Node* prev = NULL;
cout << " Pleae Enter Your Item For Insert To End Of List: ";
cin >> newPtr->data;
while( cur != NULL)
{
prev = cur;
cur = cur-> next;
}
if(cur == NULL)
{
newPtr->next = cur;
prev->next = newPtr;
}
}
void Node::ShowDisplay(Node* head)
{
for( Node* cur = head; cur!= NULL; cur = cur->next)
cout << " Your Data is : " << cur->data << endl;
}
void Node::DeleteNode(Node*& head, int num)
{
Node* cur;
Node* prev;
for(prev = NULL, cur = head; (cur != NULL); prev= cur,cur = cur->next)
{
if ( prev == NULL)
head = cur->next;
if(cur->data == num)
{
cout <<"\n Your Number Delete !\n ";
prev->next = cur->next ;
}
}
}
Jul 16, 2010 at 2:31pm UTC
Deleting the first node would be a little different than other deletion as here there would be no previous node,rather it wud be just a node pointer(head).Just modify the code as below:
void Node::DeleteNode(Node*& head, int num)
{
int flag = 0; //just for confirming whether number exists in the list
if(head->data = num)
{ head = head->next;flag = 1;}
else
{
Node* cur = head->next;
Node* prev = head;
while(cur !=NULL)
{
if(cur->data == num)
{ prev = cur->next;delete cur;flag = 1;break;}
else
{prev= cur,cur = cur->next}
}
}
if(flag = 0)
{ cout<<"Number not found";}
else
{cout<<"Number deleted!!";}
}
though have not tested it but it shud work.
Jul 16, 2010 at 2:54pm UTC
Hi .
i checked this code , its work just for first node . isn't work for the last node or middle node
Jul 16, 2010 at 3:05pm UTC
gimme sometime i ll try to run and fix it....might be some silly error
Jul 16, 2010 at 3:40pm UTC
Hi,
the code was fine just had some silly errors.checked it...works fine..
void Node::DeleteNode(Node*& head, int num)
{
int flag = 0; //just for confirming whether number exists in the list
if(head->data == num)
{ head = head->next;flag = 1;}
else
{
Node* cur = head->next;
Node* prev = head;
while(cur != NULL)
{
if(cur->data == num)
{ prev->next = cur->next;delete cur;flag = 1;break;}
else
{prev= cur;cur = cur->next;}
}
}
if(flag == 0)
{ cout<<"Number not found";}
else
{cout<<"Number deleted!!";}
}
Jul 16, 2010 at 3:52pm UTC
Hi. Thankssssssssssssssssssssssssssss