Oct 11, 2011

C Program to implement Doubly Linked List Operations

Data structures using C, Linked list is a data structure in which the objects are arranged in a linear order. In Doubly Linked List, each node contains three fields, one is to store data and other two fields stores the reference(address) of the previous and next node. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/

#include <stdlib.h>
typedef struct dnode {
 int data;
 struct dnode *next, *prev;
} DNODE;

DNODE *InsFront(DNODE *, int);
DNODE *InsBefore(DNODE *, int, int);
DNODE *DelNode(DNODE *, int);
void Display(DNODE *);

main() {
 DNODE *start = NULL; /* Main Program */
 int opn, elem, info, n, i;
 do {
  clrscr();
  printf("\n ### Doubly Linked List Operations ### \n\n");
  printf("\n Press 1-Creation with front Insertion");
  printf("\n       2-Insert Before a Given Node");
  printf("\n       3-Delete a Given Node");
  printf("\n       4-Display");
  printf("\n       5-Exit\n");
  printf("\n       Your option ? ");
  scanf("%d", &opn);
  switch (opn) {
  case 1:
   printf("\n\nHow Many Nodes ?");
   scanf("%d", &n);
   for (i = 1; i <= n; i++) {
    printf("\nRead the Data for Node %d ?", i);
    scanf("%d", &elem);
    start = InsFront(start, elem);
   }
   printf("\nDoubly Linked list with %d nodes is ready toUse!!\n", n);
   break;
  case 3:
   printf(" Read the Info of the Node to be deleted ? ");
   scanf("%d", &info);
   start = DelNode(start, info);
   break;
  case 2:
   printf(" Read the Data for New node\n");
   scanf("%d", &elem);
   printf(
     " Read the Info of the Node(to the left of which new node tobe inserted ? ");
   scanf("%d", &info);
   start = InsBefore(start, elem, info);
   break;
  case 4:
   printf(" Doubly Linked List is \n");
   Display(start);
   break;
  case 5:
   printf("\n\n Terminating \n\n");
   break;
  default:
   printf("\n\nInvalid Option !!! Try Again !! \n\n");
   break;
  }
  printf("\n\n\n\n  Press a Key to Continue . . . ");
  getch();
 } while (opn != 5);
}

DNODE *InsFront(DNODE *start, int elem) {
 DNODE *temp;
 temp = (DNODE *) malloc(sizeof(DNODE));
 if (temp == NULL) {
  printf(" Out of Memory !! Overflow !!!");
  return (start);
 } else {
  temp->data = elem;
  temp->prev = NULL;
  temp->next = start;
  start->prev = temp;
  printf(" New Node has been inserted at Front Successfully !!");
  return (temp);
 }
}

DNODE *InsBefore(DNODE *start, int elem, int info) {
 DNODE *temp, *t;
 temp = (DNODE *) malloc(sizeof(DNODE));
 if (temp == NULL) {
  printf(" Out of Memory !! Overflow !!!");
  return (start);
 } else {
  temp->data = elem;
  temp->next = NULL;
  temp->prev = NULL;

  if (start->data == info) /* Front Insertion */
  {
   temp->next = start;
   start->prev = temp;
   return (temp);
  } else {
   t = start;
   while (t != NULL && t->data != info)
    t = t->next;
   if (t->data == info) /* Node found */
   {
    temp->next = t;
    temp->prev = t->prev;
    t->prev->next = temp;
    t->prev = temp;
   } else
    printf(" Node not found,Invalid Info !!!");
   return (start);
  }
 }
}

DNODE *DelNode(DNODE *start, int info) {
 DNODE *t;
 if (start == NULL) {
  printf(" Underflow!!!");
  return (start);
 } else {
  t = start;
  if (start->data == info) /* Front Deletion */
  {
   start = start->next;
   start->prev = NULL;
   t->next = NULL;
   free(t);
   return (start);
  } else {
   while (t != NULL && t->data != info)
    t = t->next;
   if (t->data == info) /* node to be deleted  found*/
   {
    t->prev->next = t->next;
    t->next->prev = t->prev;
    t->next = t->prev = NULL;
    free(t);
   } else
    printf("Node not found, Invalid Info !!");
   return (start);
  }
 }
}

void Display(DNODE *start) {
 DNODE *t;
 if (start == NULL)
  printf("Empty List\n");
 else {
  t = start;
  printf("Forward Traversal \n\n Start->");
  while (t) {
   printf("[%d]->", t->data);
   t = t->next;
  }
  printf("Null\n");
 }
}

Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!