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
}
Use the Stack implementation
as an example.
Use the file ShowQueue.java to test the queue. Modify the testing file as needed.
list1
, add some
nodes to it. Then declare a new linked list lists2
and
add some nodes to it which are have the same values as the nodes in
list1.
Important: You have to create copies of the nodes, not share the nodes between the two lists. To make sure that nodes are not shared, change one of the lists, and print out both lists to make sure that the second one hasn't changed. This way of copying (where all the elements that a data structure consists of are copied and not shared) is called a deep copy, as opposed to a shallow copy (where the elements are shared).
public class WeirdLists {
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);
LinkedList listOne = new LinkedList();
LinkedList listTwo = new LinkedList();
// adding elements to list 1
listOne.addFront(one);
listOne.addFront(two);
// add one element to list 2
listTwo.addFront(three);
// changing node one
one.setNext(three);
// changing something in the second list
listTwo.getFirst().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 object diagram 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
diagram if needed. Submit the diagram.
The first class is DoubleNode
which is similar
to the class Node
, but has a reference to the previous
element, as well as the to next one. The DoubleNode class has the
following methods:
DoubleNode
s are
set to null.
public int getData()
public void setNext(DoubleNode dn)
public DoubleNode getNext()
public void setPrev(DoubleNode dn)
public DoubleNode getPrev()
DoubleLinkedList
. The class has the following methods:
public boolean isEmpty()
public DoubleNode getFirst()
returns the reference
to the first DoubleNode in the list. If there is no such node, returns
null. The list doesn't change.
public DoubleNode getLast()
returns the reference
to the last DoubleNode in the list. If there is no such node, returns
null. The list doesn't change.
public void addFront(DoubleNode dn)
Adds a new node
to the front of the list.
public void addBack(DoubleNode dn)
Adds a new node
to the back of the list.
public void removeFront()
Removes the first element
from the list. Doesn't return anything.
public void removeBack()
Removes the last element
from the list. Doesn't return anything.