C program to reverse a linked list
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
//Creating structure
struct node
{
int data;
struct node *link;
}*first=NULL;
struct node * create(struct node *);
void disp(struct node *);
struct node * rev(struct node *);
//main function
int main()
{
printf("\nEnter elements to create linked list\n");
first=create(first);
printf("\nLinked list created!\n");
disp(first);
first=rev(first);
printf("\nReversed Linked List\n");
disp(first);
return 0;
}
//function to create linked list
struct node * create(struct node *first)
{
struct node *new_node;
int choice=0;
do
{
new_node=(struct node *)malloc(sizeof(struct node));
printf("\nEnter an element\n");
scanf("%d",&new_node->data);
new_node->link=NULL;
if(first==NULL)
first=new_node;
else
{
new_node->link=first;
first=new_node;
}
printf("\nEnter 1 to continue, 0 to stop\n");
scanf("%d",&choice);
}while(choice==1);
return first;
}
//function to display linked list
void disp(struct node *first)
{
struct node *temp;
if(first==NULL)
printf("\nDisplay not possible, Linked list empty\n");
else if(first->link==NULL)
{
printf("Displaying linked list elements:\n");
printf("%d",first->data);
}
else
{
printf("Displaying linked list elements:\n");
temp=first;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
}
}
//function to reverse linked list
struct node * rev(struct node * first)
{
struct node *x,*y,*z;
x=first;
y=first->link;
z=first->link->link;
while(z!=NULL)
{
y->link=x;
x=y;
y=z;
z=z->link;
}
y->link=x;
first->link=NULL;
first=y;
return first;
}
#include <stdlib.h>
//Creating structure
struct node
{
int data;
struct node *link;
}*first=NULL;
struct node * create(struct node *);
void disp(struct node *);
struct node * rev(struct node *);
//main function
int main()
{
printf("\nEnter elements to create linked list\n");
first=create(first);
printf("\nLinked list created!\n");
disp(first);
first=rev(first);
printf("\nReversed Linked List\n");
disp(first);
return 0;
}
//function to create linked list
struct node * create(struct node *first)
{
struct node *new_node;
int choice=0;
do
{
new_node=(struct node *)malloc(sizeof(struct node));
printf("\nEnter an element\n");
scanf("%d",&new_node->data);
new_node->link=NULL;
if(first==NULL)
first=new_node;
else
{
new_node->link=first;
first=new_node;
}
printf("\nEnter 1 to continue, 0 to stop\n");
scanf("%d",&choice);
}while(choice==1);
return first;
}
//function to display linked list
void disp(struct node *first)
{
struct node *temp;
if(first==NULL)
printf("\nDisplay not possible, Linked list empty\n");
else if(first->link==NULL)
{
printf("Displaying linked list elements:\n");
printf("%d",first->data);
}
else
{
printf("Displaying linked list elements:\n");
temp=first;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
}
}
//function to reverse linked list
struct node * rev(struct node * first)
{
struct node *x,*y,*z;
x=first;
y=first->link;
z=first->link->link;
while(z!=NULL)
{
y->link=x;
x=y;
y=z;
z=z->link;
}
y->link=x;
first->link=NULL;
first=y;
return first;
}
OUTPUT:
Enter elements to create linked listEnter an element
10
Enter 1 to continue, 0 to stop
1
Enter an element
20
Enter 1 to continue, 0 to stop
1
Enter an element
30
Enter 1 to continue, 0 to stop
1
Enter an element
40
Enter 1 to continue, 0 to stop
1
Enter an element
50
Enter 1 to continue, 0 to stop
0
Linked list created!
Displaying linked list elements:
50 40 30 20 10
Reversed Linked List
Displaying linked list elements:
10 20 30 40 50