Guest User

broken?

a guest
Feb 13th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.70 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. // ------ List Node Class ------ //
  9.  
  10. class ListNode
  11. {
  12. public:
  13.     string cityName;
  14.     double x;
  15.     double y;
  16.     ListNode *next;
  17.  
  18.     // Constructor
  19.     ListNode (string n, double tx, double ty)
  20.     {
  21.         cityName = n;
  22.         x = tx;
  23.         y = ty;
  24.         next = 0;
  25.     }
  26. };
  27.  
  28. // ------ Linked List Class ------ //
  29.  
  30. class LinkedList
  31. {
  32. private:
  33.     ListNode *head;
  34. public:
  35.     // Constructor
  36.     LinkedList()
  37.     { head = 0; }
  38.  
  39.     // Destructor
  40.     ~LinkedList();
  41.  
  42.     //Operations
  43.     void appendNode(string, double, double);
  44.     void deleteNode(string);
  45.     void deleteNodeX(double);
  46.     void deleteNodeY(double);
  47.     void displayList() const;
  48.     void searchAndDisplayName(string);
  49.     void searchAndDisplayX(double);
  50.     void searchAndDisplayY(double);
  51.     void calcAndDisplay(string, int);
  52. };
  53.  
  54. void LinkedList::appendNode(string name, double x, double y)
  55. {
  56.     ListNode* newNode; // To point to new node
  57.     ListNode* nodePtr; // To traverse List
  58.  
  59.     // allocate new node
  60.     newNode = new ListNode(name, x, y);
  61.  
  62.     // If no head, head is the newNode
  63.     // else traverse the list to find the end and append newNode
  64.     if (!head)
  65.     {
  66.         head = newNode;
  67.         cout << "Record inserted successfully.\n" << endl;
  68.     }
  69.     else
  70.     {
  71.         nodePtr = head;
  72.  
  73.         cout << nodePtr;
  74.         while (nodePtr->next)
  75.         {
  76.             if (nodePtr->cityName == name)
  77.             {
  78.                 cout << "No need to insert again, as this record exists in the existing data set.\n" << endl;
  79.                 return;
  80.             }
  81.             nodePtr = nodePtr->next;
  82.         }
  83.         if (nodePtr->cityName == name)          {
  84.             {
  85.                 cout << "No need to insert again, as this record exists in the existing data set.\n" << endl;
  86.                 return;
  87.             }
  88.         }
  89.         else if (!nodePtr->next)
  90.         {
  91.             nodePtr->next = newNode;
  92.             cout << "Record inserted successfully.\n" << endl;
  93.         }
  94.     }
  95. }
  96.  
  97. void LinkedList::displayList() const
  98. {
  99.     ListNode *nodePtr; // to traverse List
  100.  
  101.     nodePtr = head; // set position to beginning of list
  102.     if (!head)
  103.         cout << "List is empty, nothing to display.\n" << endl;
  104.     return;
  105.     // Traverse list while displaying information in each node.
  106.     while (nodePtr)
  107.     {
  108.         cout << nodePtr->cityName << ", (" << nodePtr->x << ", " << nodePtr->y << ")" << endl;
  109.         nodePtr = nodePtr->next;
  110.     }
  111. }
  112.  
  113. void LinkedList::deleteNode(string searchedName)
  114. {
  115.     ListNode *nodePtr; // To traverse list
  116.     ListNode *previousNode; // To point to previous node
  117.  
  118.     //if empty do nothing
  119.     if(!head)
  120.     {
  121.         cout << "Nothing to delete!\n" << endl;
  122.         return;
  123.     }
  124.     // Check first node
  125.     if (head->cityName == searchedName)
  126.     {
  127.         nodePtr = head->next;
  128.         delete head;
  129.         head = nodePtr;
  130.         cout << "Record deleted successfully.\n" << endl;
  131.     }
  132.     else
  133.     {
  134.         nodePtr = head;
  135.  
  136.         // check all values
  137.         while (nodePtr != NULL && nodePtr->cityName != searchedName)
  138.         {
  139.             previousNode = nodePtr;
  140.             nodePtr = nodePtr->next;
  141.         }
  142.  
  143.         // if not at the end of the list, link previous and next nodes, delete current.
  144.         if (nodePtr)
  145.         {
  146.             previousNode->next = nodePtr->next;
  147.             delete nodePtr;
  148.             cout << "Record deleted successfully.\n" << endl;
  149.         }
  150.         else
  151.         {
  152.             cout << "Record does not exist!" << endl;
  153.         }
  154.     }
  155. }
  156.  
  157. void LinkedList::deleteNodeX(double coord)
  158. {
  159.     ListNode *nodePtr; // To traverse list
  160.     ListNode *previousNode; // To point to previous node
  161.  
  162.     //if empty do nothing
  163.     if(!head)
  164.     {
  165.         cout << "Nothing to delete!\n" << endl;
  166.         return;
  167.     }
  168.  
  169.     // Check first node
  170.     if (head->x == coord || head->y == coord)
  171.     {
  172.         nodePtr = head->next;
  173.         delete head;
  174.         head = nodePtr;
  175.         cout << "Record deleted successfully.\n" << endl;
  176.     }
  177.     else
  178.     {
  179.         nodePtr = head;
  180.  
  181.         // check all values x and y
  182.         while (nodePtr != NULL && nodePtr->x != coord)
  183.         {
  184.             previousNode = nodePtr;
  185.             nodePtr = nodePtr->next;
  186.         }
  187.  
  188.         // if not at the end of the list, link previous and next nodes, delete current.
  189.         if (nodePtr)
  190.         {
  191.             previousNode->next = nodePtr->next;
  192.             delete nodePtr;
  193.             cout << "Record deleted successfully.\n" << endl;
  194.         }
  195.         else
  196.         {
  197.             cout << "Record does not exist!" << endl;
  198.         }
  199.     }
  200. }
  201.  
  202. void LinkedList::deleteNodeY(double coord)
  203. {
  204.     ListNode *nodePtr; // To traverse list
  205.     ListNode *previousNode; // To point to previous node
  206.  
  207.     //if empty do nothing
  208.     if(!head)
  209.     {
  210.         cout << "Nothing to delete!\n" << endl;
  211.         return;
  212.     }
  213.  
  214.     // Check first node
  215.     if (head->x == coord || head->y == coord)
  216.     {
  217.         nodePtr = head->next;
  218.         delete head;
  219.         head = nodePtr;
  220.         cout << "Record deleted successfully.\n" << endl;
  221.     }
  222.     else
  223.     {
  224.         nodePtr = head;
  225.  
  226.         // check all values x and y
  227.         while (nodePtr != NULL && nodePtr->y != coord)
  228.         {
  229.             previousNode = nodePtr;
  230.             nodePtr = nodePtr->next;
  231.         }
  232.  
  233.         // if not at the end of the list, link previous and next nodes, delete current.
  234.         if (nodePtr)
  235.         {
  236.             previousNode->next = nodePtr->next;
  237.             delete nodePtr;
  238.             cout << "Record deleted successfully.\n" << endl;
  239.         }
  240.         else
  241.         {
  242.             cout << "Record does not exist!" << endl;
  243.         }
  244.     }
  245. }
  246.  
  247. void LinkedList::searchAndDisplayName(string searchedName)
  248. {
  249.     ListNode *nodePtr; // To traverse list
  250.     ListNode *previousNode; // To point to previous node
  251.  
  252.     //if empty do nothing
  253.     if(!head)
  254.     {
  255.         cout << "Nothing to display!\n" << endl;
  256.         return;
  257.     }
  258.  
  259.     // Check first node
  260.     if (head->cityName == searchedName)
  261.     {
  262.         cout << head->cityName << ", (" << head->x << ", " << head->y << ")" << endl;
  263.     }
  264.     else
  265.     {
  266.         nodePtr = head;
  267.  
  268.         // check all values
  269.         while (nodePtr != NULL && nodePtr->cityName != searchedName)
  270.         {
  271.             previousNode = nodePtr;
  272.             nodePtr = nodePtr->next;
  273.         }
  274.  
  275.         // if not at the end of the list, link previous and next nodes, display current.
  276.         if (nodePtr)
  277.         {
  278.             cout << nodePtr->cityName << ", (" << nodePtr->x << ", " << nodePtr->y << ")" << endl;
  279.         }
  280.         else
  281.         {
  282.             cout << "Record does not exist!" << endl;
  283.         }
  284.     }
  285. }
  286.  
  287. void LinkedList::searchAndDisplayX(double coord)
  288. {
  289.     ListNode *nodePtr; // To traverse list
  290.     ListNode *previousNode; // To point to previous node
  291.  
  292.     //if empty do nothing
  293.     if(!head)
  294.     {
  295.         cout << "Nothing to display!\n" << endl;
  296.         return;
  297.     }
  298.  
  299.     // Check first node
  300.     if (head->x == coord)
  301.     {
  302.         cout << head->cityName << ", (" << head->x << ", " << head->y << ")" << endl;
  303.     }
  304.     else
  305.     {
  306.         nodePtr = head;
  307.  
  308.         // check all values x and y
  309.         while (nodePtr != NULL && nodePtr->x != coord)
  310.         {
  311.             previousNode = nodePtr;
  312.             nodePtr = nodePtr->next;
  313.         }
  314.  
  315.         // if not at the end of the list, display current.
  316.         if (nodePtr)
  317.         {
  318.             cout << nodePtr->cityName << ", (" << nodePtr->x << ", " << nodePtr->y << ")" << endl;
  319.         }
  320.         else
  321.         {
  322.             cout << "Record does not exist!" << endl;
  323.         }
  324.     }
  325. }
  326.  
  327. void LinkedList::searchAndDisplayY(double coord)
  328. {
  329.     ListNode *nodePtr; // To traverse list
  330.     ListNode *previousNode; // To point to previous node
  331.  
  332.     //if empty do nothing
  333.     if(!head)
  334.     {
  335.         cout << "Nothing to display!\n" << endl;
  336.         return;
  337.     }
  338.  
  339.     // Check first node
  340.     if (head->y == coord)
  341.     {
  342.         cout << head->cityName << ", (" << head->x << ", " << head->y << ")" << endl;
  343.     }
  344.     else
  345.     {
  346.         nodePtr = head;
  347.  
  348.         // check all values x and y
  349.         while (nodePtr != NULL && nodePtr->y != coord)
  350.         {
  351.             previousNode = nodePtr;
  352.             nodePtr = nodePtr->next;
  353.         }
  354.  
  355.         // if not at the end of the list, display current.
  356.         if (nodePtr)
  357.         {
  358.             cout << nodePtr->cityName << ", (" << nodePtr->x << ", " << nodePtr->y << ")" << endl;
  359.         }
  360.         else
  361.         {
  362.             cout << "Record does not exist!" << endl;
  363.         }
  364.     }
  365. }
  366.  
  367.  
  368. void LinkedList::calcAndDisplay(string name, int distance)
  369. {
  370.     //To be implemented
  371. }
  372.  
  373. LinkedList::~LinkedList()
  374. {
  375.     ListNode *nodePtr; // To traverse the list
  376.     ListNode *nextNode;// To point to next node
  377.  
  378.     //start at head
  379.     nodePtr = head;
  380.  
  381.     //while not at the end of the list
  382.     while (nodePtr != NULL)
  383.     {
  384.         nextNode = nodePtr->next;
  385.         delete nodePtr;
  386.         nodePtr = nextNode;
  387.     }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment