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 Linked List.

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

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

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

//Start or Head pointer of Circular Linked List
node *start = NULL;

void insert_at_begin (void);
void insert_at_end (void);
void delete_from_begin (void);
void delete_from_end (void);
void traverse (void);

int main (void)
{
               int choice;

               //Menu
               while (1)
               {
                              printf ("\n\t***Menu***\n");
                              printf ("1. Insert at beginning of Circular Linked List\n");
                              printf ("2. Insert at end of Circular Linked List\n");
                              printf ("3. Delete from beginning of Circular Linked List\n");
                              printf ("4. Delete from end of Circular Linked List\n");
                              printf ("5. Traverse Circular Linked List\n");
                              printf ("6. 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 ( );
                                                            break;
                                             case 6:
                                                            return 0;
                                             default:
                                                            printf ("\nError! Wrong Choice.\n");
                              }
               }
};

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

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

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

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

               //Finding the last node.
               last = start;
               while (last -> next != start)
               {
                              last = last -> next;
               }

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

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

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

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

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

               //Finding the last node
               last = start;
               while (last -> next != start)
               {
                              last = last -> next;
               }

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

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

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

               //Finding the last node
               last = start;
               while (last -> next != start)
               {
                              last = last -> next;
               }

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

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

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

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

               temp = start;
               //Finding the last node of the Linked List.
               while (temp -> next -> next != start)
               {
                              temp = temp -> next;
               }
               last = temp -> next;
               temp -> next = start;
               free (last);
};

//Function to traverse the Linked List.
void traverse (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");
};

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

Choice: 1

Input element: 3

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

Choice: 1

Input element: 2

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

Choice: 1

Input element: 1

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

Choice: 5
Linked List:  1  2  3

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

Choice: 2

Input element: 4

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

Choice: 2

Input element: 5

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

Choice: 5
Linked List:  1  2  3  4  5

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

Choice: 3

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

Choice: 3

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

Choice: 5
Linked List:  3  4  5

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

Choice: 4

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

Choice: 4

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

Choice: 5
Linked List:  3

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

Choice: 6

No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib