Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a test of Linked List stuff
- // 07/31/2020
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /* In this code I am going to attempt to make a linked list that keeps a record to make random access possible
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Header Files
- using namespace std;
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include "lists.h"
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Functions prototypes
- void breek();
- void printnums(numlist*);
- void printrefs(numlist*);
- void removenum(numlist*);
- vector <numlist*> numlist::index;
- int numlist::indexcount = 0;
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Main Program
- int main() {
- int input;
- int choice;
- int count;
- vector <numlist*> tempvect;
- numlist* newnum = nullptr;
- do {
- breek();
- 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: ";
- cin >> choice;
- breek();
- switch (choice) {
- case 1:
- cout << "\nenter the number to store: ";
- cin >> input;
- newnum = new numlist(input);
- count = numlist::getcout();
- breek();
- tempvect = numlist::getlist();
- break;
- case 2:
- printnums(newnum);
- breek();
- break;
- case 3:
- printrefs(newnum);
- breek();
- break;
- case 4:
- try {
- removenum(newnum);
- count = numlist::getcout();
- tempvect = numlist::getlist();
- if (count != 0) {
- newnum = reinterpret_cast<numlist*>(tempvect[0]);
- break;
- }
- }
- catch (string error) {
- cout << error << endl;
- break;
- }
- }
- } while (choice != -1);
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Function Definitions
- void breek() {
- cout << setw(80) << setfill('*') << '*' << endl;
- }
- void printnums(numlist* temp) {
- int count = numlist::getcout();
- vector <numlist*> tempvect = numlist::getlist();
- cout << "Your List:\n" << endl;
- for (int i = 0; i < count; i++) {
- temp = reinterpret_cast<numlist*>(tempvect[i]);
- cout << i + 1 << ") " << temp->getint() << endl;
- }
- cout << endl;
- }
- void printrefs(numlist* temp) {
- int count = numlist::getcout();
- vector <numlist*> tempvect = numlist::getlist();
- for (int i = 0; i < count; i++)
- cout << i + 1 << ") " << tempvect[i] << endl;
- cout << endl;
- }
- void removenum(numlist* temp) {
- int numremove;
- vector <numlist*> tempvect = numlist::getlist();
- temp = reinterpret_cast<numlist*>(tempvect[0]);
- int numcomp = temp->getint();
- int count = temp->getcout();
- int counter = 0;
- bool check = true;
- printnums(temp);
- cout << "Which number do you want to remove?\nThe number: ";
- cin >> numremove;
- for (int i = 0; i < count-1; i++) {
- if (numremove == numcomp) {
- check = false;
- break;
- }
- else
- {
- //cout << "counter = " << counter << endl;
- temp = reinterpret_cast<numlist*>(tempvect[counter + 1]);
- //cout << "number selected: " << temp->getint() << endl;;
- numcomp = temp->getint();
- //cout << "numcomp2 = " << numcomp << endl;
- counter++;
- }
- }
- if (numremove != numcomp)
- throw string("Error: The number you entered was not part of your list.\n");
- if (check == true)
- numlist::reduce(counter - 1);
- else
- numlist::reduce(0);
- delete temp;
- temp = reinterpret_cast<numlist*>(tempvect[0]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement