SI 204 Spring 2017 / HWs


Homework 35: Linked list in C++

Name: _____________________________________________ Alpha: ___________________

Describe help received: ________________________________________________________

  • Due before class on Friday, April 21
  • This homework contains code to be submitted electronically. Put your code in a folder called hw35 and submit using the 204sub command.
  • This is a written homework - be sure to turn in a hard-copy of your completed assignment before the deadline. Use the codeprint command to print out your code and turn that in as well.

Assignment

  1. Circle one to indicate how you did for the reading assignment from Homework 33 before class on Monday:

    How carefully did you complete the reading? (Circle one)

    Not at all
    Skimmed it
    Read some
    Read all
  2. Circle one to indicate how you did for the reading assignment from Homework 34 before class on Wednesday:

    How carefully did you complete the reading? (Circle one)

    Not at all
    Skimmed it
    Read some
    Read all
  3. Required reading for this homework: Section 5 (structs and classes) from the Unit 10 notes.
    Circle below to indicate how you did for this reading assignment:

    How carefully did you complete the reading? (Circle one)

    Not at all
    Skimmed it
    Read some
    Read all
  4. Given the following C++ declarations:
    void wombat(int x, double y);
    int wombat(int x);
    int wombat(char* s);
    
    int a;
    double b;
    char c;
    char d[128];
    fill in the following table with the type (only) of each expression. Write ERROR for if the expression would be a compiler or run-time error.
    expressiontype (or ERROR)
    a
    wombat(a)
    wombat(a,b)
    wombat(b)
    a < b
    wombat(d) == 10
    wombat(a,b) == 10
    wombat(wombat(&c))
  5. Note: This is the exact same assignment as from Homework 32 from last week, except that now you need to do it in C++, using new and delete for example.

    Pierce the Painter likes to paint many layers of colors on top of each other, then strip them off and remember what was underneath. Write a program paint.cpp to help Pierce keep track of these colors.
    Your program will read a series of two kinds of commands:

    • paint color
      Adds a layer of color on top of the current color.
    • strip
      Removes the topmost layer of color, revealing whatever was just underneath.
    Before each command, your program should report what color is on the wall by saying "The top color is X." When there are no colors, like at the beginning of the program, say "The canvas is blank."
    When Pierce tries to strip from a blank canvas, quit the program.

    Of course, you will want to create a linked list to help you! Each node in your linked list should store a single string, for the name of a color. When Pierce paints, that means adding to the front of the linked list. When Pierce wants to strip, that means removing the first node in the list.

    Examples

    roche@ubuntu$ ./paint
    roche@ubuntu$ ./paint
    The canvas is blank.
    strip
    roche@ubuntu$ ./paint
    The canvas is blank.
    paint red
    The top color is red.
    strip
    The canvas is blank.
    strip
    roche@ubuntu$ ./paint
    The canvas is blank.
    paint blue
    The top color is blue.
    paint blue
    The top color is blue.
    paint green
    The top color is green.
    strip
    The top color is blue.
    paint orange
    The top color is orange.
    strip
    The top color is blue.
    strip
    The top color is blue.
    paint purple
    The top color is purple.
    paint purple
    The top color is purple.
    strip
    The top color is purple.
    strip
    The top color is blue.
    strip
    The canvas is blank.
    paint white
    The top color is white.
    strip
    The canvas is blank.
    strip