The following form of cascading if/else statements may (or may not, depending on your code) be helpful:
if (cond1) {
code 1
} else if (cond2) {
code 2
} ...
...
} else if (condn {
code n
} else {
code n+1
}
(fill in your appropriate conditions and code). If cond1 is
true, code 1 will be executed, and the rest of the conditional
expression will be skipped. Otherwise cond2 will be checked,
and if it's true, code 2 will be executed, otherwise the next
condition will be checked, and so on. If none of the conditions are
true, code n+1 will be executed.
NEW! As it turns out, an almost complete solution for this problem is found in Assignment 8 in CSci 1302 (Problem 5). However, you must modify the solution in two ways:
import java.util.*;
as the very first line
of your program file.
main
declare and initialize the
random number generator:Random r = new Random();
nextInt
on r:k = r.nextInt(n);
// Queue implementation with arrays
public class CharQueue {
private char [] items = new char [100];
private int first = -1; // in the book this is called 'head'
private int last = 0; // in the book this is called 'tail'
private int length = 0;
// a constructor, need for every class
public CharQueue() {
// do nothing
}
// add the code for isEmpty
// add the code for dequeue
// add the code for enqueue
}
You may use the following stack
implementation as an example. Note that both a stack and a queue work
with characters.
// Stack implementation with arrays
public class CharStack {
private char [] items = new char [100];
private int top = -1;
// a constructor, need for every class
public CharStack() {
// do nothing
}
public boolean isEmpty() {
return top == -1;
}
public char pop() {
if(isEmpty()) {
System.out.println("Stack underflow");
System.exit(0);
}
// if we got here, the stack is not empty
char c = items[top];
top--;
return c;
}
public void push(char c) {
if (top == 99) {
System.out.println("Stack overflow");
System.exit(0);
}
// if we got here, the stack has more room
top++;
items[top]=c;
}
}
Use the file ShowQueue.java
to test the queue. Modify the testing file as needed.
public class MoreWeirdLists {
public static void main(String [] args) {
// creating nodes
Node one = new Node(1);
Node two = new Node(2);
Node three = new Node(3);
Node four = new Node(4);
two.setNext(one);
LinkedList listOne = new LinkedList();
LinkedList listTwo = new LinkedList();
listOne.setFirst(one);
listTwo.setFirst(two);
listOne.addFront(three);
listOne.getFirst().getNext().setNext(four);
// what will be printed?
System.out.println("Printing listOne:");
Node n = listOne.getFirst();
while (n != null) {
System.out.println(n.getData());
n = n.getNext();
}
System.out.println("Printing listTwo:");
n = listTwo.getFirst();
while (n != null) {
System.out.println(n.getData());
n = n.getNext();
}
}
}
Draw the memory picture that shows the final state of the lists and
the nodes. What would be printed by the two loops according to your
memory picture? Run the program and check the results. Redo the
picture if needed.
public int getData()
public void setNext(DoubleNode dn)
public DoubleNode getNext()
public void setPrev(DoubleNode dn)
public DoubleNode getPrev()
public boolean isEmpty()
public void setFirst(DoubleNode dn)
public DoubleNode getFirst()
public void setLast(DoubleNode dn)
public DoubleNode getLast()
(there is no setLast())
public void addFront(DoubleNode dn)
public void addBack(DoubleNode dn)
Make sure that your implementation creates a new node in every method that takes a node as a parameter. Returning a node from a method is fine.