Advertisement
skb50bd

Locationwise_Linked_List

Jun 14th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. struct Node {
  5.     int data;
  6.     Node *next;
  7. } *Head;
  8.  
  9. int count() {
  10.     int c = 0;
  11.     for (Node *Current = Head; Current != NULL; Current = Current -> next)
  12.         c++;
  13.     return c;
  14. }
  15.  
  16. int display() {
  17.     if (Head == NULL)
  18.         printf("List is Empty");
  19.     for (Node *Current = Head; Current != NULL; Current = Current -> next)
  20.         printf("%d ", Current -> data);
  21.     printf("\n\n");
  22. }
  23.  
  24. void add(int num) {
  25.     Node *NewNode = (Node *) malloc(sizeof(Node));
  26.     NewNode -> data = num;
  27.     NewNode -> next = NULL;
  28.     if (Head != NULL)
  29.         NewNode -> next = Head;
  30.     Head = NewNode;
  31. }
  32.  
  33. void addAfter(int num, int loc) {
  34.     int i;
  35.     Node *left, *right, *NewNode;
  36.     right = Head;
  37.     for (i = 0; i < loc; i++) {
  38.         left = right;
  39.         right = right -> next;
  40.     }
  41.     NewNode = (Node *) malloc(sizeof(Node));
  42.     NewNode -> data = num;
  43.     left -> next = NewNode;
  44.     NewNode -> next = right;
  45. }
  46.  
  47. void append(int num) {
  48.     Node *NewNode = (Node *) malloc(sizeof(Node));
  49.     NewNode -> data = num;
  50.     NewNode -> next = NULL;
  51.     Node *Current = Head;
  52.     while (Current -> next != NULL)
  53.         Current = Current -> next;
  54.     Current -> next = NewNode;
  55. }
  56.  
  57. void insert(int num) {
  58.     int loc;
  59.     int c = count();
  60.     while (1) {
  61.         printf("Enter the Location: ");
  62.         scanf("%d", &loc);
  63.         if (loc > c || loc < 0)
  64.             printf("Invalid Location.\nMaximum Available Location is: %d\n\n", c);
  65.         else
  66.             break;
  67.     }
  68.     if (loc == 0)
  69.         add(num);
  70.     else if (loc < count())
  71.         addAfter(num, loc);
  72.     else
  73.         append(num);
  74. }
  75.  
  76. int Delete(int num) {
  77.     for (Node *Current = Head, *Previous; Current != NULL; Previous = Current, Current = Current -> next) {
  78.         if (Current -> data == num) {
  79.             if (Current == Head)
  80.                 Head = Head -> next;
  81.             else
  82.                 Previous -> next = Current -> next;
  83.             free(Current);
  84.             return 1;
  85.         }
  86.     }
  87.     return 0;
  88. }
  89.  
  90. int main() {
  91.     int choice, num;
  92.     Head = NULL;
  93.  
  94.     while (1) {
  95.         printf("List Operations: \n");
  96.         printf("================\n");
  97.         printf("1. Add\n");
  98.         printf("2. Display\n");
  99.         printf("3. Delete\n");
  100.         printf("4. Size\n");
  101.         printf("5. Exit\n");
  102.         printf("Choice: ");
  103.         scanf("%d", &choice);
  104.  
  105.         switch (choice) {
  106.             case 1: {
  107.                 printf("Enter the Number to Add: ");
  108.                 scanf("%d", &num);
  109.                 insert(num);
  110.                 break;
  111.             }
  112.             case 2: {
  113.                 display();
  114.                 break;
  115.             }
  116.             case 3: {
  117.                 if (Head == NULL)
  118.                     printf("List is Empty. Nothing to Delete.\n");
  119.                 else {
  120.                     printf("Enter Number to Delete: ");
  121.                     scanf("%d", &num);
  122.                     if (Delete(num) == 1)
  123.                         printf("Successfully Deleted %d\n", num);
  124.                     else
  125.                         printf("%d Not Deleted.\nValue Might Not Found\n", num);
  126.                 }
  127.                 break;
  128.             }
  129.             case 4: {
  130.                 printf("Size of The list is %d\n", count());
  131.                 break;
  132.             }
  133.             case 5: {
  134.                 exit(0);
  135.             }
  136.             default: {
  137.                 printf("Invalid Input. Try Again\n");
  138.             }
  139.         }
  140.         printf("\n\n");
  141.     }
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement