Advertisement
Limited_Ice

Lists.cpp

Jul 31st, 2020
1,859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. //  This is a test of Linked List stuff
  2. //  07/31/2020
  3.  
  4. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5.  
  6. /*    In this code I am going to attempt to make a linked list that keeps a record to make random access possible    
  7. */
  8.  
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. //  Header Files
  11.  
  12. using namespace std;
  13.  
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <vector>
  17. #include "lists.h"
  18.  
  19.  
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. // Functions prototypes
  22.  
  23. void breek();
  24. void printnums(numlist*);
  25. void printrefs(numlist*);
  26. void removenum(numlist*);
  27.  
  28. vector <numlist*> numlist::index;
  29. int numlist::indexcount = 0;
  30. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //  Main Program
  32.  
  33. int main() {
  34.     int input;
  35.     int choice;
  36.     int count;
  37.     vector <numlist*> tempvect;
  38.     numlist* newnum = nullptr;
  39.     do {
  40.         breek();
  41.         cout << "What do you want to do?\n\n1) Store a number\n2)view your list of numbers\n3)view the index of mem addresses\n4)delete a number\ntype \"-1\" to exit\n\nYour Choice: ";
  42.         cin >> choice;
  43.         breek();
  44.         switch (choice) {
  45.  
  46.         case 1:
  47.             cout << "\nenter the number to store: ";
  48.             cin >> input;
  49.             newnum = new numlist(input);
  50.             count = numlist::getcout();
  51.             breek();
  52.             tempvect = numlist::getlist();
  53.             break;
  54.  
  55.         case 2:
  56.             printnums(newnum);
  57.             breek();
  58.             break;
  59.        
  60.         case 3:
  61.             printrefs(newnum);
  62.             breek();
  63.             break;
  64.  
  65.         case 4:
  66.             try {
  67.                 removenum(newnum);
  68.                 count = numlist::getcout();
  69.                 tempvect = numlist::getlist();
  70.                 if (count != 0) {
  71.                     newnum = reinterpret_cast<numlist*>(tempvect[0]);
  72.                     break;
  73.                 }
  74.             }
  75.             catch (string error) {
  76.                 cout << error << endl;
  77.                 break;
  78.             }
  79.        
  80.         }
  81.     } while (choice != -1);
  82.  
  83.  
  84.  
  85.         return 0;
  86. }
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. // Function Definitions
  89. void breek() {
  90.     cout << setw(80) << setfill('*') << '*' << endl;
  91. }
  92.  
  93. void printnums(numlist* temp) {
  94.  
  95.     int count = numlist::getcout();
  96.     vector <numlist*> tempvect = numlist::getlist();
  97.     cout << "Your List:\n" << endl;
  98.     for (int i = 0; i < count; i++) {
  99.         temp = reinterpret_cast<numlist*>(tempvect[i]);
  100.         cout << i + 1 << ") " << temp->getint() << endl;
  101.     }
  102.     cout << endl;
  103.    
  104.  
  105. }
  106.  
  107. void printrefs(numlist* temp) {
  108.  
  109.     int count = numlist::getcout();
  110.     vector <numlist*> tempvect = numlist::getlist();
  111.     for (int i = 0; i < count; i++)
  112.         cout << i + 1 << ") " << tempvect[i] << endl;
  113.     cout << endl;
  114.  
  115. }
  116.  
  117. void removenum(numlist* temp) {
  118.  
  119.     int numremove;
  120.     vector <numlist*> tempvect = numlist::getlist();
  121.     temp = reinterpret_cast<numlist*>(tempvect[0]);
  122.     int numcomp = temp->getint();
  123.     int count = temp->getcout();
  124.     int counter = 0;
  125.     bool check = true;
  126.     printnums(temp);
  127.     cout << "Which number do you want to remove?\nThe number: ";
  128.     cin >> numremove;
  129.    
  130.     for (int i = 0; i < count-1; i++) {
  131.         if (numremove == numcomp) {
  132.             check = false;
  133.             break;
  134.         }
  135.         else
  136.         {
  137.             //cout << "counter = " << counter << endl;
  138.             temp = reinterpret_cast<numlist*>(tempvect[counter + 1]);
  139.             //cout << "number selected: " << temp->getint() << endl;;
  140.             numcomp = temp->getint();
  141.             //cout << "numcomp2 =  " << numcomp << endl;
  142.             counter++;
  143.  
  144.         }
  145.     }
  146.     if (numremove != numcomp)
  147.         throw string("Error: The number you entered was not part of your list.\n");
  148.        
  149.     if (check == true)
  150.             numlist::reduce(counter - 1);
  151.  
  152.     else
  153.             numlist::reduce(0);
  154.     delete temp;
  155.     temp = reinterpret_cast<numlist*>(tempvect[0]);
  156.    
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement