Challenge 53

Description

The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager.

Requirements

  1. Write the following struct
    struct Window
    {
      string appname;
      Window *next;
      Window *prev;
    };
  2. Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node.
  3. Create private variables Window * current, * dummy. current will keep track of the “current” program. Think of it as the program currently being used by the user in this simulated operating system. head and dummy will always point to the dummy node. (You really could get by using just head or just dummy)
  4. Add the following private function. This function will be used to dynamically create new nodes for the linked list.
    Window * create()
    {
      Window * newnode;
      try
      {
        newnode = new Window;
      }
      catch (bad_alloc)
      {
        newnode = NULL;
      }
      return newnode;
    }
    
    // USAGE: To create a new node, use as follows
    // Window * newnode = create();
  5. 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. Make sure that the dummy node is also deleted.
  6. Create a destructor which will call deallocate() and set head to NULL.
  7. Create a default constructor which will instantiate a dummy node. Set head and dummy to point to this dummy node. Set the appname property of the dummy node to a blank string. Make sure that dummy’s next and prev pointers point to itself when first starting out.
  8. Create a public function bool start_app(string name). This function will create a new Window node and insert the new node to the left of the dummy node. (Remember that after adding the first node, this node’s next and prev will point to the dummy node. Also, next and prev of the dummy node will point to the first node. This only occurs with the addition of the first node). Set current to the node created in this function. (think of current as the last app launched by the user)
  9. Create a public function Window * find_app(string name). This function will perform a traversal searching the queue for a node with appname equal to  name. You can perform the traversal in any direction (going in the direction of next from the dummy node, or going in the direction of prev from the prev node). No matter which direction you choose to traverse to search for the node, you will either find a matching node, in which case you will return the address of that node, or you end up back at the dummy node without finding a match. If you traverse the entire queue without finding the matching node, the function should return a NULL. Remember to skip over the dummy node during any traversals.
  10. Create a public function bool close_app(string name). This function calls find_app() to help find the app’s node. If a matching node is found, delete the node and take it out of the queue. If a matching node is found, return true. Otherwise, return false. Set current to the next node following the app that was just closed. Remember to skip over the dummy node during any traversals — current cannot point to the dummy node. If the queue is empty, then current is NULL.
  11. Create a public function string get_current(). This function will return  appname of current. If current is NULL, it should return a blank string.
  12. Create a public function string next(). This program advances current to the next app in the circular queue. Remember to skip the dummy node during the traversal. If the queue is empty, then current is NULL.
  13. Create a public function string previous(). This program “advances” current to the previous app in the circular queue. Remember to skip the dummy node during the traversal. If the queue is empty, then current is NULL.
  14. main() is given below. Use it exactly as is. Note that main uses the WindowManager class as an abstract data type (ADT). From the perspective of the object winman, it doesn’t even know or care that a linked-list is the mechanism that makes the circular queue work.
  15. In main(), 
    1. Option 1 will prompt the user to enter a string, the app’s name. Call winman.start_app() to simulate running this app.
    2. Option 2 will close the current running app
    3. Option 3 will prompt the user to enter an app name. Use winman.close_app() to find the app, and then close it
    4. After both option 2 and 3, show the name of the new current app by calling the get_current() function (If an app is closed, then the next app becomes the new current app as long as other apps are running).
    5. Option 4 calls winman.next() and shows the current app
    6. Option 5 calls winman.previous() and shows the current app
    7. Option 0 exits your running program.

Sample main()

int main()
{
  WindowManager winman;

  // simulate some apps running
  winman.start_app("Microsoft Word");
  winman.start_app("Firefox");
  winman.start_app("Halo");
  winman.start_app("Calculator");

  // at this point, Calculator is the last app launched
  // and should be the value of current in winman
  int action;
  do
  {
    cout << "1 - Launch new app" << endl;
    cout << "2 - Close current app" << endl;
    cout << "3 - Find app, then close it" << endl;
    cout << "4 - Go to next app" << endl;
    cout << "5 - Go to previous app" << endl;
    cout << "0 - Shutdown" << endl;
    cin >> action;

    // fill in the rest of the necessary code to perform 
    // menu actions
  }
  while (action != 0);

  cout << "Shutting down..." << endl;

  return 0;
}

CATALOG ID: CPP-CHAL00053

Print Requirements