Add remove, replace, update public members to call private members and handle output
This commit is contained in:
parent
cf38979faa
commit
fb0251c2b6
|
@ -12,7 +12,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
enum OPS {
|
enum OPS {
|
||||||
EXIT, INSERT, INSERTAT, EMPTY, PEEK, PRINT, FIND
|
EXIT, INSERT, INSERTAT, EMPTY, PEEK, PRINT, FIND, REMOVE, REPLACE
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
@ -22,12 +22,12 @@ int main()
|
||||||
SingleList testList;
|
SingleList testList;
|
||||||
bool exit = false;
|
bool exit = false;
|
||||||
int choice = -1;
|
int choice = -1;
|
||||||
int val, ins;
|
int val, key;
|
||||||
while (!exit)
|
while (!exit)
|
||||||
{
|
{
|
||||||
std::cout << "##### Singly Linked List Menu #####\n\t0. Exit"
|
std::cout << "##### Singly Linked List Menu #####\n\t0. Exit"
|
||||||
<< "\n\t1. Insert\n\t2. Insert at\n\t3. Empty list\n\t4. Peek top of list"
|
<< "\n\t1. Insert\n\t2. Insert at\n\t3. Empty list\n\t4. Peek top of list"
|
||||||
<< "\n\t5. Print list\n\t6. Find\n";
|
<< "\n\t5. Print list\n\t6. Find\n\t7. Remove\n\t8. Replace\n";
|
||||||
std::cin >> choice;
|
std::cin >> choice;
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
switch (choice)
|
switch (choice)
|
||||||
|
@ -44,18 +44,15 @@ int main()
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case INSERTAT:
|
case INSERTAT:
|
||||||
std::cout << "Enter a value to insert at within our list: ";
|
std::cout << "Enter a new value to add to our list: ";
|
||||||
std::cin >> ins;
|
|
||||||
std::cin.clear();
|
|
||||||
std::cout << "Enter a value to add to our list: ";
|
|
||||||
std::cin >> val;
|
std::cin >> val;
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
if (!testList.insert(val, ins)) {
|
std::cout << "Enter an existing value to insert at within our list: ";
|
||||||
std::cout << "No changes made, [" << ins << "] was not found in our list\n";
|
std::cin >> key;
|
||||||
}
|
std::cin.clear();
|
||||||
else {
|
if (testList.insert(val, key)) {
|
||||||
std::cout << "List after inserting ["
|
std::cout << "List after inserting ["
|
||||||
<< val << "] at [" << ins << "]: \n";
|
<< val << "] at [" << key << "]: \n";
|
||||||
testList.print();
|
testList.print();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -65,20 +62,43 @@ int main()
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PEEK:
|
case PEEK:
|
||||||
std::cout << "[" << testList.peek() << "] is at the top of our list\n";
|
testList.peek();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PRINT:
|
case PRINT:
|
||||||
testList.print();
|
testList.print();
|
||||||
|
// test2.print();
|
||||||
|
// test3.print();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FIND:
|
case FIND:
|
||||||
std::cout << "Enter a value to search for within our list: ";
|
std::cout << "Enter an existing value to search for within our list: ";
|
||||||
std::cin >> val;
|
std::cin >> val;
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
testList.find(val);
|
testList.find(val);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case REMOVE:
|
||||||
|
std::cout << "Enter an existing value to remove from our list: ";
|
||||||
|
std::cin >> val;
|
||||||
|
std::cin.clear();
|
||||||
|
testList.remove(val);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case REPLACE:
|
||||||
|
std::cout << "Enter a new value to add to our list: ";
|
||||||
|
std::cin >> val;
|
||||||
|
std::cin.clear();
|
||||||
|
std::cout << "Enter an existing value to replace within our list: ";
|
||||||
|
std::cin >> key;
|
||||||
|
std::cin.clear();
|
||||||
|
if (testList.replace(val, key)) {
|
||||||
|
std::cout << "List after replacing ["
|
||||||
|
<< key << "] by [" << val << "]: \n";
|
||||||
|
testList.print();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
std::cout << "Invalid entry...\n";
|
std::cout << "Invalid entry...\n";
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -56,14 +56,11 @@ SingleList::~SingleList()
|
||||||
*/
|
*/
|
||||||
bool SingleList::insert(int val)
|
bool SingleList::insert(int val)
|
||||||
{
|
{
|
||||||
Node *newNode = new Node(val);
|
bool inserted = insert(val, head);
|
||||||
if (isEmpty())
|
if (inserted)
|
||||||
head = newNode;
|
std::cout << "[" << val << "] was inserted into the list\n";
|
||||||
else {
|
else std::cout << "[" << val << "] could not be inserted into the list\n";
|
||||||
newNode->next = head;
|
return inserted;
|
||||||
head = newNode;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** insert at
|
/** insert at
|
||||||
|
@ -75,16 +72,44 @@ bool SingleList::insert(int val)
|
||||||
*/
|
*/
|
||||||
bool SingleList::insert(int val, int key)
|
bool SingleList::insert(int val, int key)
|
||||||
{
|
{
|
||||||
Node *newNode = new Node(val);
|
bool inserted = insert(val, key, head);
|
||||||
if (isEmpty() || head->data == key) return insert(val);
|
if (inserted)
|
||||||
|
std::cout << "[" << val << "] was inserted into the list\n";
|
||||||
Node *result = findPrevious(key, head);
|
else std::cout << "[" << val << "] could not be inserted into the list\n";
|
||||||
if (result == NULL) return false;
|
return inserted;
|
||||||
else {
|
|
||||||
newNode->next = result->next;
|
|
||||||
result->next = newNode;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** remove
|
||||||
|
* @brief Removes a value in the list by calling a private member and handling output
|
||||||
|
*
|
||||||
|
* @param val Value to be removed from the list
|
||||||
|
* @return true If the value was removed from the list
|
||||||
|
* @return false If the value was not removed from the list
|
||||||
|
*/
|
||||||
|
bool SingleList::remove(int val)
|
||||||
|
{
|
||||||
|
bool removed = remove(val, head);
|
||||||
|
if (removed)
|
||||||
|
std::cout << "[" << val << "] was removed from the list\n";
|
||||||
|
else std::cout << "[" << val << "] could not be removed from the list\n";
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** replace
|
||||||
|
* @brief Replaces a value in the list by calling a private member and handling output
|
||||||
|
*
|
||||||
|
* @param val Value to insert into the list
|
||||||
|
* @param key Value to be replaced within the list
|
||||||
|
* @return true If the key has been replaced in the list by val
|
||||||
|
* @return false If the key has not been replaced in the list by val
|
||||||
|
*/
|
||||||
|
bool SingleList::replace(int val, int key)
|
||||||
|
{
|
||||||
|
bool replaced = replace(val, key, head);
|
||||||
|
if (replaced)
|
||||||
|
std::cout << "[" << key << "] was replaced by [" << val << "] in the list\n";
|
||||||
|
else std::cout << "[" << key << "] could not be replaced by [" << val << "] in the list\n";
|
||||||
|
return replaced;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** makeEmpty
|
/** makeEmpty
|
||||||
|
@ -93,7 +118,7 @@ bool SingleList::insert(int val, int key)
|
||||||
void SingleList::makeEmpty()
|
void SingleList::makeEmpty()
|
||||||
{
|
{
|
||||||
Node *temp;
|
Node *temp;
|
||||||
while(head != NULL) {
|
while(!isEmpty()) {
|
||||||
temp = head;
|
temp = head;
|
||||||
head = head->next;
|
head = head->next;
|
||||||
delete temp;
|
delete temp;
|
||||||
|
@ -116,10 +141,11 @@ bool SingleList::isEmpty() const
|
||||||
*
|
*
|
||||||
* @return int The value held at the Node pointed to by SingleList::head
|
* @return int The value held at the Node pointed to by SingleList::head
|
||||||
*/
|
*/
|
||||||
int SingleList::peek() const
|
void SingleList::peek() const
|
||||||
{
|
{
|
||||||
if (head == NULL) return -1;
|
if (!isEmpty())
|
||||||
else return head->data;
|
std::cout << "[" << head->data << "] is at the top of our list\n";
|
||||||
|
else std::cout << "Nothing to peek, our list is empty...\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
/** print
|
/** print
|
||||||
|
@ -128,7 +154,8 @@ int SingleList::peek() const
|
||||||
*/
|
*/
|
||||||
void SingleList::print() const
|
void SingleList::print() const
|
||||||
{
|
{
|
||||||
print(head);
|
if(!isEmpty()) print(head);
|
||||||
|
else std::cout << "Nothing to print, our list is empty...\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
/** find
|
/** find
|
||||||
|
@ -154,6 +181,92 @@ bool SingleList::find(int val) const
|
||||||
* Private Member Functions
|
* Private Member Functions
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
/** insert
|
||||||
|
* @brief Private member to handle inserting value into the list
|
||||||
|
*
|
||||||
|
* @param val Value to be inserted
|
||||||
|
* @param head The head of the list to insert the value into
|
||||||
|
* @return true If the value was inserted
|
||||||
|
* @return false If the value could not be inserted
|
||||||
|
*/
|
||||||
|
bool SingleList::insert(int val, Node *&head)
|
||||||
|
{
|
||||||
|
Node *newNode = new Node(val);
|
||||||
|
// If the list is not empty, update next pointer to head node
|
||||||
|
if (!isEmpty()) newNode->next = head;
|
||||||
|
// Always set head to our newNode
|
||||||
|
head = newNode;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** insert at
|
||||||
|
* @brief Private member to handle inserting a value at a key within our list
|
||||||
|
*
|
||||||
|
* @param val Value to be inserted
|
||||||
|
* @param key Key value to search for within the list
|
||||||
|
* @param head Head node of the list to insert to
|
||||||
|
* @return true If the value was inserted
|
||||||
|
* @return false If the value was not inserted
|
||||||
|
*/
|
||||||
|
bool SingleList::insert(int val, int key, Node *&head)
|
||||||
|
{
|
||||||
|
Node *newNode = new Node(val);
|
||||||
|
if (isEmpty()) return false;
|
||||||
|
// Let insert() handle inserting at the head
|
||||||
|
else if (head->data == key) return insert(val);
|
||||||
|
|
||||||
|
Node *keyNode = findPrevious(key, head);
|
||||||
|
// If there was no keyNode found, the key does is not in our list
|
||||||
|
// Don't insert anything, return false and let caller decide whats next
|
||||||
|
if (keyNode == NULL) return false;
|
||||||
|
// Set the newNode to come BEFORE our keyNode
|
||||||
|
newNode->next = keyNode->next;
|
||||||
|
keyNode->next = newNode;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** remove
|
||||||
|
* @brief Private member to remove values from the list
|
||||||
|
*
|
||||||
|
* @param val Value to be removed
|
||||||
|
* @param head Head of the list to remove the value from
|
||||||
|
* @return true If the value has been removed from the list
|
||||||
|
* @return false If the value has not been removed from the list
|
||||||
|
*/
|
||||||
|
bool SingleList::remove(int val, Node *&head)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (head == NULL) return false;
|
||||||
|
else if (head->data == val) {
|
||||||
|
head = head->next;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node *prevNode = findPrevious(val, head);
|
||||||
|
if (prevNode == NULL) return false;
|
||||||
|
Node *gtfo = prevNode->next;
|
||||||
|
prevNode->next = prevNode->next->next;
|
||||||
|
delete gtfo;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** replace
|
||||||
|
* @brief Private member to replace values within the list
|
||||||
|
*
|
||||||
|
* @param val Value to insert into the list
|
||||||
|
* @param key Value to be replaced within the list
|
||||||
|
* @param head Head of the list to replace the value
|
||||||
|
* @return true If the key has been replaced by val within the list
|
||||||
|
* @return false If the key has not been replaced by val within the list
|
||||||
|
*/
|
||||||
|
bool SingleList::replace(int val, int key, Node *&head)
|
||||||
|
{
|
||||||
|
Node *replacee = find(key, head);
|
||||||
|
if (replacee == NULL) return false;
|
||||||
|
replacee->data = val;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/** find
|
/** find
|
||||||
* @brief Find and return a Node which contains the given value
|
* @brief Find and return a Node which contains the given value
|
||||||
*
|
*
|
||||||
|
@ -162,16 +275,17 @@ bool SingleList::find(int val) const
|
||||||
*/
|
*/
|
||||||
SingleList::Node* SingleList::find(int val, Node *start) const
|
SingleList::Node* SingleList::find(int val, Node *start) const
|
||||||
{
|
{
|
||||||
|
// If given a NULL list, return NULL
|
||||||
|
// If given a head which contains the requested value, return the foundNode
|
||||||
if (start == NULL || start->data == val) return start;
|
if (start == NULL || start->data == val) return start;
|
||||||
|
|
||||||
Node *temp = start;
|
Node *foundNode = start;
|
||||||
while (temp->next != NULL) {
|
while (foundNode->next != NULL) {
|
||||||
temp = temp->next;
|
foundNode = foundNode->next;
|
||||||
if (temp->data == val) break;
|
if (foundNode->data == val) return foundNode;
|
||||||
}
|
}
|
||||||
|
// If we have not yet returned a foundNode, the key is not in our list
|
||||||
if (temp->data == val) return temp;
|
return NULL;
|
||||||
else return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** findPrevious
|
/** findPrevious
|
||||||
|
@ -209,4 +323,3 @@ void SingleList::print(Node *start) const
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,9 +26,11 @@ class SingleList{
|
||||||
~SingleList();
|
~SingleList();
|
||||||
bool insert(int val);
|
bool insert(int val);
|
||||||
bool insert(int val, int key);
|
bool insert(int val, int key);
|
||||||
|
bool remove(int val);
|
||||||
|
bool replace(int val, int key);
|
||||||
void makeEmpty();
|
void makeEmpty();
|
||||||
bool isEmpty() const;
|
bool isEmpty() const;
|
||||||
int peek() const;
|
void peek() const;
|
||||||
void print() const;
|
void print() const;
|
||||||
bool find(int val) const;
|
bool find(int val) const;
|
||||||
|
|
||||||
|
@ -40,6 +42,10 @@ class SingleList{
|
||||||
Node(int val): data(val), next(NULL) {};
|
Node(int val): data(val), next(NULL) {};
|
||||||
};
|
};
|
||||||
Node *head;
|
Node *head;
|
||||||
|
bool insert(int val, Node *&head);
|
||||||
|
bool insert(int val, int key, Node *&head);
|
||||||
|
bool remove(int val, Node *&head);
|
||||||
|
bool replace(int val, int key, Node *&head);
|
||||||
Node* find(int val, Node *start) const;
|
Node* find(int val, Node *start) const;
|
||||||
Node* findPrevious(int val, Node *start) const;
|
Node* findPrevious(int val, Node *start) const;
|
||||||
void print(Node *start) const;
|
void print(Node *start) const;
|
||||||
|
|
Loading…
Reference in New Issue