Wednesday, 20 August 2014

Filled Under:

C program to Reverse a linked list

Share

C program to Reverse a linked list


Linked list is a data structure, which consist of nodes. Nodes contain information i.e data field and linked field. You can perform various c program to implement insertion deletion and counting of nodes in linked list. Reverse linked list c code need some technique.For implementing this c program to reverse linked list we need to take 3 node pointer.

Example of C program to Reverse a linked list

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
 int no;
 struct node *next;
};
struct node *head=NULL,*temp,*LLt;
void addrecord();
void main()
{
 int ch;
 clrscr();
 while (1)
 {
  printf("n add element (number) TO LISTn 0 TO EXIT / END");
  scanf("%d", &ch)
  if (ch==0)
  {
   exit(0);
  }
  else
  {
   AddElement(ch,*LLt );
  } 
  printf("the reverse of linked list is %d " reverse(*LLt));
} 
 
void AddElement(ch,*LLt )
{
 struct node *temp;
 temp=(struct node*)malloc(sizeof(struct node));
 temp->no=ch; 
 if (LLt==NULL)
 {
  LLt=temp;
  temp->next=NULL; 
 }
 else
 { 
  LLt->next=temp;
  temp->next=NULL; 
 }
}
node *reverse(node *first)
{
 node *temp = NULL; 
 if(first->next != NULL)
 {
  temp = reverse(first->next);
  temp->next = first;
  return first;
 }
 else
  return first;
}