Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- #include <iostream>
- #include <string>
- #include "Header.h"
- LinkedList list;
- do {
- cout << "n~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
- cout << "Linked List Options:n" << endl;
- cout << "1 - Add an item" << endl;
- cout << "2 - Find an item" << endl;
- cout << "3 - Remove an item" << endl;
- cout << "4 - Display list" << endl;
- cout << "5 - Destroy list" << endl;
- cout << "6 - Destroy list and Exit" << endl;
- cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
- cin >> option;
- switch (option)
- {
- case 1:
- {
- cout << "Enter a number to add to the list:" << endl;
- cin >> num;
- cout << "Choose a place to insert the number:n 1 - First n 2 - Last" << endl;
- do {
- cin >> choice;
- if (choice == 1)
- {
- placement = true;
- break;
- }
- else if (choice == 2)
- {
- placement = false;
- break;
- }
- else
- {
- cout << "nInvalid entry. Choose again: " << endl;
- }
- }while(choice != 1 || choice != 2);
- list.insertItem(num, placement);
- break;
- }
- case 2:
- {
- cout << "Enter a value to search for:" << endl;
- cin >> num;
- list.findItem(num);
- break;
- }
- case 3:
- {
- cout << "Remove item via: n1 - Item Position n2 - Value" << endl;
- cin >> choice;
- if (choice == 1)
- {
- cout << "Enter an item's position, the first would be "1"" << endl;
- cin >> num;
- list.removeNodePosition(num);
- }
- else if (choice == 2)
- {
- cout << "Enter a value to remove:" << endl;
- cin >> num;
- list.removeValue(num);
- }
- else
- cout << "Invalid entry... restarting menu" << endl;
- break;
- }
- case 4:
- {
- list.display();
- break;
- }
- case 5:
- {
- list.destroyList();
- break;
- }
- case 6:
- {
- list.destroyList();
- break;
- }
- };
- } while (option != 6);
- return 0;
- };
- // driver.cpp
- // GemperlineA-Lab9
- //
- // Created by Adam Gemperline on 12/15/17.
- // Copyright © 2017 Adam Gemperline. All rights reserved.
- //
- #include "Header.h"
- #include <iostream>
- using namespace std;
- void LinkedList::insertItem(int n, bool atHead)
- {
- Node *newNode;
- Node *nodePtr;
- nodePtr = nullptr;
- //Allocate the new node
- newNode = new Node;
- //Store the value
- newNode->val = n;
- //Redirect pointer to null
- newNode->next = nullptr;
- if (atHead == true)
- {
- if (!head)
- {
- head = newNode;
- cout << "There were no nodes, first node created" << endl;
- }
- else
- {
- newNode->next = head;
- head = newNode;
- }
- }
- else
- {
- if (!head)
- {
- // if head is NULL, set the new node at the head of the list
- head = newNode;
- }
- else
- {
- nodePtr = head;
- // traverse through the list
- while (nodePtr->next)
- {
- //find the last node in the list
- nodePtr = nodePtr->next;
- };
- //insert newNode as the last node
- nodePtr->next = newNode;
- };
- }
- }
- void LinkedList::display()
- {
- Node *nodePtr;
- nodePtr = head;
- while (nodePtr != nullptr)
- {
- cout << nodePtr->val << " " << endl;
- nodePtr = nodePtr->next;
- };
- if (nodePtr == nullptr)
- {
- cout << "nEnd of list" << endl;
- }
- }
- int LinkedList::findItem(int n)
- {
- Node *nodePtr;
- nodePtr = head;
- while (nodePtr != nullptr)
- {
- if (nodePtr->val == n)
- {
- cout << "Yes, the list contains that number!" << endl;
- nodePtr = nodePtr->next;
- }
- else if (nodePtr->val != n && nodePtr != nullptr)
- nodePtr = nodePtr->next;
- else
- cout << "That number is not in the list..." << endl;
- }
- return 0;
- }
- bool LinkedList::removeNodePosition(int n)
- {
- Node *nodePtr;
- Node *previousNode;
- //if the list is empty, do nothing
- if (!head)
- {
- cout << "The list is empty." << endl;
- }
- for (int counter = 1; counter <= n; counter++)
- {
- //initialize nodePtr to head of the list
- nodePtr = head;
- //skip nodes whose value does not equal search value
- while (nodePtr != nullptr && counter != n)
- {
- previousNode = nodePtr;
- nodePtr = nodePtr->next;
- };
- if (counter == n)
- {
- previousNode = nodePtr;
- previousNode->next = nodePtr->next;
- delete nodePtr;
- cout << "nItem deleted." << endl;
- }
- }
- return 0;
- }
- bool LinkedList::removeValue(int n)
- {
- Node *nodePtr;
- Node *previousNode;
- //initialize nodePtr to head of the list
- nodePtr = head;
- //if the list is empty, do nothing
- if (!head)
- {
- cout << "The list is empty." << endl;
- }
- // determine if the first node is the one
- if (head->val == n)
- {
- nodePtr = head->next;
- delete head;
- head = nodePtr;
- cout << "nItem deleted." << endl;
- }
- else
- {
- //skip nodes whose value does not equal search value
- while (nodePtr != nullptr && nodePtr->val != n)
- {
- previousNode = nodePtr;
- nodePtr = nodePtr->next;
- }
- //if nodePtr is not at end of list, link previous node to the node after nodePtr
- if (nodePtr)
- {
- previousNode = nodePtr;
- previousNode->next = nodePtr->next;
- delete nodePtr;
- cout << "nItem deleted." << endl;
- }
- }
- return 0;
- }
- void LinkedList::destroyList()
- {
- char choice;
- cout << "Are you sure you want to destroy? (Y/N)" << endl;
- do {
- cin >> choice;
- if (choice == 'Y' || choice == 'y')
- {
- LinkedList::~LinkedList();
- return;
- }
- else if (choice == 'N' || choice == 'n')
- {
- return;
- }
- else
- cout << "Invalid entry. Choose again:" << endl;
- }while(choice != 'Y' || choice != 'y' || choice != 'N' || choice != 'n');
- }
- LinkedList::~LinkedList()
- {
- int count = 1;
- Node *nodePtr;
- Node *nextNode;
- // position nodePtr at the head of list
- nodePtr = head;
- // loop to traverse the list
- while(nodePtr != nullptr)
- {
- //set next pointer to the next node after nodePtr
- nextNode = nodePtr->next;
- delete nodePtr;
- cout << "Node " << count << " destroyed..." << endl;
- //set node Ptr to the next node
- nodePtr = nextNode;
- count ++;
- }
- cout << "List destroyed" << endl;
- }
- // Header.h
- #pragma once
- #ifndef HEADER_H
- #define HEADER_H
- // Declare a structure for the list
- struct Node
- {
- int val;
- Node *next;
- };
- class LinkedList
- {
- private:
- int nodeCount;
- Node *head;
- public:
- // Constructor
- LinkedList()
- {
- head = nullptr;
- };
- // Destructor
- ~LinkedList();
- // Linked List Operations
- void insertItem(int, bool);
- int findItem(int);
- //int getNodeCount();
- bool removeNodePosition(int);
- bool removeValue(int);
- void destroyList();
- void display();
- };
- #endif
Add Comment
Please, Sign In to add comment