Breaking News
recent

WAP to delete a node from the beginning of the linked list

#include<iostream.h>
#include<conio.h>
class node
{                     
public:
char data;
node *next;
};

void main()
{
                node *start, *p;
                char ch;
                int j=0;
                clrscr();
                start='\0';
do
                {
                if(start=='\0')
                                {
                                start=new node;
                                p=start;
                                }
                else
                                {
                                p->next=new node;
                                p=p->next;
                                }

cout<<"\nEnter data   ";
cin>>p->data;
j++;
cout<<"\nEnter choice (y/n)?";
cin>>ch;
}while(ch=='Y'||ch=='y');
p->next='\0';

cout<<"\nData for nodes ";
p=start;
while(p!='\0')
                {
                cout<<endl<<p->data;
                p=p->next;
                }
cout<<endl<<"No. of nodes  "<<j;

if(start==0)
                {
                cout<<"There is no node in the list ";
                }
else
                {
                p=start;
                start=p->next;
                delete p;
                cout<<"\nData deleted from beginning   ";
                }


cout<<"\nData for nodes after deletion  ";
p=start;
while(p!='\0')
{
                cout<<endl<<p->data;
                p=p->next;
}
getch();

}

Output:



Unknown

Unknown

No comments:

Post a Comment

Powered by Blogger.