This commit is contained in:
louiscklaw
2025-01-31 19:15:17 +08:00
parent 09adae8c8e
commit 6c60a73f30
1546 changed files with 286918 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
public class ArrayQueue {
public static final int CAPACITY = 1000;
private int capacity;
private Object[] array;
private int front = 0;
private int rear = 0;
public ArrayQueue() {
this(CAPACITY);
}
public ArrayQueue(int cap) {
capacity = cap;
array = new Object[capacity];
}
public int size() {
return (capacity - front + rear) % capacity;
}
public boolean isEmpty() {
return (front == rear);
}
public void enqueue(Object item) throws QueueFullException {
if (size() == capacity - 1) {
throw new QueueFullException();
}
array[rear] = item;
rear = (rear + 1) % capacity;
}
public Object dequeue() throws QueueEmptyException {
if (isEmpty()) {
throw new QueueEmptyException();
}
Object item = array[front];
array[front] = null;
front = (front + 1) % capacity;
return item;
}
public Object front() throws QueueEmptyException {
if (isEmpty()) {
throw new QueueEmptyException();
}
return array[front];
}
public String toString() {
String s = "[ ";
int next = front;
for (int i = 0; i < size(); i++) {
s += array[next] + " ";
next = front + i;
}
return s + " ]";
}
}

View File

@@ -0,0 +1,5 @@
public class EmptyListException extends RuntimeException {
public EmptyListException() {
super("List is empty.");
}
}

View File

@@ -0,0 +1,88 @@
public class LinkedList {
private ListNode head;
private ListNode tail;
private int count;
public LinkedList() {
head = null;
tail = null;
count = 0;
}
public boolean isEmpty() {
return (head==null);
}
public int getCount() {
return count;
}
public void addToHead(Object item) {
count++;
if (isEmpty()) {
head = tail = new ListNode(item);
} else {
head = new ListNode(item, head);
}
}
public void addToTail(Object item) {
count++;
if (isEmpty()) {
head = tail = new ListNode(item);
} else {
tail.next = new ListNode(item);
tail = tail.next;
}
}
public Object removeFromHead() throws EmptyListException {
if (isEmpty()) {
throw new EmptyListException();
}
Object item = head.data;
if (head == tail) // there's only one single node
head = tail = null;
else
head = head.next;
count--;
return item;
}
public Object removeFromTail() throws EmptyListException {
if (isEmpty()) {
throw new EmptyListException();
}
count--;
Object item = tail.data;
if (head == tail) { // there is only one node
head = tail = null;
return item;
}
// search for the second last node
ListNode current = head;
while (current.next != tail)
current = current.next;
// set second last node as new tail
tail = current;
tail.next = null;
return item;
}
public String toString() {
String s = "[ ";
// traverse the list from head towards tail
ListNode current = head;
while (current != null) {
s += current.data + " ";
current = current.next;
}
return s + "]";
}
}

View File

@@ -0,0 +1,42 @@
public class LinkedQueue {
LinkedList qll;
public LinkedQueue() {
qll = new LinkedList();
}
public int size() {
return qll.getCount();
}
public boolean isEmpty() {
return qll.isEmpty();
}
public void enqueue(Object item) throws QueueFullException {
qll.addToTail(item);
}
public Object dequeue() throws QueueEmptyException {
try {
Object item = qll.removeFromHead();
return item;
} catch (EmptyListException e) {
throw new QueueEmptyException();
}
}
public String toString() {
return qll.toString();
}
public Object front() throws QueueEmptyException {
try {
Object item = qll.removeFromHead();
qll.addToHead(item);
return item;
} catch (EmptyListException e) {
throw new QueueEmptyException();
}
}
}

View File

@@ -0,0 +1,17 @@
public class ListNode {
public Object data; // set to public for implementation convenience
public ListNode next;
public ListNode(Object data) {
this.data = data;
this.next = null;
}
public ListNode(Object data, ListNode next) {
this.data = data;
this.next = next;
}
}

View File

@@ -0,0 +1,7 @@
public interface Queue {
public abstract boolean isEmpty();
public abstract int size();
public abstract Object front() throws QueueEmptyException;
public abstract void enqueue(Object item) throws QueueFullException;
public abstract Object dequeue() throws QueueEmptyException;
}

View File

@@ -0,0 +1,5 @@
public class QueueEmptyException extends Exception{
}

View File

@@ -0,0 +1,5 @@
public class QueueFullException extends Exception{
}

View File

@@ -0,0 +1,17 @@
### Q3
In topic “3.1 Linked Lists”, we have discussed the importance of using a tail for referencing the last node in a linked list. The purpose of this exercise is to have an empirical study on the impact of using the tail reference on a linked list.
Execute the program multiple times with different value for <num>. Record the execution
time as reported by the program in a spreadsheet.
(b) Copy your programs in Q3(a) to a new folder. Q3(b). Modify the Java program LinkedList.java so that the linked list does not contain the tail reference. Needless to say, when the last node in the linked list is wanted, you need to traverse the list from the head towards its end until the last node is reached. Execute LinkedQueueTiming again using the same set of <num> values as in Q3(a). Record the execution time as reported by the program in the spreadsheet.
| n | num | with tail Q3(a) | average time(with tail) | waithout tail Q3(b) | average time(without tail) |
| --- | --- | --- | --- | --- | --- |
| 1 | 10 | 266800 | 276500 | 272300 | 294200 |
| 2 | 100 | 281600 | 288650 | 325500 | 345600 |
| 3 | 1000| 669900 | 671020 | 1767600 | 1678600 |
| 4 | 10000 | 1575200 | 1595300 | 90549600 | 101132100 |
| 5 | 100000 | 5840900 | 6020100 | 9229402000 | 9103203100 |
| 6 | 1000000 | 24357800 | 26236800 | 1500203112900 | 1612141023950 |