Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
struct node *cn()
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
return(n);
}
void createnode(int n) //Function to create Linked list.
{
int i,x;
struct node * temp,*newnode;
printf("Enter elements of linked list :\n");
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(head==NULL) //checking in linked list is empty
{
head=cn();
head->data=x;
head->next=NULL;
temp=head;
}
else
{
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
}
}
}
void display()
{
struct node *temp;
temp=head;
while (temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
void reverselist()
{
struct node *prev,*cur;
prev=head;
head=head->next;
cur=head;
prev->next=NULL;
while (head!=NULL)
{
head=head->next;
cur->next=prev;
prev=cur;
cur=head;
}
head=prev;
}
void main()
{
int n;
printf("Enter size of Linked list\n");
scanf("%d",&n);
createnode(n);
printf("Displaying list:\n");
display();
printf("\nReverse list is :\n");
reverselist();
display();
}
0 Comments