Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]


Write a program to implement Circular Doubly Linked List.

/*
 File: DSPrgm04.c
 Author: Aditya Saini
 Date: Oct 17, 2019
 Description: Program to implement the Circular Doubly Linked List.
*/

#include <stdio.h>
#include <alloc.h>

//Structure definition
typedef struct node
{
               int data;
               struct node *next;
               struct node *previous;
} node;

//Start and End pointer of Circular Doubly Linked List
node *start = NULL;
node *end = NULL;

void insert_at_begin (void);
void insert_at_end (void);
void delete_from_begin (void);
void delete_from_end (void);
void traverse_from_start (void);
void traverse_from_end (void);

int main (void)
{
               int choice;

               //Menu
               while (1)
               {
                              printf ("\n\t***Menu***\n");
                              printf ("1. Insert at beginning of Circular Doubly Linked List\n");
                              printf ("2. Insert at end of Circular Doubly Linked List\n");
                              printf ("3. Delete from beginning of Circular Doubly Linked List\n");
                              printf ("4. Delete from end of Circular Doubly Linked List\n");
                              printf ("5. Traverse Circular Doubly Linked List from start\n");
                              printf ("6. Traverse Circular Doubly Linked List from end\n");
                              printf ("7. Exit\n");
                              printf ("\nChoice: ");
                              scanf ("%d", &choice);

                              switch (choice)
                              {
                                             case 1:
                                                            insert_at_begin ( );
                                                            break;
                                             case 2:
                                                            insert_at_end ( );
                                                            break;
                                             case 3:
                                                            delete_from_begin ( );
                                                            break;
                                             case 4:
                                                            delete_from_end ( );
                                                            break;
                                             case 5:
                                                            traverse_from_start ( );
                                                            break;
                                             case 6:
                                                            traverse_from_end ( );
                                                            break;
                                             case 7:
                                                            return 0;
                                             default:
                                                            printf ("\nError! Wrong Choice.\n");
                              }
               }
};

//Function to insert the node at the beginning of the Circular Doubly Linked List.
void insert_at_begin (void)
{
               node *new;

               //Creating node
               new = (node *) malloc (sizeof (node));
               if (new == NULL)
               {
                              printf ("\nError! Memory Full. ");
                              printf ("Please delete some elements to insert new one.\n");
                              return;
               }

               //Set data value
               printf ("\nInput element: ");
               scanf ("%d", &new -> data);
               new -> previous = NULL;
               new -> next = NULL;

               //Placing first node of Circular Doubly Linked List.

               if (start == NULL)
               {
                              start = new;
                              end = new;
                              start -> previous = start;
                              start -> next = start;
                              return;
               }

               //Placing node at the beginning of the Circular Doubly Linked List.
               new -> next = start;
               start -> previous = new;
               start = new;
               start -> previous = end;
               end -> next = start;
};

//Function to insert the node at the end of the Circular Doubly Linked List.
void insert_at_end (void)
{
               node *new;

               //Creating node
               new = (node *) malloc (sizeof (node));
               if (new == NULL)
               {
                              printf ("\nError! Memory Full. ");
                              printf ("Please delete some elements to insert new one.\n");
                              return;
               }

               //Set data value
               printf ("\nInput element: ");
               scanf ("%d", &new -> data);
               new -> previous = NULL;
               new -> next = NULL;

               //Placing first node of Circular Doubly Linked List.
               if (start == NULL)
               {
                              start = new;
                              end = new;
                              start -> next = start;
                              start -> previous = start;
                              return;
               }

               //Placing node at the end of the Circular Doubly Linked List.
               end -> next = new;
               new -> previous = end;
               end = new;
               end -> next = start;
               start -> previous = end;
};

//Function to delete the node from the beginning of the Circular Doubly Linked List.
void delete_from_begin (void)
{
               node *temp;

               if (start == NULL)
               {
                              printf ("\nError! Empty Linked List.\n");
                              return;
               }

               //If there is only one node in Circular Doubly Linked List.
               if (start -> next == start)
               {
                              free (start);
                              start = NULL;
                              end = NULL;
                              return;
               }

               //Delete node from the beginning of the Circular Doubly Linked List.
               temp = start;
               start = start -> next;
               start -> previous = end;
               end -> next = start;
               free (temp);
};

//Function to delete the node from the end of the Circular Doubly Linked List.
void delete_from_end (void)
{
               node *temp;

               if (start == NULL)
               {
                              printf ("\nError! Empty Linked List.\n");
                              return;
               }

               //If there is only one node in Circular Doubly Linked List.
               if (end -> previous == end)
               {
                              free (start);
                              start = NULL;
                              end = NULL;
                              return;
               }

               //Delete node from the end of the Circular Doubly Linked List.
               temp = end;
               end = temp -> previous;
               end -> next = start;
               start -> previous = end;
               free (temp);
};

//Function to traverse the Circular Doubly Linked List from start.
void traverse_from_start (void)
{
               node *temp;

               temp = start;
               if (temp == NULL)
               {
                              printf ("\nError! Empty Linked List.\n");
                              return;
               }

               printf ("Linked List:  ");
               while (temp -> next != start)
               {
                              printf ("%d  ", temp -> data);
                              temp = temp -> next;
               }
               printf ("%d", temp -> data);
               printf ("\n");
};

//Function to traverse the Circular Doubly Linked List from end.
void traverse_from_end (void)
{
               node *temp;

               temp = end;
               if (temp == NULL)
               {
                              printf ("\nError! Empty Linked List.\n");
                              return;
               }

               printf ("Reverse Linked List:  ");
               while (temp -> previous != end)
               {
                              printf ("%d  ", temp -> data);
                              temp = temp -> previous;
               }
               printf ("%d", temp -> data);
               printf ("\n");
};

Output
               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 1

Input element: 5

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 1

Input element: 4

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 1

Input element: 3

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 1

Input element: 2

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 1

Input element: 1

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 2

Input element: 6

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 2

Input element: 7

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 2

Input element: 8

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 2

Input element: 9

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 2

Input element: 10

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 5
Linked List:  1  2  3  4  5  6  7  8  9  10

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 6
Reverse Linked List:  10  9  8  7  6  5  4  3  2  1

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 3

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 3

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 4

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice:  4

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 5
Linked List:  3  4  5  6  7  8

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 6
Linked List:  8  7  6  5  4  3

               ***Menu***
1. Insert at beginning of Circular Doubly Linked List
2. Insert at end of Circular Doubly Linked List
3. Delete from beginning of Circular Doubly Linked List
4. Delete from end of Circular Doubly Linked List
5. Traverse Circular Doubly Linked List from start
6. Traverse Circular Doubly Linked List from end
7. Exit

Choice: 7


No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib