Challenge 48

Description

The purpose of this challenge is to implement a stack using a linked list as a backing data structure

Requirements

  1. Write the following structs
    struct Location
    {
      string name;
      string address;
    };
    
    struct VisitNode
    {
      Location loc;
      VisitNode * next;
    };
  2. Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node.
  3. Add the following private function. This function will be used to dynamically create new nodes for the linked list.
    VisitNode * create()
    {
      VisitNode * newnode;
      try
      {
        newnode = new VisitNode;
      }
      catch (bad_alloc)
      {
        newnode = NULL;
      }
      return newnode;
    }
    
    // USAGE: To create a new node, use as follows
    // VisitNode * newnode = create();
  4. Create a private function called void deallocate(). This function will traverse the entire list and delete each node. Remember to make a copy of head. Also, realize that if you delete the current node as you’re traversing, you may lose the reference to the next node.
  5. Create a destructor which will call deallocate() and set head to NULL.
  6. Create a public function, bool push(string name, string address). This function will:
    1. create a new node (VisitNode) using the create() function you wrote earlier. The new node will be assigned the values for loc from the function’s parameter values.
    2. add the new node to the front of the linked list (where head points). Remember to deal with two possible scenarios: 1) The list is still empty and this is the first node to be added and 2) The list is not empty
    3. point head to the newly-added node
    4. return true if it’s successful, and false otherwise. The only reason it would be false is if the new node didn’t get created by the create() function (if the new node is NULL)
  7. Create a public function, string pop(). This function will
    1. Identify the node at the front of the list (if not empty)
    2. Copy the value of the node’s loc.name property into a temporary variable
    3. Point head to the next node in the list (or NULL if it’s the last node)
    4. delete the first node (this effectively is the “pop” action)
    5. Return the loc.name value stored in the temporary variable. Return a blank string if the linked list is empty
  8. Create a friend function void show(Stack & S). This will display all the loc.name values of all the nodes on a single line, separated by spaces. Traverse the entire linked list to show the node values.
  9. main() is given below. Use it exactly as is. Note that main uses the Stack class as an abstract data type (ADT). From the perspective of the object browser, it doesn’t even know or care that a linked-list is the mechanism that makes LIFO (last-in, first-out) work.

Sample main()

int main()
{
  Stack browser;

  // simulate a browser history
  browser.push("Google", "//google.com");
  browser.push("Amazon", "//amazon.com");
  browser.push("LinkedIn", "//LinkedIn.com");
  browser.push("Reddit", "//reddit.com");  

  show(browser);   // this should show the entire history

  // simulate clicking Back button
  string top = browser.pop();
  if (top != "")
    cout << endl << "Clicked back from " << top << endl;
  show(browser);

  // simulate clicking Back button  
  top = browser.pop();
  if (top != "")
    cout << endl << "Clicked back from " << top << endl;
  show(browser);

  // simulate clicking Back button
  top = browser.pop();
  if (top != "")
    cout << endl << "Clicked back from " << top << endl;
  show(browser);

  return 0;
}

CATALOG ID: CPP-CHAL00048

Print Requirements