fueanta

Singly Linked List

Nov 25th, 2016
158
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4.  
  5. struct node {
  6.     int data;
  7.     node *next;
  8. } *head, *curr, *newNODE;
  9.  
  10. int length;
  11.  
  12. void listInitiate();
  13. void create_List(int);
  14. void show_List();
  15. void search_List(int);
  16. void insert_AT_first(int);
  17. void insert_after_value(int, int);
  18. void insert_before_value(int, int);
  19. void insert_between_values(int, int, int);
  20. void insert_AT_pos(int, int);
  21. void insert_AT_last(int);
  22. void delete_AT_first();
  23. void delete_after_value(int);
  24. void delete_before_value(int);
  25. void delete_between_values(int, int);
  26. void delete_AT_pos(int);
  27. void delete_AT_last();
  28.  
  29. int main() {
  30.     // decorate your-self.....
  31. }
  32.  
  33. void listInitiate() {
  34.     head = NULL;
  35. }
  36.  
  37. void create_List(int n) {
  38.     listInitiate();
  39.     cout << "\nCreating a new list of size " << n << "." << endl; length = n;
  40.     for (int i = 0; i < n; i++) {
  41.         int item; cout << "\nElement " << i + 1 << ": "; cin >> item;
  42.         newNODE = new node;
  43.         newNODE->data = item;
  44.         newNODE->next = NULL;
  45.         if (head == NULL) {
  46.             head = newNODE;
  47.             curr = head;
  48.         }
  49.         else {
  50.             curr->next = newNODE;
  51.             curr = curr->next;
  52.         }
  53.     }
  54. }
  55.  
  56. void show_List() {
  57.     node *i; int l;
  58.     if (head != NULL) {
  59.         cout << "\nCurrently in the List, you have following elements: \n";
  60.         for (i = head, l = 1; i != NULL; i = i->next, l++) {
  61.             cout << "\nElement " << l << ": " << i->data << endl;
  62.         }
  63.         cout << "\nEnd of the list." << endl;
  64.     }
  65.     else cout << "\nYour list is empty." << endl;
  66. }
  67.  
  68. void search_List(int item) {
  69.     node *i; int l;
  70.     for (i = head, l = 1; i->data != item && i->next != NULL; l++)
  71.         i = i->next;
  72.     if (i->data == item)
  73.         cout << "\nElement found..! Position of first appearance in the list : " << l << endl;
  74.     else cout << "\nSorry! Element isn't in the list." << endl;
  75. }
  76.  
  77. void insert_AT_first(int item) {
  78.     newNODE = new node;
  79.     newNODE->data = item;
  80.     newNODE->next = head;
  81.     head = newNODE;
  82.     cout << "\nElement " << item << " has been added as the new head of the list." << endl;
  83. }
  84.  
  85. void insert_after_value(int value, int item) {
  86.     node *i; int l;
  87.     for (i = head, l = 1; i->data != value && i->next != NULL; l++)
  88.         i = i->next;
  89.     if (i->data == value) {
  90.         newNODE = new node;
  91.         newNODE->data = item;
  92.         newNODE->next = i->next;
  93.         i->next = newNODE;
  94.         cout << "\nElement " << value << " has been found on the list at postion no: " << l
  95.             << " and " << item << " has been added after " << value << " at position no: "
  96.             << l + 1 << endl;
  97.     }
  98.     else {
  99.         cout << "\nOPERATION failed..!" << endl;
  100.         cout << "Searched for " << value << " among the " << l << " elements of the list but "
  101.             << value << " could not be found." << endl;
  102.     }
  103. }
  104.  
  105. void insert_before_value(int value, int item) {
  106.     node *i; int l;
  107.     if (head->data == value) {
  108.         insert_AT_first(item);
  109.         cout << "\nElement " << value <<
  110.             " has been found on the head so " << item << " has been added as the new head."
  111.             << endl;
  112.     }
  113.     else {
  114.         for (i = head, l = 1; i->next->data != value && i->next != NULL; l++)
  115.             i = i->next;
  116.         if (i->next->data == value) {
  117.             newNODE = new node;
  118.             newNODE->data = item;
  119.             newNODE->next = i->next;
  120.             i->next = newNODE;
  121.             cout << "\nElement " << value << " has been found on the list at postion no: " << l + 1
  122.                 << " and " << item << " has been added before " << value << " at position no: "
  123.                 << l + 1 << endl;
  124.         }
  125.         else {
  126.             cout << "\nOPERATION failed..!" << endl;
  127.             cout << "Searched for " << value << " among the " << l << " elements of the list but "
  128.                 << value << " could not be found." << endl;
  129.         }
  130.     }
  131. }
  132.  
  133. void insert_between_values(int val1, int val2, int item) {
  134.     node *i; int l;
  135.     for (i = head, l = 1; i->data != val1 && i->next != NULL; l++)
  136.         i = i->next;
  137.     if (i->data == val1 && i->next->data == val2) {
  138.         newNODE = new node;
  139.         newNODE->data = item;
  140.         newNODE->next = i->next;
  141.         i->next = newNODE;
  142.         cout << "\nElement " << val1 << " has been found on the list at postion no: " << l
  143.             << " and element " << val2 << " has been found on the list at positon no: " << l + 1
  144.             << " So, " << item << " has been added between them at position no: " << l + 1 << endl;
  145.     }
  146.     else {
  147.         cout << "\nOPERATION failed..!" << endl;
  148.         cout << val1 << " and " << val2 << " couldn't be found sequencially." << endl;
  149.     }
  150. }
  151.  
  152. void insert_AT_pos(int position, int item) {
  153.     node *i; int l;
  154.     if (position == 1) {
  155.         insert_AT_first(item);
  156.     }
  157.     else {
  158.         for (i = head, l = 1; l != position - 1 && i->next != NULL; l++)
  159.             i = i->next;
  160.         if (l == position - 1) {
  161.             newNODE = new node;
  162.             newNODE->data = item;
  163.             newNODE->next = i->next;
  164.             i->next = newNODE;
  165.             cout << "\nElement " << item << " has been added at the list at position no: "
  166.                 << l + 1 << endl;
  167.         }
  168.         else {
  169.             cout << "\nOPERATION failed..!" << endl;
  170.             cout << "\nGiven position is out of the range." << endl;
  171.         }
  172.     }
  173. }
  174.  
  175. void insert_AT_last(int item) {
  176.     node *i; int l;
  177.     newNODE = new node;
  178.     newNODE->data = item;
  179.     newNODE->next = NULL;
  180.     if (head == NULL) {
  181.         head = newNODE;
  182.         cout << "\nList was empty so, data " << item << " has been added at the list as the head."
  183.             << endl;
  184.     }
  185.     else {
  186.         for (i = head, l = 1; i->next != NULL; l++)
  187.             i = i->next;
  188.         i->next = newNODE;
  189.         cout << "\nElement " << item << " has been added at the tail of the list at position no: "
  190.             << l + 1 << endl;
  191.     }
  192. }
  193.  
  194. void delete_AT_first() {
  195.     node *i = head;
  196.     head = head->next;
  197.     cout << "\nPrevious head " << i->data << " has been deleted." << endl;
  198.     delete i;
  199.     if (head != NULL)
  200.         cout << "\nNew head value: " << head->data << endl;
  201. }
  202.  
  203. void delete_after_value(int value) {
  204.     node *i; int l;
  205.     for (i = head, l = 1; i->data != value && i->next != NULL; l++)
  206.         i = i->next;
  207.     if (i->data == value && i->next != NULL) {
  208.         newNODE = new node;
  209.         newNODE = i->next;
  210.         i->next = newNODE->next;
  211.         newNODE->next = NULL;
  212.         delete newNODE;
  213.         cout << "\nElement " << value << " has been found at position no: " << l << " so, item at"
  214.             << " position no: " << l + 1 << " has been deleted." << endl;
  215.     }
  216.     else if (i->data == value && i->next == NULL) {
  217.         cout << "\nElement found at position no: " << l << ", which is the last element of the list."
  218.             << " So, operation could not be done." << endl;
  219.     }
  220.     else {
  221.         cout << "\nOPERATION Failed..!" << endl;
  222.         cout << "\nGiven Value isn't in the list." << endl;
  223.     }
  224. }
  225.  
  226. void delete_before_value(int value) {
  227.     if (head->data == value) {
  228.         cout << "\nOperation Failed. No element exists before the head." << endl;
  229.     }
  230.     else if (head->next != NULL) {
  231.         if (head->next->data == value)
  232.             delete_AT_first();
  233.         else if (head->next->next != NULL) {
  234.             node *i; int l;
  235.             for (i = head, l = 3; i->next->next->data != value && i->next->next->next != NULL; l++)
  236.                 i = i->next;
  237.             if (i->next->next->data == value) {
  238.                 newNODE = new node;
  239.                 newNODE = i->next;
  240.                 i->next = newNODE->next;
  241.                 newNODE->next = NULL;
  242.                 delete newNODE;
  243.                 cout << "\nElement " << value << " has been found at position no: " << l << " therefore,"
  244.                     << " element at position no: " << l - 1 << " has been deleted." << endl;
  245.             }
  246.             else cout << "\nOperation Failed. Such element doesn't exist on the list." << endl;
  247.         }
  248.         else cout << "\nOperation Failed. Such element doesn't exist on the list." << endl;
  249.     }
  250.     else cout << "\nOperation Failed. Such element doesn't exist on the list." << endl;
  251. }
  252.  
  253. void delete_between_values(int val1, int val2) {
  254.     node *i; int l;
  255.     for (i = head, l = 1; i->data != val1 && i->next != NULL; l++)
  256.         i = i->next;
  257.     if (i->data == val1 && i->next != NULL) {
  258.         if (i->next->next != NULL && i->next->next->data == val2) {
  259.             newNODE = new node;
  260.             newNODE = i->next;
  261.             i->next = i->next->next;
  262.             newNODE->next = NULL;
  263.             cout << "\nElement " << val1 << " has been found at position no: " << l << " and element "
  264.                 << val2 << " has been found at position no: " << l + 2 << " therefore, data "
  265.                 << newNODE->data << " has been deleted from position no: " << l + 1 << endl;
  266.         }
  267.         else cout << "\nNo such element exists between " << val1 << " and " << val2
  268.             << " or may be " << val2 << " doesn't exist." << endl;
  269.     }
  270.     else cout << "\nEither " << val1 << " or " << val2 << " or both doesn't exist." << endl;
  271. }
  272.  
  273. void delete_AT_pos(int position) {
  274.     node *i; int l;
  275.     if (position == 1)
  276.         delete_AT_first();
  277.     else {
  278.         for (i = head, l = 1; l != position - 1 && i->next != NULL; l++)
  279.             i = i->next;
  280.         if (i->next == NULL)
  281.             cout << "\nInvalid position." << endl;
  282.         else {
  283.             newNODE = new node;
  284.             newNODE = i->next;
  285.             i->next = newNODE->next;
  286.             newNODE->next = NULL;
  287.             cout << "\nElement at position no: " << position << " is " << newNODE->data << " and just got"
  288.                 << " deleted." << endl;
  289.             delete newNODE;
  290.         }
  291.     }
  292. }
  293.  
  294. void delete_AT_last() {
  295.     node *i; int l;
  296.     if (head->next == NULL)
  297.         delete_AT_first();
  298.     else {
  299.         for (i = head, l = 1; i->next->next != NULL; l++)
  300.             i = i->next;
  301.         newNODE = new node;
  302.         newNODE = i->next;
  303.         i->next = NULL;
  304.         cout << "\nLast element of the list was " << newNODE->data << " which was at position no: "
  305.             << l + 1 << " and just got deleted." << endl;
  306.         delete newNODE;
  307.     }
  308. }
RAW Paste Data