}
Skip to main content
REVERSAL IN SINGLE LINK LIST:
REVERSAL IN SINGLE LINK LIST:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *link;
};
void reversal(struct node *head);
void main()
{
struct node *head=NULL,*ptr=NULL,*temp=NULL;
int i,n;
clrscr();
printf("enter the number of nodes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=(struct node *)malloc(sizeof(struct node));
printf("enter the data in node part\n");
scanf("%d",&temp->data);
temp->link=NULL;
if(head==NULL)
{ head=temp;
}
else
ptr=head;
while(ptr->link!=NULL)
{ ptr=ptr->link;
}
ptr->link=temp;
}
ptr=head;
printf("print link list\n");
while(ptr!=NULL)
{ printf("\n%d",ptr->data);
ptr=ptr->link;
}
reversal(head);
getch();
}
void reversal(struct node *head)
{
struct node *prevnode=NULL,*currentnode=NULL,*nextnode=NULL,*point;
currentnode=head;
printf("\nreverse link list\n");
while(currentnode!=NULL)
{
nextnode=currentnode->link;
currentnode->link=prevnode;
prevnode=currentnode;
currentnode=nextnode;
}
head=prevnode;
point=head;
while(point!=NULL)
{ printf("\n%d",point->data);
point=point->link;
}
}