Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <string>
- #include <iostream>
- using namespace std;
- // ------ List Node Class ------ //
- class ListNode
- {
- public:
- string cityName;
- double x;
- double y;
- ListNode *next;
- // Constructor
- ListNode (string n, double tx, double ty)
- {
- cityName = n;
- x = tx;
- y = ty;
- next = 0;
- }
- };
- // ------ Linked List Class ------ //
- class LinkedList
- {
- private:
- ListNode *head;
- public:
- // Constructor
- LinkedList()
- { head = 0; }
- // Destructor
- ~LinkedList();
- //Operations
- void appendNode(string, double, double);
- void deleteNode(string);
- void deleteNodeX(double);
- void deleteNodeY(double);
- void displayList() const;
- void searchAndDisplayName(string);
- void searchAndDisplayX(double);
- void searchAndDisplayY(double);
- void calcAndDisplay(string, int);
- };
- void LinkedList::appendNode(string name, double x, double y)
- {
- ListNode* newNode; // To point to new node
- ListNode* nodePtr; // To traverse List
- // allocate new node
- newNode = new ListNode(name, x, y);
- // If no head, head is the newNode
- // else traverse the list to find the end and append newNode
- if (!head)
- {
- head = newNode;
- cout << "Record inserted successfully.\n" << endl;
- }
- else
- {
- nodePtr = head;
- cout << nodePtr;
- while (nodePtr->next)
- {
- if (nodePtr->cityName == name)
- {
- cout << "No need to insert again, as this record exists in the existing data set.\n" << endl;
- return;
- }
- nodePtr = nodePtr->next;
- }
- if (nodePtr->cityName == name) {
- {
- cout << "No need to insert again, as this record exists in the existing data set.\n" << endl;
- return;
- }
- }
- else if (!nodePtr->next)
- {
- nodePtr->next = newNode;
- cout << "Record inserted successfully.\n" << endl;
- }
- }
- }
- void LinkedList::displayList() const
- {
- ListNode *nodePtr; // to traverse List
- nodePtr = head; // set position to beginning of list
- if (!head)
- cout << "List is empty, nothing to display.\n" << endl;
- return;
- // Traverse list while displaying information in each node.
- while (nodePtr)
- {
- cout << nodePtr->cityName << ", (" << nodePtr->x << ", " << nodePtr->y << ")" << endl;
- nodePtr = nodePtr->next;
- }
- }
- void LinkedList::deleteNode(string searchedName)
- {
- ListNode *nodePtr; // To traverse list
- ListNode *previousNode; // To point to previous node
- //if empty do nothing
- if(!head)
- {
- cout << "Nothing to delete!\n" << endl;
- return;
- }
- // Check first node
- if (head->cityName == searchedName)
- {
- nodePtr = head->next;
- delete head;
- head = nodePtr;
- cout << "Record deleted successfully.\n" << endl;
- }
- else
- {
- nodePtr = head;
- // check all values
- while (nodePtr != NULL && nodePtr->cityName != searchedName)
- {
- previousNode = nodePtr;
- nodePtr = nodePtr->next;
- }
- // if not at the end of the list, link previous and next nodes, delete current.
- if (nodePtr)
- {
- previousNode->next = nodePtr->next;
- delete nodePtr;
- cout << "Record deleted successfully.\n" << endl;
- }
- else
- {
- cout << "Record does not exist!" << endl;
- }
- }
- }
- void LinkedList::deleteNodeX(double coord)
- {
- ListNode *nodePtr; // To traverse list
- ListNode *previousNode; // To point to previous node
- //if empty do nothing
- if(!head)
- {
- cout << "Nothing to delete!\n" << endl;
- return;
- }
- // Check first node
- if (head->x == coord || head->y == coord)
- {
- nodePtr = head->next;
- delete head;
- head = nodePtr;
- cout << "Record deleted successfully.\n" << endl;
- }
- else
- {
- nodePtr = head;
- // check all values x and y
- while (nodePtr != NULL && nodePtr->x != coord)
- {
- previousNode = nodePtr;
- nodePtr = nodePtr->next;
- }
- // if not at the end of the list, link previous and next nodes, delete current.
- if (nodePtr)
- {
- previousNode->next = nodePtr->next;
- delete nodePtr;
- cout << "Record deleted successfully.\n" << endl;
- }
- else
- {
- cout << "Record does not exist!" << endl;
- }
- }
- }
- void LinkedList::deleteNodeY(double coord)
- {
- ListNode *nodePtr; // To traverse list
- ListNode *previousNode; // To point to previous node
- //if empty do nothing
- if(!head)
- {
- cout << "Nothing to delete!\n" << endl;
- return;
- }
- // Check first node
- if (head->x == coord || head->y == coord)
- {
- nodePtr = head->next;
- delete head;
- head = nodePtr;
- cout << "Record deleted successfully.\n" << endl;
- }
- else
- {
- nodePtr = head;
- // check all values x and y
- while (nodePtr != NULL && nodePtr->y != coord)
- {
- previousNode = nodePtr;
- nodePtr = nodePtr->next;
- }
- // if not at the end of the list, link previous and next nodes, delete current.
- if (nodePtr)
- {
- previousNode->next = nodePtr->next;
- delete nodePtr;
- cout << "Record deleted successfully.\n" << endl;
- }
- else
- {
- cout << "Record does not exist!" << endl;
- }
- }
- }
- void LinkedList::searchAndDisplayName(string searchedName)
- {
- ListNode *nodePtr; // To traverse list
- ListNode *previousNode; // To point to previous node
- //if empty do nothing
- if(!head)
- {
- cout << "Nothing to display!\n" << endl;
- return;
- }
- // Check first node
- if (head->cityName == searchedName)
- {
- cout << head->cityName << ", (" << head->x << ", " << head->y << ")" << endl;
- }
- else
- {
- nodePtr = head;
- // check all values
- while (nodePtr != NULL && nodePtr->cityName != searchedName)
- {
- previousNode = nodePtr;
- nodePtr = nodePtr->next;
- }
- // if not at the end of the list, link previous and next nodes, display current.
- if (nodePtr)
- {
- cout << nodePtr->cityName << ", (" << nodePtr->x << ", " << nodePtr->y << ")" << endl;
- }
- else
- {
- cout << "Record does not exist!" << endl;
- }
- }
- }
- void LinkedList::searchAndDisplayX(double coord)
- {
- ListNode *nodePtr; // To traverse list
- ListNode *previousNode; // To point to previous node
- //if empty do nothing
- if(!head)
- {
- cout << "Nothing to display!\n" << endl;
- return;
- }
- // Check first node
- if (head->x == coord)
- {
- cout << head->cityName << ", (" << head->x << ", " << head->y << ")" << endl;
- }
- else
- {
- nodePtr = head;
- // check all values x and y
- while (nodePtr != NULL && nodePtr->x != coord)
- {
- previousNode = nodePtr;
- nodePtr = nodePtr->next;
- }
- // if not at the end of the list, display current.
- if (nodePtr)
- {
- cout << nodePtr->cityName << ", (" << nodePtr->x << ", " << nodePtr->y << ")" << endl;
- }
- else
- {
- cout << "Record does not exist!" << endl;
- }
- }
- }
- void LinkedList::searchAndDisplayY(double coord)
- {
- ListNode *nodePtr; // To traverse list
- ListNode *previousNode; // To point to previous node
- //if empty do nothing
- if(!head)
- {
- cout << "Nothing to display!\n" << endl;
- return;
- }
- // Check first node
- if (head->y == coord)
- {
- cout << head->cityName << ", (" << head->x << ", " << head->y << ")" << endl;
- }
- else
- {
- nodePtr = head;
- // check all values x and y
- while (nodePtr != NULL && nodePtr->y != coord)
- {
- previousNode = nodePtr;
- nodePtr = nodePtr->next;
- }
- // if not at the end of the list, display current.
- if (nodePtr)
- {
- cout << nodePtr->cityName << ", (" << nodePtr->x << ", " << nodePtr->y << ")" << endl;
- }
- else
- {
- cout << "Record does not exist!" << endl;
- }
- }
- }
- void LinkedList::calcAndDisplay(string name, int distance)
- {
- //To be implemented
- }
- LinkedList::~LinkedList()
- {
- ListNode *nodePtr; // To traverse the list
- ListNode *nextNode;// To point to next node
- //start at head
- nodePtr = head;
- //while not at the end of the list
- while (nodePtr != NULL)
- {
- nextNode = nodePtr->next;
- delete nodePtr;
- nodePtr = nextNode;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment