Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Problems:  Write a C program to implement a circular queue that contains a list of names.  

Data Structure Definition:

Node: A node is a basic building block of a linked list. A node may contain different segments, but basically it contains two parts which are as followed,

 

                                    Data or information part : It stores the data in the node

                                       Link part : It points to the next node or holds the address to the next node.

Source Code:

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define MAX 10

  5. //defining the basic element of the queue
  6. typedef struct Q
  7. {
  8. int R,F;
  9. char *data[MAX];
  10. }Q;

  11. //declaration of functions
  12. void initialise(Q *P);
  13. int empty(Q *P);
  14. int full(Q *P);
  15. void enqueue(Q *P,char *x);
  16. char * dequeue(Q *P);
  17. void print(Q *P);

  18. //main function
  19. int main()
  20. {
  21. //intializing a node named queue
  22. Q q;
  23. //declaration and initialization fof variables
  24.     int op;
  25.     char x[40],*y;
  26.     
  27.     //initializ the queue
  28.     initialise(&q);
  29.         
  30. do{
  31. //displaying the menu
  32. printf("\n\n 1) Enqueue\n 2) Dequeue\n 3) Print\n 4) Quit");
  33. //inputting the input choice 
  34. printf("\n Enter Your Choice : ");
  35. scanf("%d",&op);
  36. //calling functions and passing arguments through the functions as per th choice of the user
  37. switch(op)
  38. case 1: 
  39. printf("\n Enter string : ");
  40.     fflush(stdin);
  41.     scanf("%s",x);
  42.     if(!full(&q))
  43. {
  44. y=(char*)malloc(strlen(x)+1);
  45. strcpy(y,x);
  46. enqueue(&q,y);
  47. }
  48.     else
  49. printf("\n Queue is full !!!!");
  50.         break;
  51.     case 2:
  52.     if(!empty(&q))
  53.     {
  54. y=dequeue(&q);
  55. printf("\n Deleted Data = %s",y);
  56.     }
  57.     else
  58. printf("\n Queue is empty !!!!");
  59.     break;
  60.     case 3: 
  61.     print(&q);a
  62. break;
  63. }
  64.       }while(op!=4);
  65. }

  66. //function to initialize a circular queue
  67. void initialise(Q *P)
  68. {
  69. //declaration of variable
  70. int i;
  71. //initializing the queue
  72. for(i=0;i<MAX;i++)
  73. P->data[i]=NULL;
  74. P->R=-1;
  75. P->F=-1;
  76. }

  77. //function to check if the queue is empty
  78. int empty(Q *P)
  79. {
  80. //if the queue is empty, return 1 else, return 0
  81. if(P->R==-1)
  82. return(1);
  83. return(0);
  84. }

  85. //function to check if the queue is full
  86. int full(Q *P)
  87. {
  88. //return 1 if the queue is full, else return 0
  89. if((P->R+1)%MAX==P->F)
  90. return(1);
  91. return(0);
  92. }

  93. //function to enqueue and element in the queue
  94. void enqueue(Q *P,char *x)
  95. {
  96. //enqueueing the element
  97. if(P->R==-1)
  98. {
  99. P->R=P->F=0;
  100. if(P->data[P->R]!=NULL)
  101. free(P->data[P->R]);
  102. P->data[P->R]=x;
  103. }
  104. else
  105. {
  106. P->R=(P->R+1)%MAX;
  107. if(P->data[P->R]!=NULL)
  108. free(P->data[P->R]);
  109. P->data[P->R]=x;
  110. }
  111. }

  112. //function to dequeue 
  113. char * dequeue(Q *P)
  114. {
  115. //declaring a character type pointer
  116. char *x;
  117. //dequeing the element from the queue
  118. x=P->data[P->F];
  119. if(P->R==P->F)
  120. {
  121. P->R=-1;
  122. P->F=-1;
  123. }
  124. else
  125. P->F=(P->F+1)%MAX;
  126. return(x);
  127. }

  128. //function to display the elements of the queue
  129. void print(Q *P)
  130. {
  131. int i;
  132. if(!empty(P))
  133. {
  134. printf("\n");
  135. for(i=P->F;i!=P->R;i=(i+1)%MAX)
  136. printf("%s\t",P->data[i]);
  137. }
  138.        printf("%s\t",P->data[i]);
  139. }

Output:

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 1

 

 Enter string : computer

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 1

 

 Enter string : science

 

1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 1

 

Enter string : honours

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 3

 

computer        science   honours

 

1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 2

 

 Deleted Data = computer

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 2

 

 Deleted Data = science

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

 Enter Your Choice : 2

 

 Deleted Data = honours

 

 1) Enqueue

 2) Dequeue

 3) Print

 4) Quit

Enter Your Choice : 2

 

 Queue is empty !!!!


This is a C program which implements queue using linklist.


Source Code:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4. //A structure is created for the node in queue

  5. struct queu
  6. {
  7. int info;
  8. struct queu *next;//Next node address
  9. };
  10. typedef struct queu *NODE;
  11. //This function will push an element into the queue

  12. NODE push(NODE rear)
  13. {
  14. NODE NewNode;
  15. //New node is created to push the data
  16. NewNode=(NODE)malloc(sizeof(struct queu));
  17. printf ("\nEnter the no to be pushed = ");

  18. scanf ("%d",&NewNode->info);
  19. NewNode->next=NULL;
  20. //setting the rear pointer
  21. if (rear != NULL)
  22. rear->next=NewNode;
  23. rear=NewNode;
  24. return(rear);
  25. }
  26. //This function will pop the element from the queue

  27. NODE pop(NODE f,NODE r)
  28. {
  29. //The Queue is empty when the front pointer is NULL

  30. if(f==NULL)
  31. printf ("\nThe Queue is empty");
  32. else
  33. {
  34. printf ("\nThe poped element is = %d",f->info);

  35. if(f != r)
  36. f=f->next;
  37. else
  38. f=NULL;
  39. }
  40. return(f);
  41. }
  42. //Function to display the element of the queue

  43. void traverse(NODE fr,NODE re)
  44. {
  45. //The queue is empty when the front pointer is NULL

  46. if (fr==NULL)
  47. printf ("\nThe Queue is empty");
  48. else
  49. {
  50. printf ("\nThe element(s) is/are = ");

  51. while(fr != re)
  52. {
  53. printf("%d ",fr->info);
  54. fr=fr->next;
  55. };
  56. printf ("%d",fr->info);
  57. }
  58. }
  59. int main()
  60. {
  61. int choice;
  62. char option;
  63. //declaring the front and rear pointer
  64. NODE front, rear;
  65. //Initializing the front and rear pointer to NULL

  66. front = rear = NULL;
  67. do
  68. {
  69. //clrscr();
  70. printf ("1. Push\n");
  71. printf ("2. Pop\n");
  72. printf ("3. Traverse\n");
  73. printf ("\n\nEnter your choice = ");

  74. scanf ("%d",&choice);
  75. switch(choice)
  76. {
  77. case 1:
  78. //calling the push function
  79. rear = push(rear);
  80. if (front==NULL)
  81. {
  82. front=rear;
  83. }
  84. break;
  85. case 2:
  86. //calling the pop function by passing
  87. //front and rear pointers
  88. front = pop(front,rear);
  89. if (front == NULL)
  90. rear = NULL;
  91. break;
  92. case 3:
  93. traverse(front,rear);
  94. break;
  95. }
  96. printf ("\n\nPress (Y/y) to continue = ");

  97. fflush(stdin);
  98. scanf ("%c",&option);
  99. }while(option == 'Y' || option == 'y');
  100. }

Output:

1. Push

2. Pop

3. Traverse

 

Enter your choice = 1

 

Enter the no to be pushed = 14

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 1

 

Enter the no to be pushed = 25

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 1

 

Enter the no to be pushed = 36

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 2

 

The poped element is = 14

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 3

 

The element(s) is/are = 25 36

 

Press (Y/y) to continue = N

 

Data structure definition:-

 The data structure Node represents a single node in the linked list]

Structure Node

        (   data to hold the relevant data value in a Node,

            next that holds the address of the next Node      

       )      




Source Code:   

  1. #include<conio.h>
  2. #include<stdio.h>
  3. #define max 5
  4. int queue[max],front=0,rear=0;
  5. int menu();
  6. void enqueue();
  7. void dequeue();
  8. void display();
  9. void main()
  10. {
  11. int ch;
  12. clrscr();
  13. printf("\nQueues using Arrays\n");
  14. do
  15. {
  16.     ch=menu();
  17.     switch(ch)
  18.     {
  19.     case 1: enqueue();
  20.     break;
  21.     case 2: dequeue();
  22.     break;
  23.     case 3: display();
  24.     break;
  25.     case 4: exit();
  26.     break;
  27.     default:printf("\n Please enter a valid choice!!");
  28.     }
  29. }while(1);
  30. }
  31.  
  32. int menu()
  33.     {
  34.     int ch;
  35.     printf("\n1.ENQUEUE \n2.DEQUEUE \n3.DISPLAY \n4.EXIT");
  36.     printf("\nEnter your Choice:");
  37.     scanf("%d",&ch);
  38.     return ch;
  39.     }
  40.  
  41. void enqueue()
  42. {
  43.     int element;
  44.     if(rear==max)
  45.     {
  46.         printf("\nOverflow!!");
  47.     }
  48.     else
  49.     {
  50.         printf("\nEnter Element:");
  51.         scanf("%d",&element);
  52.         queue[rear++]=element;
  53.         printf("\n %d Enqueued at %d",element,rear);
  54.     }
  55.  
  56. }
  57.  
  58. void dequeue()
  59. {
  60.     if(rear==front)
  61.     {
  62.         printf("\nUnderflow!!");
  63.     }
  64.     else
  65.     {
  66.         front++;
  67.         printf("\nElement is Dequeued from %d",front);
  68.    }
  69. }
  70. void display()
  71. {
  72.     int i;
  73.     if(front==rear)
  74.     {
  75.         printf("\nQueue is Empty!!!");
  76.     }
  77.     else
  78.     {
  79.         printf(" \n");
  80.         for(i=front;i<max;i++)
  81.         {
  82.             printf(" | %d ",queue[i]);
  83.         }
  84.             printf("|");
  85.     }
  86. }

Output:

Queues using Arrays
 
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:12
 
 12 Enqueued at 1
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:23
 
 23 Enqueued at 2
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:
23 Enqueued at 2
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:34
 
 34 Enqueued at 3
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:45
 
 45 Enqueued at 4
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:56
 
 56 Enqueued at 5
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:2
 
Element is Dequeued from 1
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:2
 
Element is Dequeued from 2
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:3
 
 | 34  | 45  | 56 |
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice: 4
 
 
 

Write a Program in C to sort a linked list containing numbers using any sorting technique of your choice.

DATA STRUCTURE DEFINITION

_______________________________________________

NODE: Data {To store data  in  the Node}

                                                      Next {To store link of  the next Node}

                ______________________________________________________________


Sorting of singly linked list Algorithm: https://browncodeit.blogspot.com/2020/06/an-algorithm-for-singly-linked-list-sorting.html

Coding in C

