Challenge 49

Description

The purpose of this challenge is to implement a stack using an array as a backing data structure

Requirements

  1. Write the following struct
    struct Location
    {
      string name;
      string url;
    };
  2. Create a class called Stack. In this class,
    1. create a private variable Location nodes[100]
    2. create a private variable int count
  3. Create a public function, bool push(string name, string url). This function will:
    1. Copy the values of all elements of the nodes array to the next corresponding node greater in the sequence (move the contents of node1 to node2, node2 to node3, nodeto noden+1). Remember that you have to do this starting from the last node in the list that contains actual data. By shifting all the node contents to the right, you effectively leave the first node “empty”, or rather usable for insertion of new content.
    2. Now that the first node is “empty” fill its name and url values from the function’s parameter values.
    3. Don’t forget to update the count variable
    4. Return true if count < 100. Otherwise, return false.
  4. Create a public function, string pop(). This function will:
    1. Determine if the stack is “empty”. If it is empty, return a blank string
    2. If the array contains node data, copy the value of name from the top node to a temporary variable
    3. Copy each successive node to the node that precedes it (copy node3 to node2, node2 to node1, noden to noden-1)
    4. Don’t forget to update count. This effectively performs the “pop” action
  5. Create a friend function void show(Stack & S). This will display all the name values of the nodes on a single line, separated by spaces. Traverse the entire array to show the node values.
  6. 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 an array 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-CHAL00049

Print Requirements