Write a 'C' Program to create singly linked list,reverse it and display both of the list.


#include

#include

#include

struct node

{

int data;

struct node *link;

}*head;

void createlist();

void display();

void reverse();

void main()

{

clrscr();

head=NULL;

printf("\n Create List \n");

createlist();

printf("\n Element in the list are : ");

display();

printf("\n\n");

printf("\n Revered List is: ");

reverse();

getch();

}


void createlist()

{

struct node *newnode,*prev;

int ele,n,i;

head=NULL;

printf("\n How Many Nodes you want to enter? : ");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

newnode=(struct node*)malloc(sizeof(struct node));

if(newnode==NULL)

{

printf("\n List is Empty");

return;

}

printf("\n Enter Element %d : ",i);

scanf("%d",&ele);

newnode->data=ele;

newnode->link=NULL;

if(head==NULL)

{

head=newnode;

prev=newnode;

}

else

{

prev->link=newnode;

prev=newnode;

}

}

}

void display()

{

struct node *temp;

temp=head;

if(head==NULL)

{

printf("\n List is Empty.");

return;

}

while(temp!=NULL)

{

printf("\t%d",temp->data);

temp=temp->link;

}

}

void reverse()

{

struct node *p,*q,*temp1;

int temp;

q=head;

while(q!=NULL)

{

p=q->link;

while(p!=NULL)

{

if(q->data!=p->data)

{

temp=q->data;

q->data=p->data;

p->data=temp;

}

p=p->link;

}

q=q->link;

}

temp1=head;

while(temp1!=NULL)

{

printf("\t%d",temp1->data);

temp1=temp1->link;

}

}


/*OUTPUT


 Create List


 How Many Nodes you want ot enter? : 5


 Enter Element 1 : 2


 Enter Element 2 : 1


 Enter Element 3 : 3


 Enter Element 4 : 4


 Enter Element 5 : 5


 Element in the list are :      2       1       3       4       5



 Revered List is:       5       4       3       1       2



 Create List


 How Many Nodes you want ot enter? : 5


 Enter Element 1 : 2


 Enter Element 2 : 1


 Enter Element 3 : 3


 Enter Element 4 : 4


 Enter Element 5 : 5


 Element in the list are :      2       1       3       4       5



 Revered List is:       5       4       3       1       2 */  
Write a 'C' Program to create singly linked list,reverse it and display both of the list. Write a 'C' Program to create singly linked list,reverse it and display both of the list. Reviewed by on November 17, 2013 Rating: 5
Powered by Blogger.