Advertisement
skb50bd

SortedLinkedList

Jun 6th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 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 = 0;
  59.     for (Node *Current = Head; Current != NULL; Current = Current -> next)
  60.         if (Current -> data < num)
  61.             loc++;
  62.     if (loc == 0)
  63.         add(num);
  64.     else if (loc < count())
  65.         addAfter(num, loc);
  66.     else
  67.         append(num);
  68. }
  69.  
  70. int Delete(int num) {
  71.     for (Node *Current = Head, *Previous; Current != NULL; Previous = Current, Current = Current -> next) {
  72.         if (Current -> data == num) {
  73.             if (Current = Head)
  74.                 Head = Head -> next;
  75.             else
  76.                 Previous -> next = Current -> next;
  77.             free(Current);
  78.             return 1;
  79.         }
  80.     }
  81.     return 0;
  82. }
  83.  
  84. int main() {
  85.     int choice, num;
  86.     Head = NULL;
  87.  
  88.     while (1) {
  89.         printf("List Operations: \n");
  90.         printf("================\n");
  91.         printf("1. Add\n");
  92.         printf("2. Display\n");
  93.         printf("3. Delete\n");
  94.         printf("4. Size\n");
  95.         printf("5. Exit\n");
  96.         printf("Choice: ");
  97.         scanf("%d", &choice);
  98.  
  99.         switch (choice) {
  100.             case 1: {
  101.                 printf("Enter the Number to Add: ");
  102.                 scanf("%d", &num);
  103.                 insert(num);
  104.                 break;
  105.             }
  106.             case 2: {
  107.                 display();
  108.                 break;
  109.             }
  110.             case 3: {
  111.                 if (Head == NULL)
  112.                     printf("List is Empty. Nothing to Delete.\n");
  113.                 else {
  114.                     printf("Enter Number to Delete: ");
  115.                     scanf("%d", &num);
  116.                     if (Delete(num) == 1)
  117.                         printf("Successfully Deleted %d\n", num);
  118.                     else
  119.                         printf("%d Not Deleted.\nValue Might Not Found\n", num);
  120.                 }
  121.                 break;
  122.             }
  123.             case 4: {
  124.                 printf("Size of The list is %d\n", count());
  125.                 break;
  126.             }
  127.             case 5: {
  128.                 exit(0);
  129.             }
  130.             default: {
  131.                 printf("Invalid Input. Try Again\n");
  132.             }
  133.         }
  134.         printf("\n\n");
  135.     }
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement