reversing a link list..
Page 1 of 1
reversing a link list..
#include<iostream>
#include<conio.h>
using namespace std;
struct treenode
{
int info;
struct treenode *next;
};
typedef struct treenode *NODEPTR;
//NODEPTR getnode(void);
void display(NODEPTR);
void reverse(NODEPTR);
int main()
{
NODEPTR l,q,l1;
l=new treenode;
l1=l;
//l->info=1;
//l->next=NULL;
cout<<"enter linklist"<<endl;
int x;
for(int i=1;i<10;i++)
{
cout<<"enter element";
cin>>x;
//q=getnode();
l->info=x;
l->next=new treenode;
l=l->next;
}
cout<<"enter element"<<endl;
cin>>x;
l->info=x;
l->next=NULL;
display(l1);
reverse(l1);
getch();
return 0;
}
/*NODEPTR getnode(void)
{
NODEPTR p;
p=new treenode;
//p=(NODEPTR) malloc(sizeof(struct treenode));
return p;
}*/
void reverse(NODEPTR l)
{
NODEPTR r,q,a;
r=new treenode;
r->next=NULL;
r->info=l->info;
q=r;
if(q->next==NULL)
{cout<<"q "<<q->info<<endl;}
while(l->next!=NULL)
{
// a=new treenode;
//a=r;
r=new treenode;
r->next=q ;
l=l->next;
r->info=l->info;
q=r;
}
cout<<"reversed list"<<endl;
display(q);
}
void display(NODEPTR l1)
{while(l1->next!=NULL)
{
cout<<l1->info;
l1=l1->next;
}cout<<l1->info;
cout<<endl;
}
#include<conio.h>
using namespace std;
struct treenode
{
int info;
struct treenode *next;
};
typedef struct treenode *NODEPTR;
//NODEPTR getnode(void);
void display(NODEPTR);
void reverse(NODEPTR);
int main()
{
NODEPTR l,q,l1;
l=new treenode;
l1=l;
//l->info=1;
//l->next=NULL;
cout<<"enter linklist"<<endl;
int x;
for(int i=1;i<10;i++)
{
cout<<"enter element";
cin>>x;
//q=getnode();
l->info=x;
l->next=new treenode;
l=l->next;
}
cout<<"enter element"<<endl;
cin>>x;
l->info=x;
l->next=NULL;
display(l1);
reverse(l1);
getch();
return 0;
}
/*NODEPTR getnode(void)
{
NODEPTR p;
p=new treenode;
//p=(NODEPTR) malloc(sizeof(struct treenode));
return p;
}*/
void reverse(NODEPTR l)
{
NODEPTR r,q,a;
r=new treenode;
r->next=NULL;
r->info=l->info;
q=r;
if(q->next==NULL)
{cout<<"q "<<q->info<<endl;}
while(l->next!=NULL)
{
// a=new treenode;
//a=r;
r=new treenode;
r->next=q ;
l=l->next;
r->info=l->info;
q=r;
}
cout<<"reversed list"<<endl;
display(q);
}
void display(NODEPTR l1)
{while(l1->next!=NULL)
{
cout<<l1->info;
l1=l1->next;
}cout<<l1->info;
cout<<endl;
}
yasharth02- Posts : 3
Join date : 2008-06-14
Re: reversing a link list..
yasharth02 wrote:#include<iostream>
#include<conio.h>
using namespace std;
struct treenode
{
int info;
struct treenode *next;
};
typedef struct treenode *NODEPTR;
//NODEPTR getnode(void);
void display(NODEPTR);
void reverse(NODEPTR);
int main()
{
NODEPTR l,q,l1;
l=new treenode;
l1=l;
//l->info=1;
//l->next=NULL;
cout<<"enter linklist"<<endl;
int x;
for(int i=1;i<10;i++)
{
cout<<"enter element";
cin>>x;
//q=getnode();
l->info=x;
l->next=new treenode;
l=l->next;
}
cout<<"enter element"<<endl;
cin>>x;
l->info=x;
l->next=NULL;
display(l1);
reverse(l1);
getch();
return 0;
}
/*NODEPTR getnode(void)
{
NODEPTR p;
p=new treenode;
//p=(NODEPTR) malloc(sizeof(struct treenode));
return p;
}*/
void reverse(NODEPTR l)
{
NODEPTR r,q,a;
r=new treenode;
r->next=NULL;
r->info=l->info;
q=r;
if(q->next==NULL)
{cout<<"q "<<q->info<<endl;}
while(l->next!=NULL)
{
// a=new treenode;
//a=r;
r=new treenode;
r->next=q ;
l=l->next;
r->info=l->info;
q=r;
}
cout<<"reversed list"<<endl;
display(q);
}
void display(NODEPTR l1)
{while(l1->next!=NULL)
{
cout<<l1->info;
l1=l1->next;
}cout<<l1->info;
cout<<endl;
}
yasharth02- Posts : 3
Join date : 2008-06-14
Re: reversing a link list..
yasharth02: here i have limited the no of items in d list to 10..if u want u can make a query driven prog to ask d user for as many inputs as he wants wrote:yasharth02 wrote:#include<iostream>
#include<conio.h>
using namespace std;
struct treenode
{
int info;
struct treenode *next;
};
typedef struct treenode *NODEPTR;
//NODEPTR getnode(void);
void display(NODEPTR);
void reverse(NODEPTR);
int main()
{
NODEPTR l,q,l1;
l=new treenode;
l1=l;
//l->info=1;
//l->next=NULL;
cout<<"enter linklist"<<endl;
int x;
for(int i=1;i<10;i++)
{
cout<<"enter element";
cin>>x;
//q=getnode();
l->info=x;
l->next=new treenode;
l=l->next;
}
cout<<"enter element"<<endl;
cin>>x;
l->info=x;
l->next=NULL;
display(l1);
reverse(l1);
getch();
return 0;
}
/*NODEPTR getnode(void)
{
NODEPTR p;
p=new treenode;
//p=(NODEPTR) malloc(sizeof(struct treenode));
return p;
}*/
void reverse(NODEPTR l)
{
NODEPTR r,q,a;
r=new treenode;
r->next=NULL;
r->info=l->info;
q=r;
if(q->next==NULL)
{cout<<"q "<<q->info<<endl;}
while(l->next!=NULL)
{
// a=new treenode;
//a=r;
r=new treenode;
r->next=q ;
l=l->next;
r->info=l->info;
q=r;
}
cout<<"reversed list"<<endl;
display(q);
}
void display(NODEPTR l1)
{while(l1->next!=NULL)
{
cout<<l1->info;
l1=l1->next;
}cout<<l1->info;
cout<<endl;
}
yasharth02- Posts : 3
Join date : 2008-06-14
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|