Social Icons

Saturday 4 May 2013

Reversing a linked list inserted from begining


#include<iostream>
#include<conio.h>
using namespace std;
struct link
{
 int data;
 struct link *next;
 struct link *pre;
};
struct link *newn,*loc, *start,*ptr,*end;
void binsert();
void einsert();
void minsert();
void sinsert();
void reverse();
void display();
int main()
{char ch;
int op;
for(int i=0;i<5;i++)
{

 binsert();
 display();
}
cout<<"\n reverse of the linked list : ";
   reverse();
 
getch();
}


void display()
{
ptr=start;
while(ptr!=NULL)
{
cout<<" "<<ptr->data;
cout<<"-->";
ptr=ptr->next;
}
}



void binsert()
{
int item;
cout<<"\n Enter item to insert in begining :";
cin>>item;
newn = new struct link[1];
newn->data=item;
newn->next=NULL;
newn->pre=NULL;
//tr=start;
loc=end;
if(end==NULL)
{
start=newn;
end=newn;
}
else
{
while(loc->pre!=NULL)
{
loc=loc->pre;
}
 // newn->next=start;
loc->pre=newn;
  newn->next=loc;
  start=newn;




}

}

 void reverse()
 {
  loc=start;
  while(loc!=NULL)
  {
  cout<<" "<<loc->data;
cout<<"-->";
loc=loc->next;
 
  }
 }











No comments:

Post a Comment

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management