Advertisement
ivorakitin

LinkedList

Apr 15th, 2025 (edited)
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.04 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct elem {
  6.   int key;
  7.   elem * next;
  8. }* start;
  9.  
  10. void init();
  11. void add_b(int n);
  12. void add_mid1(int n, int k);
  13. void add_mid2(int n, int k);
  14. void add_e(int n);
  15. int del_b(int & n);
  16. int del_mid1(int & n, int k);
  17. int del_mid2(int & n, int k);
  18. int del_e(int & n);
  19. int search_iter(int n);
  20. void list();
  21. void destroy(elem *&listt);
  22. void addtolist(int array[], int l, elem *&s);
  23. void add_e(elem *&s, int n);
  24.  
  25. int main() {
  26.   int num, k, ch;
  27.   init();
  28.   do {
  29.     cout << "\n Menu \n";
  30.     cout << "1. Add the first element\n";
  31.     cout << "2. Add element before k\n";
  32.     cout << "3. Add element after k\n";
  33.     cout << "4. Add the last element\n";
  34.     cout << "5. Del the first element\n";
  35.     cout << "6. Del element before k\n";
  36.     cout << "7. Del element after k \n";
  37.     cout << "8. Del the last element\n";
  38.     cout << "9. Search element\n";
  39.     cout << "10. Show the list\n";
  40.     cout << "11. Destroy the list\n";
  41.     cout << "12. Add to list\n";
  42.     cout << "0. Exit\n";
  43.     cout << "Your choice:";
  44.     cin >> ch;
  45.     switch (ch) {
  46.     case 1:
  47.       cout << "num=";
  48.       cin >> num;
  49.       add_b(num);
  50.       break;
  51.     case 2:
  52.       cout << "num=";
  53.       cin >> num;
  54.       cout << "k=";
  55.       cin >> k;
  56.       add_mid1(num, k);
  57.       break;
  58.     case 3:
  59.       cout << "num=";
  60.       cin >> num;
  61.       cout << "k=";
  62.       cin >> k;
  63.       add_mid2(num, k);
  64.       break;
  65.     case 4:
  66.       cout << "num=";
  67.       cin >> num;
  68.       add_e(num);
  69.       break;
  70.     case 5:
  71.       if (del_b(num))
  72.         list();
  73.       break;
  74.     case 6:
  75.       cout << "num=";
  76.       cin >> num;
  77.       cout << "k=";
  78.       cin >> k;
  79.       if (del_mid1(num, k))
  80.         list();
  81.       break;
  82.     case 7:
  83.       cout << "num=";
  84.       cin >> num;
  85.       cout << "k=";
  86.       cin >> k;
  87.       if (del_mid2(num, k))
  88.         list();
  89.       break;
  90.     case 8:
  91.       if (del_e(num))
  92.         list();
  93.       break;
  94.     case 9:
  95.       cout << "num=";
  96.       cin >> num;
  97.       if (search_iter(num))
  98.         cout << "\n" << num << " is found!\n";
  99.       else
  100.         cout << "\n " << num << " is not found!\n";
  101.       break;
  102.     case 10:
  103.       list();
  104.       break;
  105.     case 11:
  106.         destroy(start);
  107.         break;
  108.     case 12:
  109.         {
  110.             int array[10] = { 10, 20, 30, 55, 66, 20, 54, 554, 534, 5325};
  111.            
  112.             addtolist(array, 10, start);
  113.         }
  114.         break;
  115.     }
  116.   } while (ch != 0);
  117.   return 0;
  118.   system("pause");
  119. }
  120.  
  121. void add_e(elem *&s, int n)
  122. {
  123.   elem *p = s, *q = s;
  124.   q = new elem;
  125.   q->key = n;
  126.   q->next = NULL;
  127.   if (s)
  128.   {
  129.     while (p -> next)
  130.       p = p -> next;
  131.     p -> next = q;
  132.   } else
  133.     s = q;
  134. }
  135.  
  136. void addtolist(int array[], int l, elem *&s)
  137. {
  138.     for (int i = 0; i < l; i++)
  139.     {
  140.         add_e(s, array[i]);
  141.     }
  142. }
  143.  
  144. void destroy(elem *&listt)
  145. {
  146.     while (listt)
  147.     {
  148.         elem *t = listt;
  149.         cout << "Deleting: " << listt->key << endl;
  150.         listt = listt->next;
  151.         delete t;  
  152.     }
  153. }
  154.  
  155. void init() {
  156.   start = NULL;
  157. }
  158. void add_b(int n) {
  159.   elem * p = start;
  160.   start = new elem;
  161.   start -> key = n;
  162.   start -> next = p;
  163. }
  164.  
  165. void add_mid1(int n, int k) {
  166.   elem * q = NULL, * p = start;
  167.   while (p -> key != k)
  168.     p = p -> next;
  169.   q = new elem;
  170.   q -> next = p -> next;
  171.   q -> key = p -> key;
  172.   p -> next = q;
  173.   p -> key = n;
  174. }
  175.  
  176. void add_mid2(int n, int k) {
  177.   elem * q = NULL, * p = start;
  178.   while (p -> key != k)
  179.     p = p -> next;
  180.   q = new elem;
  181.   q -> next = p -> next;
  182.   q -> key = n;
  183.   p -> next = q;
  184. }
  185.  
  186. void add_e(int n) {
  187.   elem * p = start, * q = start;
  188.   q = new elem;
  189.   q -> key = n;
  190.   q -> next = NULL;
  191.   if (start) {
  192.     while (p -> next)
  193.       p = p -> next;
  194.     p -> next = q;
  195.   } else
  196.     start = q;
  197. }
  198.  
  199. int del_b(int & n) {
  200.   elem * p = start;
  201.   if (start) {
  202.     n = start -> key;
  203.     start = start -> next;
  204.     delete p;
  205.     return 1;
  206.   } else
  207.     return 0;
  208. }
  209.  
  210. int del_mid1(int & n, int k) {
  211.   elem * q = NULL, * p = start;
  212.   if (start) {
  213.     while (p -> key != k) {
  214.       q = p;
  215.       p = p -> next;
  216.     }
  217.     n = q -> key;
  218.     q -> next = p -> next;
  219.     q -> key = p -> key;
  220.  
  221.     delete p;
  222.     return 1;
  223.   } else
  224.     return 0;
  225. }
  226.  
  227. int del_mid2(int & n, int k) {
  228.   elem * q = NULL, * p = start;
  229.   if (start) {
  230.     while (p -> key != k)
  231.       p = p -> next;
  232.     q = p -> next;
  233.     n = q -> key;
  234.  
  235.     p -> next = q -> next;
  236.     delete q;
  237.     return 1;
  238.   } else
  239.     return 0;
  240. }
  241.  
  242. int del_e(int & n) {
  243.   elem * q = NULL, * p = start;
  244.   if (start) {
  245.     if (p -> next) {
  246.       while (p -> next) {
  247.         q = p;
  248.         p = p -> next;
  249.       }
  250.       q -> next = NULL;
  251.     } else
  252.       start = NULL;
  253.     n = p -> key;
  254.     delete p;
  255.     return 1;
  256.   } else
  257.     return 0;
  258. }
  259.  
  260. int search_iter(int n) {
  261.   elem * p = start;
  262.   if (start) {
  263.     while ((p -> key != n) && (p -> next))
  264.       p = p -> next;
  265.     if (p -> key == n)
  266.       return 1;
  267.     else
  268.       return 0;
  269.   }
  270.   return 0;
  271. }
  272.  
  273. void list()
  274. {
  275.     elem *p = start;
  276.     if (start)
  277.     while (p)
  278.     {
  279.         cout << p->key << "\t";
  280.         p = p->next;
  281.     }
  282.     cout << "\n\n";
  283. }
  284.  
Tags: linkedlist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement