Monday, August 27, 2007

PROGRAMME TO REVERSE A DOUBLY LINKED LINK LIST

/* PROGRAMME TO REVERSE A DOUBLY LINKED LINK LIST */


#include<stdio.h>
struct llist
{
int val;
struct llist *next,*prev;
};
typedef struct llist list;

main()
{
list *create(void);
void reverse(list *temp);
list *root;
clrscr();
printf("PLEASE ENTER THE LIST,ENTER -99 TO END:");
root=create();
reverse(root);
getch();
}

list *create(void)
{
list *head,*temp,*temp1;
int data;
head=(list*)malloc(sizeof(list));
temp=head;
head->prev=NULL;
do
{
scanf("%d",&data);
if(data==-99)
return(head);
temp1=(list*)malloc(sizeof(list));
temp1->val=data;
temp->next=temp1;
temp1->prev=temp;
temp1->next=NULL;
temp=temp1;
}while(1);
}

void reverse(list *head)
{
list *t1,*t2,*t3;
t1=head->next;
t2=t1->next;
t3=head;
do
{
t1->next=t3;
t1->prev=NULL;
head->next=t2;
t3->prev=t1;
t3=t1;
t1=t2;
t2=t2->next;
}while(t1!=NULL);
printf("\nTHE REVERSED LIST IS:");
t1=t3;
while(t1!=head)
{
printf(" %d",t1->val);
t1=t1->next;
}
return;
}

No comments: