Advertisement
heisenberger

Untitled

Aug 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define TRAILER -9999
  5.  
  6. struct Node {
  7.   int data;
  8.   struct Node *next;
  9. };
  10.  
  11. struct Node *head = NULL;
  12.  
  13. void Display(struct Node *p) {
  14.   if(p == NULL) return;
  15.   printf("%d ", p->data);
  16.   Display(p->next);
  17. }
  18.  
  19. void append(int x) {
  20.   struct Node *temp = (struct Node*) malloc (sizeof(struct Node));
  21.   struct Node *temp1 = (struct Node*) malloc (sizeof(struct Node));
  22.   temp->data = x;
  23.   temp->next = NULL;
  24.   if(head == NULL) head = temp;
  25.   else {
  26.     temp1 = head;
  27.     while(temp1->next != NULL) {
  28.       temp1 = temp1->next;
  29.     }
  30.     temp1->next = temp;
  31.   }
  32.   printf("List: ");
  33.   Display(head);
  34.   printf("\n");
  35. }
  36.  
  37. void countNodes() {
  38.   int counter = 0;
  39.   struct Node *temp = (struct Node *) malloc (sizeof(struct Node));
  40.   temp = head;
  41.   while(temp != NULL) {
  42.     temp = temp->next;
  43.     counter++;
  44.   }
  45.   printf("Nodes = %d\n", counter);
  46. }
  47.  
  48. void searchList() {
  49.   int x;
  50.   printf("Enter the value to be searched: ");
  51.   scanf("%d", &x);
  52.   struct Node *temp = (struct Node *) malloc (sizeof(struct Node));
  53.   temp = head;
  54.   int counter = 0;
  55.   while(temp->next != NULL) {
  56.     counter++;
  57.     if(temp->data == x) {
  58.       printf("%d is stored in node #%d\n", x, counter);
  59.       break;
  60.     }
  61.     temp = temp->next;
  62.   }
  63.   if(temp->next == NULL && temp->data != x) {
  64.     printf("%d is not present in the list\n", x);
  65.   }
  66. }
  67.  
  68. void reverseList(struct Node *p) {
  69.   if(p->next == NULL) {
  70.     head = p;
  71.     return;
  72.   }
  73.   reverseList(p->next);
  74.   struct Node *q = p->next;
  75.   q->next = p;
  76.   p->next = NULL;
  77. }
  78.  
  79. void menu() {
  80.   int input;
  81.   do {
  82.     printf("\nEnter your preference: \n");
  83.     printf("1 - Count the number of nodes\n");
  84.     printf("2 - Reverse the list\n");
  85.     printf("3 - Search an element in the list\n");
  86.     printf("4 - Exit\n");
  87.     scanf("%d", &input);
  88.  
  89.     switch(input) {
  90.       case 1: countNodes(); break;
  91.       case 2:
  92.         reverseList(head);
  93.         printf("Reversed List: ");
  94.         Display(head);
  95.         printf("\n");
  96.         break;
  97.       case 3: searchList(); break;
  98.       case 4: break;
  99.       default: printf("Invalid input\n"); break;
  100.     }
  101.   } while(input != 4);
  102. }
  103.  
  104. int main() {
  105.   int input, x;
  106.   while(x != TRAILER) {
  107.     printf("Enter value (%d to exit): ", TRAILER);
  108.     scanf("%d", &x);
  109.     if(x != TRAILER) append(x);
  110.   }
  111.  
  112.   menu();
  113.  
  114.   return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement