Skip to main content

Featured

functions in Python

C++ program to reverse the linked list


//This program reverses the linked list.
#include<header.h>
struct node
{
char info[10];
node *next;
};
void main ()
{
clrscr();
node *start,*save,*newptr;
start=NULL;
char ans;
do{
   node *newptr=new node;
   if(start==NULL)
   {
   cout<<"Enter Info for the new node\n";
   cin>>newptr->info;
   start=newptr;
   newptr->next=NULL;
   save=start;
   }
   else
   {cout<<"Enter Info for the new node\n";
   cin>>newptr->info;
   save->next=newptr;
   newptr->next=NULL;
   save=newptr;
   }
   cout<<"Wanna Insert new node ?(y/n)\n";
   cin>>ans;
  }while(ans=='y'||ans=='Y');
node *ptr;
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
cout<<ptr->info<<endl;
}
node *nptr,*save2;
save=ptr=start;
int flag=0;
for(nptr=save->next;nptr!=NULL;nptr=save2)
{ if(flag==0)
  {save->next=NULL;
   save2=nptr->next;
   nptr->next=save;
   save=nptr;
   flag=1;
  }
 else
 {save2=nptr->next;
   nptr->next=save;
   save=nptr;
 }
}
start=save;
cout<<"\nNow Showing the reversed linked list"<<endl;
delay(500);
 for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
cout<<ptr->info<<endl;
}
getch();
}

Comments

Popular Posts