Breaking News
recent

WAP to create and traverse a linked list

#include<iostream.h>
#include<conio.h>
class node
{ public:
  int data;
  node *next;
  };
void main()
{
  node *start,*p;
  char ch='y';

int j=0;clrscr();
  start='\0';
  do
  {
  if (start=='\0')
  {
                 start= new node;
                 p=start;
                 }
  else
                 {
                 p->next = new node;
                 p=p->next;
                 }
  cout<<"\n Enter data ";
                cin>>p->data;j++;
               
cout<<"\n Enter your choice (y/n)? ";
                cin>>ch;
                }
                while(ch=='y' || ch=='Y');
                p->next='\0';cout<<"\n--------------------------------";
                cout<<"\n Data for nodes ";
                p=start;
                while(p!=0)
                {
                cout<<"\n"<<p->data;
                p=p->next;
                }
                cout<<"\n Number of nodes = "<<j;
                getch();

}

Output:


Unknown

Unknown

No comments:

Post a Comment

Powered by Blogger.