CSC300 Apr23

slide version

single file version

Contents

  1. Topics
  2. Stack
  3. Stack Application 1 (hw4)
  4. 1.3.4 Details
  5. How to read input one character at a time
  6. Example
  7. Stack Application 2
  8. Queue
  9. Queue Application
  10. 1.3.3
  11. 1.3.13
  12. 1.3.6
  13. 1.3.12

Topics[1] [top]

  1. Midterm
  2. Homework 3
  3. Stack
  4. Queue
  5. Stack Application
  6. Queue Application
  7. 1.3.3
  8. 1.3.6
  9. 1.3.12
  10. 1.3.13

Stack[2] [top]

Stack<E>
(private members)
boolean empty()
void push(E x)
E pop()
E peek()
Iterator<E> iterator()

Stack Application 1 (hw4)[3] [top]

1.3.4 Write a stack client Parentheses that reads in a text stream from standard input and uses a stack to determine whether its parentheses are properly balanced. For example, your program should print true for [()]{}{[()()]()} and false for [(]).

1.3.4 Details[4] [top]

1.3.4 Write a stack client Parentheses that reads in a text stream from standard input and uses a stack to determine whether its parentheses are properly balanced. For example, your program should print true for [()]{}{[()()]()} and false for [(]).

Adding word, etc., changes the solution to the problem only slightly.

That is, for input

      [(Hello) World!]{Is this balanced?}{[(one)(two)](three)}
    

the program should return true.

For input

      [(Is this ] balanced?)
    

the program should return false.

How to read input one character at a time[5] [top]

The Scanner class has a method:

      public Scanner useDelimiter(String pattern)
    

The default pattern matches any sequence of whitespace characters (blank ' ', tab '\t', newline '\n', carriage return '\r' and a few more)

So the next() method will skip leading string made up of the delimiter characters and stop at trailing delimiter characters or end of input.

Setting the delimter to the empty string causes the next() method to not skip any leading characters and it will read a string consisting of just one character.

Example[6] [top]

This simple example reads an input text file, in.txt, prints the file to standard output one character at a time and prints the total number of '(' characters and the total number of ')' characters.

    1		
    2	 public static main(String[] args) {
    3	   Scanner in = MyIO.openInput("in.txt");
    4	   in.useDelimiter("");
    5	   int lparenCount = 0;
    6	   int rparenCount = 0;
    7	
    8	   while(in.hasNext()) {
    9	     String s = in.next();
   10	     char ch = s.charAt(0); 
   11	     System.out.print(ch);
   12	     if (ch == '(' ) {
   13		lparenCount++;
   14	     } else if (ch == ')' ) {
   15		rparenCount++;
   16	     }
   17	   }
   18	   System.out.printf("\nThere were %d '(' and %d ')'\n",
   19	     lparenCount, rparenCount);
   20	 }

Stack Application 2[7] [top]

Given a Sudoku puzzle, use a Stack and brute force to solve the puzzle.

At each empty position insert the first value in 1 - 9 that is valid, where valid means not duplicated in the same row or column or small 3 x 3 square. Push that value and position on the stack.

If none of the values 1 - 9 is valid, pop the stack, remove the value at popped position and try the next values.

Continue until all positions have been filled.

Queue[8] [top]

Queue<E>
(private members)
boolean empty()
void add(E x)
E remove()
E peek()
Iterator<E> iterator()

Queue Application[9] [top]

Write the static method readInts in a class MyIO. The method should have a file name as an argument and return an array of all the integers in the file. (That is, implement Sedgewick's readInt library method.)


public class MyIO
{

   public static int[] readInts(String fname)
   {
      ...
   }
}

1.3.3[10] [top]

1.3.3 Suppose that a client performs an intermixed sequence of (stack) push and pop operations. The push operations put the integers 0 through 9 in order onto the stack; the pop operations print out the return values. Which of the following sequence(s) could not occur?

a. 4 3 2 1 0 9 8 7 6 5
b. 4 6 8 7 5 3 2 9 0 1
c. 2 5 6 7 4 8 9 3 1 0
d. 4 3 2 1 0 5 6 7 8 9
e. 1 2 3 4 5 6 9 8 7 0
f. 0 4 6 5 3 8 1 7 2 9
g. 1 4 7 9 8 6 5 3 0 2
h. 2 1 4 3 6 5 8 7 9 0
    

1.3.13[11] [top]

1.3.13 Suppose that a client performs an intermixed sequence of (queue) enqueue and dequeue operations. The enqueue operations put the integers 0 through 9 in order onto the queue; the dequeue operations print out the return value. Which of the following sequence(s) could not occur?

a. 0 1 2 3 4 5 6 7 8 9
b. 4 6 8 7 5 3 2 9 0 1
c. 2 5 6 7 4 8 9 3 1 0
d. 4 3 2 1 0 5 6 7 8 9
    

1.3.6[12] [top]

1.3.6 What does the following code fragment do to the queue q?


Stack<String> stack = new Stack<String>();

while (!q.isEmpty()) {
  stack.push(q.dequeue());
}

while (!stack.isEmpty()) {
  q.enqueue(stack.pop());
}

For example, suppose the elements in the queue are:

(front) "A" "B" "C" "D" "E" (back)
    

1.3.12[13] [top]

1.3.12 Write an iterable Stack client that has a static method copy() that takes a stack of strings as argument and returns a copy of the stack. Note : This ability is a prime example of the value of having an iterator, because it allows development of such functionality without changing the basic API.