all in java
DLL class
import java.util.*;
public class DLL implements Iterable {
private DLLNode head, tail;
public DLL() { head = tail = null; }
public boolean isEmpty() { return head == null; }
public void clear() { head = tail = null; }
public void addToHead(T el) { if (head != null) { head = new DLLNode(el, head, null); head.next.prev = head; } else { head = tail = new DLLNode(el); } }
public void addToTail(T el) { if (tail != null) { tail = new DLLNode(el, null, tail); tail.prev.next = tail; } else { head = tail = new DLLNode(el); } }
public T deleteFromHead() { if (isEmpty()) { return null; } T el = head.info; if (head == tail) // if only one node on the list; { head = tail = null; } else { // if more than one node in the list; head = head.next; head.prev = null; } return el; }
public T deleteFromTail() { if (isEmpty()) { return null; } T el = tail.info; if (head == tail) // if only one node on the list; { head = tail = null; } else { // if more than one node in the list; tail = tail.prev; tail.next = null; } return el; }
public T delete(T el) { if (isEmpty()) { return null; } else if (head.info.equals(el)) { return deleteFromHead(); } else if (tail.info.equals(el)) { return deleteFromTail(); } else { DLLNode tmp; for (tmp = head.next; tmp != null && !tmp.info.equals(el); tmp = tmp.next); if (tmp != null) { tmp.prev.next = tmp.next; tmp.next.prev = tmp.prev; return tmp.info; } else { return null; } } }
public void printAll() { for (DLLNode tmp = head; tmp != null; tmp = tmp.next) { System.out.print(tmp.info + " "); } }
public T find(T el) { DLLNode tmp; for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next); if (tmp == null) { return null; } else { return tmp.info; } }
public T getFirst() { if (head != null) { return head.info; } else { return null; } }
public T getLast() { if (tail != null) { return tail.info; } else { return null; } }
public String toString() { String s = "["; for (DLLNode tmp = head; tmp != null; tmp = tmp.next) { s += tmp.info + " "; } return s + "]"; }
public Iterator iterator() { return new DLLIterator(); }
private class DLLIterator implements Iterator {
DLLNode tmp = head;
public boolean hasNext() { return tmp != null; }
public T next() { T info = tmp.info; tmp = tmp.next; return info; }
public void remove() { // not implemented } }
// node of generic doubly linked list class class DLLNode {
public T info; public DLLNode next, prev;
public DLLNode() { next = null; prev = null; }
public DLLNode(T el) { info = el; next = null; prev = null; }
public DLLNode(T el, DLLNode n, DLLNode p) { info = el; next = n; prev = p; } } }
CS 205 Lab 5 - Queue Objectives: Learn How to Implement Queue using Doubly Linked List Learn How to Use Queue Examples: 1. Class Array Queue shows how queue may be implemented using array (as discussed in the lecture). Class TestArray Queue shows how this class may be used to perform the following operations: Enqueue an element Dequeue an element and print it Get first element and print it Print all elements in the queue Clear the queue Quit . 2. Class LLQueue shows how queue may be implemented using java.util.LinkedList (as discussed in the lecture). Class TestLLQueue shows how to use this class. Tasks: 1. The given file DLL.java implements generic linked list as we saw in Lab03. (a). Use this class to implement a class, DLLQueue, that represents a queue for processing any types of values (generic queue). (b) Write a test class, TestDoubleQueue, that uses DLLQueue to allow a user to perform the same operations as TestLL Queue but using Double data objects. (c) Write a test class, TestStudentQueue, that allows a user to perform the same operations as in (b) above but using Student objects. CS 205 Lab 5 - Queue Objectives: Learn How to Implement Queue using Doubly Linked List Learn How to Use Queue Examples: 1. Class Array Queue shows how queue may be implemented using array (as discussed in the lecture). Class TestArray Queue shows how this class may be used to perform the following operations: Enqueue an element Dequeue an element and print it Get first element and print it Print all elements in the queue Clear the queue Quit . 2. Class LLQueue shows how queue may be implemented using java.util.LinkedList (as discussed in the lecture). Class TestLLQueue shows how to use this class. Tasks: 1. The given file DLL.java implements generic linked list as we saw in Lab03. (a). Use this class to implement a class, DLLQueue, that represents a queue for processing any types of values (generic queue). (b) Write a test class, TestDoubleQueue, that uses DLLQueue to allow a user to perform the same operations as TestLL Queue but using Double data objects. (c) Write a test class, TestStudentQueue, that allows a user to perform the same operations as in (b) above but using Student objects