Advertisement
Guest User

Stackoverflow Program

a guest
Oct 16th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. class node
  9. {
  10.     public:
  11.     int key;
  12.     double d_variable;
  13.     char ch_variable;
  14.     node* next;
  15. };
  16. class my_list
  17. {
  18.     public:
  19.     node * head;
  20.     int size;
  21.  
  22.     my_list()
  23.     {
  24.         head = NULL;
  25.         size = 0;
  26.     }
  27.     void AddOneElement(int value)   //adding one element with value provided by user
  28.     {
  29.         node* temp1,* temp2;
  30.         node** temp3;
  31.         temp3 = &head;
  32.         temp1 = new node;
  33.         temp1->key = value;
  34.         temp1->d_variable = rand();
  35.         temp1->ch_variable = 'T';
  36.         temp2 = head;
  37.         while (temp2 != NULL && temp2->key <= temp1->key)
  38.         {
  39.             if (temp2->key == temp1->key)
  40.                 return;
  41.             temp3 = &temp2->next;
  42.             temp2 = temp2->next;
  43.         }
  44.             *temp3 = temp1;
  45.             temp1->next = temp2;
  46.             size++;
  47.     }
  48.     void AddManyElements(int amount)    //adding many elements with random key value
  49.     {
  50.         for (int i = 0; i < amount; i++)
  51.         {
  52.         node* temp1,* temp2;
  53.         node** temp3;
  54.         temp3 = &head;
  55.         temp1 = new node;
  56.         temp1->key = ( 4  *rand()) % 99901 + 99;
  57.         temp1->d_variable = rand();
  58.         temp1->ch_variable = 'T';
  59.         temp2 = head;
  60.         while (temp2 != NULL && temp2->key <= temp1->key)
  61.             {
  62.                 if (temp2->key == temp1->key)
  63.                     {
  64.                     temp1->key = (4 * rand()) % 99901 + 99;
  65.                     temp2 = head;
  66.                     }
  67.                 else
  68.                     {
  69.                     temp3 = &temp2->next;
  70.                     temp2 = temp2->next;
  71.                     }
  72.             }
  73.                 *temp3 = temp1;
  74.                 temp1->next = temp2;
  75.                 size++;
  76.         }
  77.     }
  78.     /*void ShowElements()               //function that shows all elements, thats for me for testing purposes
  79.     {
  80.         node* temp;
  81.         temp = head;
  82.         int i = 0;
  83.         if(temp!=NULL)
  84.         {
  85.             while (temp!=NULL)
  86.             {
  87.                 cout << "Key number "<<i+1<<" :" << temp->key << endl;
  88.                 temp = temp->next;
  89.                 i++;
  90.             }
  91.         }
  92.         else    //case in which list is empty
  93.         {
  94.             cout << endl << "This list is empty!" << endl;
  95.         }
  96.  
  97.     }*/
  98.     void ListSize()
  99.     {
  100.         cout << endl << "Size:" << size<<endl;
  101.     }
  102.     void DeleteElement(int value)
  103.     {
  104.         node *temp1, *temp2,*temp3;
  105.         temp1 = new node;
  106.         temp3 = new node;
  107.         temp1->key = value;
  108.         temp2 = head;
  109.         if (temp2 == NULL)      //list is empty
  110.         {
  111.             cout << "There is no list" << endl;
  112.             return;
  113.         }
  114.         if (temp2 != NULL && temp2->key == temp1->key)  //element is on the first place
  115.         {
  116.             temp3 = temp2->next;
  117.             delete temp2;
  118.             head = temp3;
  119.             size--;
  120.             return;
  121.         }
  122.         while (temp2 != NULL && temp2->key != temp1->key)   //searching for the element through list
  123.         {
  124.             temp3 = temp2;
  125.             temp2 = temp2->next;
  126.         }
  127.         if (temp2 == NULL)return;   //there is no element
  128.         temp3->next = temp2->next;  //element has been found
  129.         delete temp2;
  130.         size--;
  131.     }
  132.     void DeleteList()
  133.     {
  134.         node* temp1;
  135.         temp1 = head;
  136.         if (temp1 == NULL)return;
  137.         else
  138.         {
  139.             while (temp1 != NULL)
  140.             {
  141.                 node* temp2;
  142.                 temp2 = temp1;
  143.                 temp1 = temp1->next;
  144.                 delete temp2;
  145.                 size--;
  146.             }
  147.             head = temp1;
  148.         }
  149.     }
  150.     void ShowFirstElements(int value)
  151.     {
  152.         if (value > size)return;
  153.         else
  154.         {
  155.             node* temp;
  156.             temp = head;
  157.             if (temp == NULL)return;
  158.             else
  159.             {
  160.                 for (int i = 0; i < value; i++)
  161.                 {
  162.                     cout << "Key:" << temp->key << endl;
  163.                     temp = temp->next;
  164.                 }
  165.             }
  166.  
  167.         }
  168.     }
  169.     void ShowLastElements(int value)
  170.     {
  171.         node* temp;
  172.         temp = head;
  173.         if (head == NULL)return;
  174.         if (value > size)return;
  175.         int j = size - value;
  176.         for (int i = 0; i <j; i++)
  177.         {
  178.             temp = temp->next;
  179.         }
  180.         while (temp != NULL)
  181.         {
  182.             cout << endl << "Key:" << temp->key;
  183.             temp = temp->next;
  184.         }
  185.     }
  186.     void FindKey(int value)
  187.     {
  188.         node* temp;
  189.         temp = head;
  190.         if (temp == NULL)
  191.         {
  192.             cout << endl << "List is empty" << endl;
  193.         }
  194.         else
  195.         {
  196.             while (temp != NULL && temp->key != value)
  197.             {
  198.                 temp = temp->next;
  199.             }
  200.             if (temp == NULL)
  201.             {
  202.                 cout << endl << "Element does not exist" << endl;
  203.             }
  204.             else
  205.             {
  206.                 cout << endl << "Element does exist";
  207.                 cout<< endl << "key:" << temp->key;
  208.                 cout << endl << "d_variable:" << temp->d_variable;
  209.                 cout << endl << "ch_variable:" << temp->ch_variable;
  210.             }
  211.         }
  212.  
  213.     }
  214. };
  215.  
  216. int main()
  217. {
  218.     clock_t begin, end;
  219.     double time_spent;
  220.     begin = clock();
  221.     ifstream my_file;
  222.     int amount=0, key1=0, key2=0, key3=0, key4=0, key5=0;
  223.     my_list testing;
  224.     srand(time(NULL));
  225.    
  226.  
  227.     my_file.open("inlab02.txt");
  228.    
  229.     if (my_file.good())
  230.     {
  231.         cout << "File opened succesfully, loading variables..." << endl;
  232.         my_file >> amount;
  233.         my_file >> key1;
  234.         my_file >> key2;
  235.         my_file >> key3;
  236.         my_file >> key4;
  237.         my_file >> key5;
  238.     }
  239.     else
  240.     {
  241.         cout << endl << "Could not open the file, ending program..." << endl;
  242.     }
  243.     testing.FindKey(key1);
  244.     testing.AddManyElements(amount);
  245.     testing.ListSize();
  246.     testing.ShowFirstElements(20);
  247.     cout << endl;
  248.     testing.AddOneElement(key2);
  249.     cout << endl;
  250.     testing.ShowFirstElements(20);
  251.     testing.AddOneElement(key3);
  252.     cout << endl;
  253.     testing.ShowFirstElements(20);
  254.     testing.AddOneElement(key4);
  255.     cout << endl;
  256.     testing.ShowFirstElements(20);
  257.     testing.AddOneElement(key5);
  258.     testing.DeleteElement(key3);
  259.     cout << endl;
  260.     testing.ShowFirstElements(20);
  261.     testing.DeleteElement(key2);
  262.     cout << endl;
  263.     testing.ShowFirstElements(20);
  264.     testing.DeleteElement(key5);
  265.     testing.ListSize();
  266.     testing.FindKey(key5);
  267.     cout << endl;
  268.     testing.ShowLastElements(11);
  269.     testing.DeleteList();
  270.     testing.ShowLastElements(11);
  271.     testing.ListSize();
  272.     my_file.close();
  273.     end = clock();
  274.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  275.     cout << endl << "Execution time:" << time_spent;
  276.     return 0;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement