Advertisement
rado_dimitrov66

Stack of number V2

May 29th, 2024
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Sstack
  5. {
  6.     int key;
  7.     Sstack* next;
  8. }*start = NULL;
  9.  
  10. struct Pstack
  11. {
  12.     int key;
  13.     Pstack* next;
  14. }*start2 = NULL;
  15.  
  16. void addElement(unsigned int& countOfNumber)
  17. {
  18.     Sstack* s;
  19.     s = start;
  20.     int Snum;
  21.     do
  22.     {
  23.         cout << "Enter Positive Number: ";
  24.         cin >> Snum;
  25.  
  26.     } while (Snum < 0);
  27.  
  28.  
  29.     start = new Sstack;
  30.  
  31.     start->key = Snum;
  32.     start->next = s;
  33.  
  34.     countOfNumber++;
  35.  
  36.  
  37.     cout << "Element added" << endl;
  38.  
  39.     cout << endl;
  40. }
  41.  
  42. void addElementToP(unsigned int& countOfNumber, unsigned int n)
  43. {
  44.     Pstack* s;
  45.     s = start2;
  46.  
  47.  
  48.     start2 = new Pstack;
  49.  
  50.     start2->key = n;
  51.     start2->next = s;
  52.  
  53.     countOfNumber++;
  54.  
  55.  
  56.     cout << "Element added" << endl;
  57.  
  58.     cout << endl;
  59. }
  60.  
  61. void removeElement(bool isP)
  62. {
  63.  
  64.     if (isP) {
  65.  
  66.  
  67.         while (start2)
  68.         {
  69.  
  70.             Pstack* s = start2;
  71.  
  72.             bool isLast = false;
  73.  
  74.             do
  75.             {
  76.                 if (!s->next) {
  77.  
  78.                     isLast = true;
  79.                     break;
  80.  
  81.                 }
  82.  
  83.                 if (s->next != start2) {
  84.                     s = s->next;
  85.                 }
  86.  
  87.             } while (s->next != start2);
  88.  
  89.             if (isLast)
  90.             {
  91.                 start2 = NULL;
  92.  
  93.                 delete s;
  94.  
  95.             }
  96.             else {
  97.                 start2 = s;
  98.  
  99.                 delete s;
  100.             }
  101.         }
  102.  
  103.  
  104.     }
  105.     else {
  106.  
  107.  
  108.         while (start)
  109.         {
  110.  
  111.             Sstack* s = start;
  112.  
  113.             bool isLast = false;
  114.  
  115.             do
  116.             {
  117.                 if (!s->next) {
  118.  
  119.                     isLast = true;
  120.                     break;
  121.  
  122.                 }
  123.  
  124.                 if (s->next != start) {
  125.                     s = s->next;
  126.                 }
  127.  
  128.             } while (s->next != start);
  129.  
  130.  
  131.             if (isLast)
  132.             {
  133.                 start = NULL;
  134.  
  135.                 delete s;
  136.  
  137.             }
  138.             else {
  139.                 start = s;
  140.  
  141.                 delete s;
  142.  
  143.  
  144.                 start->next = NULL;
  145.             }
  146.  
  147.         }
  148.  
  149.  
  150.         cout << "Element/s removed\n";
  151.  
  152.     }
  153.  
  154. }
  155.  
  156. void printElements()
  157. {
  158.     Sstack* s = start;
  159.  
  160.     while (s)
  161.     {
  162.         cout << s->key << " ";
  163.  
  164.         s = s->next;
  165.     }
  166.  
  167.     cout << endl;
  168.  
  169.     if (start2)
  170.     {
  171.         Pstack* s2 = start2;
  172.  
  173.         while (s2)
  174.         {
  175.             cout << s2->key << " ";
  176.  
  177.             s2 = s2->next;
  178.         }
  179.     }
  180.  
  181.     cout << endl;
  182. }
  183.  
  184. void sortElementInPStack(unsigned int countOfNumber)
  185. {
  186.     unsigned int count = 0;
  187.  
  188.     Sstack* s = start;
  189.  
  190.     int smallest = s->key;
  191.  
  192.     s = s->next;
  193.  
  194.     removeElement(true);
  195.  
  196.     while (count < countOfNumber)
  197.     {
  198.         for (int i = count + 1; i < countOfNumber; i++)
  199.         {
  200.             if (smallest > s->key)
  201.             {
  202.                 smallest = s->key;
  203.             }
  204.  
  205.             s = s->next;
  206.         }
  207.  
  208.         addElementToP(count, smallest);
  209.  
  210.         s = start;
  211.  
  212.         if (count < countOfNumber)
  213.         {
  214.             for (int i = 0; i < count; i++)
  215.             {
  216.                 s = s->next;
  217.             }
  218.  
  219.             smallest = s->key;
  220.         }
  221.  
  222.  
  223.  
  224.  
  225.     }
  226.  
  227.  
  228.  
  229.  
  230. }
  231.  
  232. int main()
  233. {
  234.     short choice;
  235.  
  236.     unsigned int countOfNumber = 0;
  237.  
  238.     do
  239.     {
  240.         cout << "[1] Add element\n[2] Remove element \n[3] Print elements\n[4] Sort by descending order\n[0] Exit\n";
  241.         cin >> choice;
  242.  
  243.         switch (choice)
  244.         {
  245.         case 1:
  246.             system("cls");
  247.  
  248.             addElement(countOfNumber);
  249.  
  250.             choice = -1;
  251.  
  252.             break;
  253.         case 2:
  254.  
  255.             system("cls");
  256.  
  257.             if (!start) {
  258.                 cout << "No elements to remove\n";
  259.             }
  260.             else {
  261.                 removeElement(false);
  262.             }
  263.  
  264.  
  265.             choice = -1;
  266.  
  267.             break;
  268.  
  269.         case 3:
  270.  
  271.             system("cls");
  272.  
  273.             if (!start) {
  274.                 cout << "No elements to print\n";
  275.             }
  276.             else {
  277.                 printElements();
  278.             }
  279.  
  280.             choice = -1;
  281.  
  282.             break;
  283.  
  284.         case 4:
  285.  
  286.             system("cls");
  287.  
  288.  
  289.             if (!start) {
  290.                 cout << "No elements to sort\n";
  291.             }
  292.             else {
  293.  
  294.                 if (countOfNumber > 1) {
  295.                     sortElementInPStack(countOfNumber);
  296.  
  297.                 }
  298.                 else {
  299.                     cout << "Only one element\n";
  300.                 }
  301.  
  302.             }
  303.  
  304.             choice = -1;
  305.  
  306.             break;
  307.         default:
  308.             break;
  309.         }
  310.  
  311.     } while (choice < 0 || choice > 4);
  312.  
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement