C program to display elements of linked list in reverse order.

Posted by Mangesh on May 08, 2018

/* program to display element of linked list in reverse order */

Description :

Singly Link list
A singly link list's node is divided into twofield. The first field holds information about the node, and second field holds the reference of next node. When we link all node is some order then it becomes Singly Link list. In singly link list we have only one reference of next node, so in singly link list we can travels in only one way. The reference (link) points to the next node in the list, or to a null value( if its last node). For Singly Link list we need one Head reference to point to the First node of the list.

Program :

#include<stdio.h>
#include<conio.h>
typedef struct node
{
  int data;
  struct node *next;
} node;
void display(node *list)
{
  printf("\nElements in linked list : ");
  while(list != NULL)
  {
    printf("  %d",list->data);
    list = list->next;
  }
}
void displayReverse(node *list)
{
  if(list->next != NULL)
    displayReverse(list->next);
  printf(" %d",list->data);
}
void main()
{
  node *head, *tail;
  char character ='y';
  head = (node *) malloc (sizeof(node));
  clrscr();
  tail = head;
  while(character == 'y'){
  printf("\nEnter the element value : ");
  scanf("%d",&tail->data);
  printf("Do you want to add more element (y/n) : ");
  scanf("%s",&character);
  if(character=='y')
  {
    tail->next =  (node *) malloc(sizeof(node));
    tail = tail->next;
  }
  else
    tail->next=NULL;
  }
  display(head);
  printf("\nDisplay linked list in reverse order :");
  displayReverse(head);
getch();
}

Output :

c program linked list Executed and tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program to create singly linked list.
2 C program to display elements of linked list in reverse order.
3 C program to count number of node in linked list.
4 C program to print a linked list in reverals order.
5 C program to search an element in linked list.
6 C for circular queue using linked list.
Latest Post
1 shubhanshu
2 test
3 C program for Breadth First Search.
4 C program for Depth First Search(DFS).
5 C for circular queue using linked list.