Sunday, December 18, 2011

Traversal in a list


////TRAVERSAL IN A LIST////
  
 #include
 #include
 #include
 #include
 struct node {       
   int info;
   node*next;
                                               
                                               }*start,*newptr,*save,*ptr,*rear;
  
 node *newnode(int) ;         
                       //to create nmew node
 void insert(node*) ;
 void travers(node*);
 void main()
 {
 clrscr();
 start=rear=NULL;
 int inf;
 char ch='y';
 while(ch=='y')
 {
 cout<<"ENTER INFo IN NEW NODE:";
 cin>>inf;
 newptr=newnode(inf);
 insert(newptr);
 cout<<"PRESS Y TO ENTER NEW NODE:";
 cin>>ch;
 }
 do{cout<<"THE LIST NOW IS:";
 traverse(start);
 getch();
 }
 while(ch=='y');
 }
 node*newnode(int n)
 {
 ptr=new node;
 ptr->info=n;
 ptr->next=NULL;
 return ptr;
 }
 void insert(node*np)
 {
 if (start==NULL)start=rear=np;
 else {
  
 rear->next=np;
 rear=np;
 }
 }
 void traverse(node*np)
 {
 while (np!=NULL)
 {
 cout<info<<"->";
 np=np->next;
 }
 cout<<"!!!\n";
 }
  
  
 OUTPUT
  
 ENTER INFO IN NEW NODE:2
 PRESS Y TO ENTER NEW NODE:y
 ENTER INFO IN NEW NODE:34
 PRESS Y TO ENTER NEW NODE:y
 ENTER INFO IN NEW NODE:54
 PRESS Y TO ENTER NEW NODE:n
 THE LIST NOW IS:1->2->34->54->!!!