Source Code:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4.  
  5. //Definition of the data structure
  6. struct node
  7. {
  8.   //parts of node
  9.  int data;
  10.  struct node *next;
  11. };
  12.  
  13. //Rename of the data structure
  14. typedef struct node node;
  15.  
  16. //Function prototype declarations
  17. node *createlist (node *head);
  18. node *sort(node *head);
  19. void display(node *head);
  20. int main()
  21. {
  22.    //Declaration of variable
  23.   int c;
  24.     node *head;
  25.     //Allocation memory dynamically for the variable
  26.     head = (node *)malloc(sizeof(node));
  27.     do
  28.      {
  29.         //Displaying and calling the functions
  30.         printf("\nCreation of a Linked List");
  31.         head = createlist(head);
  32.         printf("\nContents of Linked List :\n");
  33.         display(head); 
  34.         printf("\nSorting of Linked List :\n");
  35.        head = sort(head);
  36.         printf("\n Do you want to continue ? press 1 : ");
  37.      scanf("%d",&c);
  38.       }while(c==1);
  39.         getch();
  40. }
  41.  
  42. //Function to create a linked list
  43. node *createlist (node *head)
  44. {
  45.    //Declaration of variables
  46. int n, i;
  47.    node *list;
  48.    //Input total number
  49.    printf("\nEnter total number of elements in the list : ");
  50.    scanf("%d",&n);
  51.    //Initioalizing the list by head pointer
  52.    list = head;
  53.    for( i = 0; i<n; i++)
  54.      {
  55. printf("Enter data :");
  56.         scanf("%d", &list->data);
  57.         if( i == n-1)
  58.               list->next = NULL;
  59.           else 
  60.             {                                 
  61.               list->next=(node *)malloc(sizeof(node));
  62.               list = list->next;               
  63.              }
  64.        }
  65.        return head;
  66. }
  67.  
  68. //Function to display a linked list
  69. void display (node *head)
  70. {
  71.    //Declaration of variables
  72.     node *list;
  73.    //Initializing the list by pointer head
  74.    list= head;
  75.    while(list->next != NULL)
  76.     {
  77.                printf("%d ->",list->data);
  78.                list = list -> next;
  79.     }
  80.      printf("%d\n",list->data);
  81. }

  82. //Funtion to sort a linked list
  83. node *sort(node *head)
  84. {
  85.    //Declaration of Variables
  86.    int temp;
  87.    node *x,*y;
  88.    for(x=head;x!=NULL;x=x->next)
  89.     {
  90.       for(y=x->next;y!=NULL;y=y->next)
  91.         {
  92.            if(x->data>y->data)
  93.              {
  94.               temp = x->data;
  95.               x->data = y->data;
  96.               y->data = temp;
  97.                }
  98.         }
  99.     }

  100.  //Print the linked list in sorted order
  101.  printf("\nContents of Linked List in sorted order : \n");
  102.  display(head);
  103.  return head;
  104. }


Output

 Outputs:-

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :15

Enter data :12

Enter data :18

 

Contents of Linked List :

15 -> 12 -> 18

 

Sorting of Linked List :

 

Contents of Linked List in sorted order :

12 -> 15 -> 18

 

 Do you want to continue ? press 1 : 1

 

Creation of a Linked List

Enter total number of elements in the list : 4

Enter data :66

Enter data :55

Enter data :

44

Enter data :77

 

Contents of Linked List :

66 -> 55 -> 44 -> 77

 

Sorting of Linked List :

 

Contents of Linked List in sorted order :

44 -> 55 -> 66 -> 77


Write a C program to implement the operations on a singly linked list. The program should include the following operations.

i. Creation of linked list

ii. Traversal of linked list

iii.Insertion of element---

                ( a). At the beginning of list

    ( b).At the end of list

         ( c).Before a given node

       ( d). After a given node

       ( e). At a given position

                                                       iv. Deletion of a node---

  (a). First node

