Advertisement
skb50bd

LinkedList-w/Options

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