My Project - Video Waker Alarm
Get Video alarm on Google Play

Reverse linked list c code


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 4 node pointer.

Reverse linked list c code

#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h>

struct node
{
   int data;
   struct node *next;
}*head;

void insert_data(int value)
{
    struct node *var,*temp; 
    temp=head;
    var=(struct node *)malloc(sizeof(struct node));
    var->data=value;

    if(head==NULL)
    {
        head=var;
        head->next=NULL;
    }
    else
    {
         while(temp->next!=NULL)
         {
              temp=temp->next;
         }
         var->next=NULL;
         temp->next=var;
    }
}

void reverse_list()
{
    struct node *temp,*temp1,*var;
    temp=head;
    var=NULL;
    while(temp!=NULL)
    {
         temp1=var;
         var=temp;
         temp=temp->next;
         var->next=temp1;
    }
    head=var;
}

void display()
{
    struct node *var;
    var=head;
    printf("\nlist of elments are \n");
    while(var!=NULL)
    {
         printf("-> %d ",var->data);
         var=var->next;
    }
}

int main()
{
    int i,value;
    char ch='y';
    head=NULL;

    printf(" 1.)  Insert node");
    printf("\n 2.)  display the list");
    printf("\n 3.)  reverse the nodes");
    printf("\n 4.)  exit");
    while(ch=='y')
    {
         printf("\nChoose to do operation :");
         scanf("%d",&i);
         switch(i)
         {
             case 1 :
             {
             printf("\nEnter the data to be inserted in node ");
             scanf("%d",&value);
             insert_data(value);
             display();
             break;
             }
             case 2 :
             {
             display();
             break;
             }
             case 3 :
             {
             reverse_list();
             display();
             break;
             }
             case 4 :
             {
             exit(0);
             break;
             }
        }
   }
getch();
}

Output of Reverse linked list c code:

Reverse linked list c code

4 thoughts on “Reverse linked list c code”

Comments are closed.