Java II Chapter 21
A queue based on a linked list uses the following code class Node{ String element; Node next; Node (String el, Node n) { element = el; next = n; } } Node front = null, rear = null; What is the right code for void add(String x) operation? Such an operation adds x to the queue
if (rear != null) { rear.next = new Node(x, null); rear = rear.next; } else { rear = new Node(x, null); front = rear;
A stack based on a linked list is based on the following code class Node{ String element; Node next; Node(String el, Node n) { element = el; next = n; } } Node top = null; The code for implementing the String peek() operation is
if (top != null) return top.element else throw new RuntimeException("Empty Stack");
A stack based on a linked list is based on the following code class Node{ String element; Node next; Node(String el, Node n) { element = el; next = n; } } Node top = null; The code for implementing the String pop() operation is
if (top != null) { String el = top.element; top = top.next; return el; } else throw new RuntimeException("Empty Stack");
Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = 0; a method that implements the String peek() operation can be written as
if (top == 0) throw new RuntimeException("Underflow"); else return s[top-1];
Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = 0; a method that implements the String pop() operation can be written as
if (top == 0) throw new RuntimeException("Underflow"); top--; String temp = s[top]; s[top] = null; return temp;
Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = -1; //Note top == -1 indicates stack is empty a method that implements a void push(String x) operation can be written as
if (top == s.length-1) throw new RuntimeException("Overflow"); top ++; s[top] = x;
Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = -1; // Note top == -1 indicates stack is empty a method that implements a String peek() operation can be written as
if (top > -1) return s[top]; else throw new RuntimeException("Empty Stack");
A queue is a container that allows elements to be stored and removed
in a first-in-first-out fashion
A stack is a container that allows elements to be stored and removed
in a last-in-first-out fashion
In an implementation of a stack based on a singly-linked list, it is most efficient to add a new item so tha
the new item has the lowest index of all items in the list
In a list implementation of a queue, the end of the list at which elements are added is called
the rear of the queue
In a list implementation of a stack, the end of the list at which elements are added and removed is called
the top of the stack
In an array-based implementation of a stack, an operation that needs to add a new element to the stack may not be able to complete because the array is full. In this case, the failed operation should
throw some appropriately defined exception
A stack based on a linked list is based on the following code class Node{ String element; Node next; Node(String el, Node n) { element = el; next = n; } } Node top = null; The code for implementing the operation void push(String x) can be written as
top = new Node(x, top);
Which of the following operations is not a stack operation?
All of these: that is, none of the above are stack operations
If pop is called on an empty stack, it will not return until the user puts something on the stack.
False
In a linked implementation of a queue, the references front and rear can only be equal if the queue is empty
False
The stack push operation
adds a single item to the stack
The stack class provided by the Java Collections Framework
cannot be used to instantiate a stack of int, or of any primitive type
The stack empty operation
checks to see if there is at least one item on the stack
The JCF Stack class is used to instantiate a stack: Stack<Integer> intStack = new Stack<Integer>(); The statements int k = 77; intStack.push(k*k); use the primitive type int instead of the wrapper type Integer. These statements
compile and execute correctly
The operation for removing an item from a queue is called
dequeue
The stack pull operation
does not exist: There is no such stack operation
The operation for adding an item to a queue is called
enqueue
The stack pop operation
extracts one element from the stack and returns it
A queue based on a linked list uses the following code class Node{ String element; Node next; Node (String el, Node n) { element = el; next = n; } } Node front, rear; What is the right code for a constructor for such a linked list class?
front = null; rear = null;
A queue based on a linked list uses the following code class Node{ String element; Node next; Node (String el, Node n) { element = el; next = n; } } Node front = null, rear = null; What is the right code for String remove() operation? Such an operation removes and returns an element from the queue
if (front == null) throw new RuntimeException("Empty"); String temp = front.element; front = front.next; if (front == null) rear = null; return temp;
Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = 0; a method for adding an item x to the stack can be written as
if (top < s.length) { s[top] = x; top ++; } else throw new RuntimeException("Overflow");
Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = -1; // Note top == -1 indicates stack is empty a method that implements a String pop() operation can be written as
if (top == -1) throw new RuntimeException("Empty Stack"); String temp = s[top]; s[top] = null; top--;
Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = 0; The boolean method to check for an empty stack can be written as:
if (top == 0) return true; else return false;
If the stack method push is called on an empty stack, __________.
it adds its argument to the stack
In a queue implementation that uses an array of fixed size,
it is necessary to use the array as a circular buffer
A stream of cars going through a toll booth is an example of a
queue
A queue based on a linked list uses the following code class Node{ String element; Node next; Node (String el, Node n) { element = el; next = n; } } Node front = null, rear = null; What is the right code for the boolean empty() method?
return front == null;
A stack based on a linked list is based on the following code class Node{ String element; Node next; Node(String el, Node n) { element = el; next = n; } } Node top = null; The code for testing whether the stack is empty is
return top == null;
The stack peek operation
returns the item at the top of the stack, but does not remove it
Compilers of modern programming languages support method calls and returns with an internal
stack
The concept of seniority, which some employers use to hire and fire workers is a __________
stack
A queue invariant is a condition
that is required to be true after the execution of each queue class method
In a list implementation of a queue, the end of the list from which elements are removed is called
the front of the queue