(b)Last node

                                 (c). Node with a particular value

                          (d). Node at a given position

     CODING in C:-

  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. //Definition of the data structure
  5. struct node
  6. {
  7.                //parts of node
  8.                int data;
  9.                struct node *next;
  10. };
  11.  
  12. //Rename of the data structure
  13. typedef struct node node;
  14.  
  15. //Function prototype declarations
  16. node *createlist (node *head);
  17. void traverse (node *head);
  18. node *insert (node *head);
  19. node *insertbegin(node *head, int d);
  20. node *insertend(node *head, int d);
  21. node *insertbefore(node *head, int d);
  22. node *insertafter(node *head, int d);
  23. node *insertatparticularposition(node *head, int d);
  24. node *delet(node *head);
  25. node *deletbegin(node *head);
  26. node *deletend(node *head);
  27. node *deletvalue(node *head);
  28. node *deletatparticularposition(node *head);
  29.  
  30. int main()
  31. {
  32.                //Declaration of  variable
  33.                char cont;
  34.     node *head;
  35.     //Allocation memory dynamically for the variable
  36.     head = (node *)malloc(sizeof(node));
  37.    
  38.    
  39.     do
  40.                {
  41.                    //Displaying and calling the functions
  42.                    printf("\nCreation of a Linked List");
  43.         head = createlist(head);
  44.                    printf("\nContents of Linked List : \n");
  45.                    traverse(head);            
  46.                    printf("\nInsertion of a new node : \n");
  47.                    head = insert(head);
  48.                    printf("\nDeletion of Node : \n");
  49.                    head = delet(head);
  50.                    printf("\n Do you want to continue ? (Y or N) : ");
  51.                    scanf("%s",&cont);
  52.                }while(cont == 'Y' || cont =='y');
  53. }
  54.  
  55. //Function to create a linked list
  56. node *createlist (node *head)
  57. {
  58.                //Declaration of variables
  59.                int n, i;
  60.                node *list;
  61.               
  62.                //Inputt total number
  63.                printf("\nEnter total number of elements in the list : ");
  64.                scanf("%d", &n);
  65.               
  66.                //Initioalizing the list by head pointer
  67.                list = head;
  68.  
  69.                for ( i = 0; i<n; i++)
  70.                {
  71.                               printf("Enter data :");
  72.                               scanf("%d", &list->data);
  73.                               if ( i == n-1)
  74.                                 list->next = NULL;
  75.                               else
  76.                               {                                
  77.                                 list->next=(node *)malloc(sizeof(node));
  78.                                 list = list->next;                             
  79.                    }
  80.                }
  81.                return head;
  82. }
  83.  
  84. //Function to traverse a linked list
  85. void traverse (node *head)
  86. {
  87.                //Declaration of variables
  88.                node *list;
  89.               
  90.                //Initializing the list by pointer head
  91.                list = head;
  92.  
  93.                while (list->next != NULL)
  94.     {
  95.                printf("%d -> ",list->data);
  96.                list = list -> next;
  97.                }
  98.                printf("%d\n", list->data);
  99. }
  100.  
  101. //Function to insert nodes in a linked list
  102. node *insert (node *head)
  103. {
  104.                //declaring variables
  105.                int element, option;
  106.               
  107.                //Inputt the data to insert
  108.                printf("Enter data to insert :");
  109.                scanf("%d", &element);
  110.               
  111.                //Displaying insertion options
  112.                printf("Insertion options:");
  113.                printf("\n1. At the beginning");
  114.                printf("\n2. At the end");
  115.                printf("\n3. Before a key node");
  116.                printf("\n4. After a key node");
  117.                printf("\n5. At a particular position");
  118.                printf("\nEnter option : ");
  119.               
  120.                //Inputt the insertion option
  121.                scanf("%d", &option);
  122.               
  123.                switch(option)
  124.                {
  125.                               //Calling of functions according to the oprtions
  126.                               case 1:
  127.                                              head = insertbegin(head, element);
  128.                                              break;
  129.                               case 2:
  130.                                              head = insertend(head, element);
  131.                                              break;
  132.                               case 3:
  133.                                              head = insertbefore(head, element);
  134.                                              break;
  135.                               case 4:
  136.                                              head = insertafter(head, element);
  137.                                              break;
  138.                               case 5:
  139.                                              head = insertatparticularposition(head, element);
  140.                                              break;
  141.                               //Printing the default case
  142.                               default:
  143.                                               printf("\nInvalid choice. Insertion not done.");                                                                   
  144.                }
  145.               
  146.                printf("\nContents of Linked List after Insertion: \n");
  147.                traverse(head); 
  148.                return head;
  149. }
  150.  
  151. //Function to insert node at the begining
  152. node *insertbegin(node *head, int d)
  153. {
  154.                //Declaration of variable
  155.                node * tempnode;
  156.               
  157.                //Allocating memory dynamically for the new node
  158.                tempnode = (node *)malloc(sizeof(node));
  159.               
  160.                //Insert the new node at the begining
  161.                tempnode->data = d;
  162.                tempnode->next = head;
  163.               
  164.                head = tempnode;           
  165.                return head;
  166. }
  167.  
  168. //Function to insert node at the end
  169. node *insertend(node *head, int d)
  170. {
  171.                //Declaration of variables
  172.                node *tempnode, *list;
  173.               
  174.                //Allocating memory dynamically for the new node
  175.                tempnode = (node *)malloc(sizeof(node));
  176.               
  177.                //Inputt the data into the new node
  178.                tempnode->data = d;
  179.                tempnode->next = NULL;
  180.  
  181.                list = head;
  182.  
  183.                while (list -> next != NULL)
  184.                   list = list -> next;
  185.               
  186.         list -> next = tempnode;
  187.                return head;  
  188. }
  189.  
  190. //Function to insert node before a particular node
  191. node *insertbefore(node *head, int d)
  192. {
  193.                //Declaration and initialization of variables
  194.                int key;
  195.                int found = 0;
  196.                node *tempnode, *list;
  197.               
  198.                //Allocating memory dynamically for the new node
  199.                tempnode = (node *)malloc(sizeof(node));
  200.               
  201.                //Inputt the data into the new node
  202.                tempnode->data = d;
  203.               
  204.                printf("Enter value of key :")          ;
  205.                scanf("%d",&key);
  206.               
  207.                if ( head -> data == key)
  208.                {
  209.                               //Insertion of the new node at the beginging
  210.                               tempnode -> next = head;
  211.                               head = tempnode;
  212.                               return head;
  213.                }
  214.               
  215.                list = head;
  216.                while (found == 0 && list -> next != NULL)
  217.                {
  218.                               if (list -> next -> data == key)
  219.                               {
  220.                                              tempnode -> next = list -> next;
  221.                                              list -> next = tempnode;
  222.                                              found = 1;
  223.                               }
  224.                               else
  225.                                  list = list -> next;
  226.                }
  227.               
  228.                if ( found == 0)
  229.                   printf ("Key Node not found!");
  230.               
  231.                return head;
  232. }
  233.  
  234. //Function to insert node after a particular node
  235. node *insertafter (node *head, int d)
  236. {
  237.                //Initialization and declaration of variables
  238.                int key;
  239.                int found = 0;
  240.                node *list, *tempnode;
  241.               
  242.                //Allocating memory dynamically for the new node
  243.                tempnode = (node *)malloc(sizeof(node));
  244.               
  245.                tempnode->data = d;
  246.                printf("Enter value of key :")          ;
  247.                scanf("%d",&key);
  248.         list = head;
  249.    
  250.         while (found == 0 && list != NULL)     
  251.         {
  252.                     if (list -> data == key)
  253.                     {
  254.                               tempnode -> next = list -> next;
  255.                               list -> next = tempnode;
  256.                               found = 1;
  257.                     }
  258.                               else
  259.                                   list = list -> next;
  260.                 }
  261.                 
  262.                if ( found == 0)
  263.                   printf ("Key Node not found!");
  264.               
  265.                return head;
  266. }
  267.  
  268. //Function to insert a node at a particular position
  269. node *insertatparticularposition(node *head, int d)
  270. {
  271.                //Declaration and initialization of variables
  272.                int keyposition;
  273.                int currentposition=1;
  274.                int validposition=0;
  275.                node *list, *tempnode;
  276.               
  277.                //Dynamically allocation of memory for the new node
  278.                tempnode = (node *)malloc(sizeof(node));
  279.                tempnode->data = d;
  280.               
  281.                //Input value of keynode
  282.                printf("Enter the position :")          ;
  283.                scanf("%d",&keyposition);
  284.               
  285.                list=head;
  286.                //If the position is in first location
  287.                if(keyposition==0)
  288.                {
  289.                               //Insert the new node at the begining
  290.                               tempnode -> next = head;
  291.                               head=tempnode;
  292.                               return head;
  293.                }
  294.                //Inserting the new node at the particular location
  295.                while(validposition==0 && list !=NULL)
  296.                {
  297.                               if(currentposition==keyposition)
  298.                               {
  299.                                              tempnode -> next = list -> next;
  300.                                              list -> next = tempnode;
  301.                                              validposition=1;
  302.                               }
  303.                               else
  304.                               {
  305.                                              currentposition=currentposition+1;
  306.                                              list=list -> next;
  307.                               }
  308.                }
  309.                if(validposition==0)
  310.                {
  311.                               printf("Invalid Position.");
  312.                }
  313.               
  314.                return head;
  315. }
  316.  
  317. //Function to delete the nodes of a linked list
  318. node *delet(node *head)
  319. {
  320.                //Initializing the option variable
  321.                int option;
  322.               
  323.                //Displaying the deletion options
  324.                printf("Deletion options:");
  325.                printf("\n1. Deletion of First Node");
  326.                printf("\n2. Deletion of Last Node");
  327.                printf("\n3. Deletion of node with a particular value");
  328.                printf("\n4. Deletion of a node at a given position");
  329.                printf("\nEnter option : ");
  330.                scanf("%d", &option);
  331.                switch(option)
  332.                {
  333.                              
  334.                               case 1:
  335.                                              head = deletbegin(head);
  336.                                              break;
  337.                case 2:
  338.                                              head = deletend(head);
  339.                                              break;
  340.                               case 3:
  341.                                               head = deletvalue(head);
  342.                                               break;
  343.                               case 4:
  344.                                              head = deletatparticularposition(head);
  345.                                              break;
  346.                               //Diplaying the default option
  347.                               default:
  348.                                              printf("\nInvalid choice. Deletion not done.");                                                                    
  349.                }
  350.                 printf("\nContents of Linked List after Deletion: \n");
  351.                traverse(head); 
  352.                return head;       
  353. }
  354.  
  355. //Function to delete the first node of the list
  356. node *deletbegin(node *head)
  357. {
  358.                //Declaring the variables
  359.                node *delnode, *list;
  360.               
  361.                //If list does not exist
  362.                if (head == NULL)
  363.                {
  364.                               printf("Empty List");
  365.                               return NULL;
  366.                }
  367.                //initializing the variable delnode with head
  368.                delnode = head;
  369.  
  370.                //Deleting the first node
  371.                head = head -> next;
  372.  
  373.                //Deallocating the deleted node
  374.                free(delnode);
  375.                return head;
  376. }
  377.  
  378. //Function to delete the last node if a list
  379. node *deletend(node *head)
  380. {
  381.                //Declaring the variables
  382.                node *delnode, *list;
  383.               
  384.                //If the list does not exist
  385.                if (head == NULL)
  386.                {
  387.                               printf("Empty List");
  388.                               return NULL;
  389.                }
  390.               
  391.                //If the node to be deleted is the first node
  392.                if (head -> next == NULL)
  393.                {
  394.                               //Deleting the node
  395.                               delnode=head;
  396.                               head = NULL;
  397.                               free(delnode);
  398.                               return(head);
  399.                }
  400.               
  401.                list = head;
  402.                //Deleting the node
  403.                while( list -> next -> next != NULL )
  404.                   list = list -> next;
  405.         delnode = list -> next;
  406.                list -> next = NULL;
  407.               
  408.                //Deallocating the deleted node
  409.                free(delnode);
  410.                return(head);
  411. }
  412.  
  413. //Function to delete a node with a particular value
  414. node *deletvalue(node *head)
  415. {
  416.                //Declaration and initialization of variables
  417.                int key, found = 0;
  418.                node *delnode, *list;
  419.               
  420.                //Inputt the value
  421.                printf("Enter value of node that you want to delete : ");
  422.                scanf("%d", &key);
  423.                //If the list is empty
  424.                if (head == NULL)
  425.                {
  426.                               printf("Empty List");
  427.                               return NULL;
  428.                }
  429.               
  430.                //If the first node is the node with the value
  431.                if (head -> data == key)
  432.                {
  433.                               //Deleting the node
  434.                               delnode=head;
  435.                               head = head-> next;
  436.                               free(delnode);
  437.                               return(head);
  438.                }
  439.                 
  440.                list = head;
  441.                //Deletion of that particular valued node
  442.                while(list -> next != NULL && found == 0)
  443.                {
  444.                               if(list -> next -> data == key)
  445.                               {
  446.                                              found = 1;
  447.                                              delnode=list->next;
  448.                                              list->next = list -> next -> next;
  449.                                             
  450.                                              //Deallocating the node
  451.                                              free(delnode);
  452.                                              return(head);
  453.                               }
  454.                               list=list->next;
  455.                }
  456.                if(found==0)
  457.                               printf("Node not found");
  458.                return(head);                    
  459. }
  460.  
  461. //Deleting a node at a particular position
  462. node *deletatparticularposition(node *head)
  463. {
  464.                //Declaration and initialization of variables
  465.                int pos,cnt=1;
  466.               
  467.                //Input the value
  468.                printf("Enter position of node that you want to delete : ");
  469.                scanf("%d", &pos);
  470.               
  471.                node *p=head,*tmp;
  472.                //To check valid position
  473.                if(pos<1 || pos>countNodes(head))
  474.                               printf("\nWrong position value, try again:");
  475.                else
  476.                {
  477.                              
  478.                               if(pos==1)//if position is 1
  479.                               {
  480.                                              p=head;
  481.                                              head=head->next;
  482.                               }
  483.                               else//if position is more than 1
  484.                               {
  485.                                             
  486.                                              while(cnt<pos)
  487.                                              {
  488.                                                             cnt++;
  489.                                                             tmp=p;
  490.                                                             p=p->next;
  491.                                              }
  492.                                             
  493.                                              tmp->next=p->next;//insert node
  494.                               }
  495.                              
  496.                               free(p);
  497.                }
  498.                return head;
  499. }
  500.  
  501. int countNodes(node *head)
  502. {
  503.                //Declaration and initialization of variables
  504.     int count=0;
  505.     node *p;
  506.     p=head;
  507.     //Count the number of nodes
  508.     while(p!=NULL)
  509.     {
  510.         count++;
  511.         p=p->next;
  512.     }
  513.     return(count);
  514. } 

OUTPUTS :-

OUTPUT 1 :-

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :23

Enter data :34

Enter data :45

 

Contents of Linked List :

23 -> 34 -> 45

 

Insertion of a new node :

Enter data to insert :12

Insertion options:

1. At the beginning

2. At the end

3. Before a key node

4. After a key node

5. At a particular position

Enter option : 1

 

Contents of Linked List after Insertion:

12 -> 23 -> 34 -> 45

 

Deletion of Node :

Deletion options:

1. Deletion of First Node

2. Deletion of Last Node

3. Deletion of node with a particular value

4. Deletion of a node at a given position

Enter option : 1

 

Contents of Linked List after Deletion:

23 -> 34 -> 45

 

 

OUTPUT 2 :-

 

 

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :12

Enter data :23

Enter data :34

 

Contents of Linked List :

12 -> 23 -> 34

 

Insertion of a new node :

Enter data to insert :45

Insertion options:

1. At the beginning

2. At the end

3. Before a key node

4. After a key node

5. At a particular position

Enter option : 2

 

Contents of Linked List after Insertion:

12 -> 23 -> 34 -> 45

 

Deletion of Node :

Deletion options:

1. Deletion of First Node

2. Deletion of Last Node

3. Deletion of node with a particular value

4. Deletion of a node at a given position

Enter option : 2

 

Contents of Linked List after Deletion:

12 -> 23 -> 34

 

OUTPUT 3:-

 

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :12

Enter data :23

Enter data :45

 

Contents of Linked List :

12 -> 23 -> 45

 

Insertion of a new node :

Enter data to insert :34

Insertion options:

1. At the beginning

2. At the end

3. Before a key node

4. After a key node

5. At a particular position

Enter option : 3

Enter value of key :45

 

Contents of Linked List after Insertion:

12 -> 23 -> 34 -> 45

 

Deletion of Node :

Deletion options:

1. Deletion of First Node

2. Deletion of Last Node

3. Deletion of node with a particular value

4. Deletion of a node at a given position

Enter option : 3

 

Enter value of node that you want to delete : 45

Contents of Linked List after Deletion:

12 -> 23 -> 34

 

OUTPUT 4:-

 

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :12

Enter data :23

Enter data :34

 

Contents of Linked List :

12 -> 23 -> 34

Insertion of a new node :

Enter data to insert :31

Insertion options:

1. At the beginning

2. At the end

3. Before a key node

4. After a key node

5. At a particular position

Enter option : 4

Enter value of key :23

 

Contents of Linked List after Insertion:

12 -> 23 -> 31 -> 34

 

Deletion of Node :

Deletion options:

1. Deletion of First Node

2. Deletion of Last Node

3. Deletion of node with a particular value

4. Deletion of a node at a given position

Enter option : 4

Enter position of node that you want to delete : 4

 

Contents of Linked List after Deletion:

12 -> 23 -> 31