compgeek
Would you like to react to this message? Create an account in a few clicks or log in to continue.

reversing a link list..

Go down

reversing a link list.. Empty reversing a link list..

Post  yasharth02 Sat Jun 14, 2008 6:22 am

#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

Back to top Go down

reversing a link list.. Empty Re: reversing a link list..

Post  yasharth02 Sat Jun 14, 2008 6:28 am

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

Back to top Go down

reversing a link list.. Empty Re: reversing a link list..

Post  yasharth02 Sat Jun 14, 2008 6:30 am

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

Back to top Go down

reversing a link list.. Empty Re: reversing a link list..

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum