Saturday 13 December 2014

wap to invert t linked list

wap to invert t linked list


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node
{
struct node *next;
int data;
}node;
void main()
{
int n,i;
node *head,*p,*q,*r;
clrscr();
printf("\nenter no of elements");
scanf("%d",&n);
head=(node*)malloc(sizeof(node));
scanf("%d",&head->data);
head->next=NULL;

p=head;
for(i=1;i<n;i++)
{
p->next=(node*)malloc(sizeof(node));
p=p->next;
scanf("%d",&p->data);
p->next=NULL;
}
p=head;
while(p!=NULL)
{
printf("\ndata=<%d>address=%u,next data address=%u",p->data,&p->data,&p->next->data);
p=p->next;
}
p=NULL;
q=head;
r=q->next;
while(q!=NULL)
{
q->next=p;
p=q;
q=r;
if(r!=NULL)
r=r->next;
}
printf("\ninversion is:");
while(p!=NULL)
{
printf("\ndata=<%d>address=%u,next data address=%u",p->data,&p->data,&p->next->data);
p=p->next;
}
getch();
}

No comments:

Post a Comment