Question: PLEASE DO NOT SPAM THE QUESTION I WILL AUTOMATICALLY DISLIKE IT (IF YOU SOLVE IT PLEASE POST A PICTURE OF IT WORKING ON YOUR END,
PLEASE DO NOT SPAM THE QUESTION I WILL AUTOMATICALLY DISLIKE IT (IF YOU SOLVE IT PLEASE POST A PICTURE OF IT WORKING ON YOUR END, THANKS)
In C++ Fix: Add a new method to the List ADT program that will search the list for a specified value and return its index in the list (or -1 if not found). Also, implement the Insert and Print methods. Please follow the comments in the code. There is a .cpp and .h file.
// File : list_demo.cpp // Desc: Implement a Linked List ADT by randomly calling // list operations with random values // -------------------------------------------------------- #include
break; case LIST_DELETE: //deleteList(&list); break; } // print the current list printList(&list); // prompt user to continue std::cout << " Again (N/n)? "; std::cin.get(again); std::cout << std::endl; } while(again != 'N' && again != 'n'); return 0; // return success to OS } // main /** * Append a random number to the end of the list * @param list -- pointer to a list ADT */ void appendList(List* list) { srand(time(NULL)); // refresh random numbers int value = rand() % 100; // calc random number value list->insert(value); // test 1-parameter insert w/ default position std::cout << "Appended " << value << " at " << list->getSize() - 1 << std::endl; } /** * Insert a random number at a random position * @param list -- pointer to a list ADT */ void insertList(List* list) { int value = rand() % 100; // calc random number value int position = list->getSize(); // init to size in case list is empty if(list->isEmpty()) { list->insert(value); // test 1-parameter insert w/ default position } else { position = rand() % list->getSize(); // calc random position (0 <-> (size - 1)) list->insert(value, position); // test 2-parameter insert } std::cout << "Inserted " << value << " at " << position << std::endl; } // insertList /** * Update a random list position with a new random number
* @param list -- pointer to a list ADT */ void updateList(List* list) { if (list->isEmpty()) { std::cout << "Update failed: list empty!" << std::endl; } else { // calc random number value and list position int value = rand() % 100; int position = rand() % list->getSize(); list->modify(value, position); // test modify method std::cout << "Updated " << value << " at " << position << std::endl; } } // updateList /** * Delete 1 value at a random position in the list * @param list -- pointer to a list ADT */ void deleteList(List* list) { if (list->isEmpty()) { std::cout << "Delete failed: list empty!" << std::endl; } else { int position = rand() % list->getSize(); // calc random position int value = list->remove(position); // test remove method std::cout << "Deleted " << value << " from " << position << std::endl; } } // deleteList /** * Print the current list to the console * @param list -- pointer to a list ADT */ void printList(List* list) { int size =list->getSize(); if (size) { std::cout << "List = {"; for (int i = 0; i < size; i++) { std::cout << list->read(i); if (i < size - 1) std::cout << ","; } std::cout << "}" << std::endl; } else { std::cout << "List is empty!" << std::endl; } } // printList
// File : List.h // Desc : Linked List ADT interface // -------------------------------------------------------- #ifndef LINKEDLIST_LIST_H #define LINKEDLIST_LIST_H // constants // -------------------------------------------------------- const int LIST_HEAD = 0; // list position of head node const int LIST_TAIL = -1; // specify current tail position class List { private: // internal storage structure for a Node struct Node { int value; Node* next; }; Node* head; // hold reference to head of the list (Node[0]) int size; // current size of the list (empty==0) public: List(); ~List(); int getSize(); bool isEmpty(); void insert(int value, int position=LIST_TAIL); int remove(int position=LIST_TAIL); int read(int position); void modify(int value, int position); private: Node* traverse(int position); }; #endif //LINKEDLIST_LIST_H
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
