Advertisement
Jaagdish47

Linked List CODE

Nov 23rd, 2024
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.02 KB | Source Code | 0 0
  1. Linked List 1 and 2
  2. 7A. Write a program to search the elements in the linked list and display the
  3. same
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>  // for malloc and exit
  7. #include <conio.h>   // for getch() (Turbo C-specific)
  8.  
  9. struct node {
  10.     int info;
  11.     struct node *next;
  12. };
  13.  
  14. int main() {
  15.     struct node *new_node, *ptr, *begin = NULL;
  16.     int item;
  17.     int option;
  18.  
  19.     // Input first element
  20.     new_node = (struct node*)malloc(sizeof(struct node));
  21.     if (!new_node) {
  22.         printf("Memory allocation failed\n");
  23.         return 1; // Exit if malloc fails
  24.     }
  25.  
  26.     printf("\nEnter the element: ");
  27.     scanf("%d", &item);
  28.     new_node->info = item;
  29.     new_node->next = NULL;
  30.     begin = new_node;
  31.     ptr = begin;
  32.  
  33.     // Loop for entering more elements
  34.     printf("\nDo you want to enter another element? 1.Yes, 2.No: ");
  35.     scanf("%d", &option);
  36.     while (option != 2) {
  37.         new_node = (struct node*)malloc(sizeof(struct node));
  38.         if (!new_node) {
  39.             printf("Memory allocation failed\n");
  40.             return 1; // Exit if malloc fails
  41.         }
  42.        
  43.         printf("\nEnter the element: ");
  44.         scanf("%d", &item);
  45.         new_node->info = item;
  46.         new_node->next = NULL;
  47.         ptr->next = new_node;
  48.         ptr = new_node;
  49.  
  50.         printf("\nDo you want to enter another element? 1.Yes, 2.No: ");
  51.         scanf("%d", &option);
  52.     }
  53.  
  54.     // Display the linked list
  55.     printf("\nThe Linked List:\n");
  56.     ptr = begin;
  57.     while (ptr != NULL) {
  58.         printf("\t%d", ptr->info);
  59.         ptr = ptr->next;
  60.     }
  61.  
  62.     // Search for an element
  63.     printf("\nEnter the element to be searched: ");
  64.     scanf("%d", &item);
  65.     ptr = begin;
  66.     while (ptr != NULL) {
  67.         if (ptr->info == item) {
  68.             printf("\nElement found in the list!\n");
  69.             getch(); // Wait for user input before exiting (Turbo C-specific)
  70.             return 0; // Exit successfully
  71.         }
  72.         ptr = ptr->next;
  73.     }
  74.    
  75.     printf("\nThe element is not present in the list!\n");
  76.  
  77.     getch(); // Wait for user input before exiting (Turbo C-specific)
  78.     return 0; // Exit successfully
  79. }
  80.  
  81. 7B. Write a program to create a single linked list and display the node elements
  82. in the reverse order.
  83.  
  84. #include <stdio.h>
  85. #include <stdlib.h>  // for malloc and free
  86. #include <conio.h>   // for getch() (Turbo C-specific)
  87.  
  88. struct node {
  89.     int info;
  90.     struct node *next;
  91. };
  92.  
  93. struct node* createNode(int data) {
  94.     struct node *newNode = (struct node*)malloc(sizeof(struct node));
  95.     if (!newNode) {
  96.         printf("Memory allocation failed\n");
  97.         exit(1);
  98.     }
  99.     newNode->info = data;
  100.     newNode->next = NULL;
  101.     return newNode;
  102. }
  103.  
  104. void displayNormal(struct node *head) {
  105.     struct node *ptr = head;
  106.     printf("The Linked List in Normal Order:\n");
  107.     while (ptr != NULL) {
  108.         printf("%d ", ptr->info);
  109.         ptr = ptr->next;
  110.     }
  111.     printf("\n");
  112. }
  113.  
  114. void displayReverse(struct node *head) {
  115.     if (head == NULL) {
  116.         return;
  117.     }
  118.     displayReverse(head->next);
  119.     printf("%d ", head->info);
  120. }
  121.  
  122. int main() {
  123.     struct node *head = NULL, *ptr = NULL;
  124.     int item, option;
  125.  
  126.     do {
  127.         printf("Enter an element to add to the list: ");
  128.         scanf("%d", &item);
  129.  
  130.         struct node *newNode = createNode(item);
  131.         if (head == NULL) {
  132.             head = newNode;
  133.         } else {
  134.             ptr->next = newNode;
  135.         }
  136.         ptr = newNode;
  137.  
  138.         printf("Do you want to add another element? 1.Yes, 2.No: ");
  139.         scanf("%d", &option);
  140.     } while (option == 1);
  141.  
  142.     displayNormal(head);
  143.     printf("The Linked List in Reverse Order:\n");
  144.     displayReverse(head);
  145.     printf("\n");
  146.  
  147.     // Free the memory allocated for the linked list
  148.     ptr = head;
  149.     while (ptr != NULL) {
  150.         struct node *temp = ptr;
  151.         ptr = ptr->next;
  152.         free(temp);
  153.     }
  154.  
  155.     getch(); // Wait for user input before closing (Turbo C-specific)
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement