StacksQueues/Java/LinkedQueue
From charlesreid1
Link: https://git.charlesreid1.com/cs/java/src/master/stacks-queues-deques/queues/LinkedQueue.java
LinkedQueue is a linked list based queue class implemented in Java.
Contents
Code
Interface
This class implements the standard queue methods:
- add/enqueue
- remove/dequeue
as well as the convenience methods:
- size
- isEmpty
- rotate (rotate moves one element from front of queue to rear of queue)
Test
Here is a test of the LinkedQueue object that shows how it works; this manipulates a queue of integers.
public static void main(String[] args) { LinkedQueue<Integer> q = new LinkedQueue<Integer>(); System.out.println(q); q.enqueue(100); q.enqueue(100); q.enqueue(100); System.out.println(q); for(int i=0; i<10; i++) { q.enqueue(i); } System.out.println(q); q.dequeue(); q.dequeue(); q.dequeue(); System.out.println(q); for(int i=0; i<3; i++) { q.dequeue(); } System.out.println(q); System.out.println("Rotate:"); q.rotate(); System.out.println(q); q.rotate(); System.out.println(q); q.rotate(); System.out.println(q); System.out.println("Size = " + q.size()); }
Implementation
class Empty extends ArrayIndexOutOfBoundsException{} class LinkedQueue { Node<E> tail; int size; public LinkedQueue() { size = 0; }; public int size() { return size; } public boolean isEmpty() { return (size==0); } public String toString() { StringBuffer sb = new StringBuffer(); if(isEmpty()) { return "[ EMPTY QUEUE ]"; } else { sb.append("[ "); Node<E> runner = tail.getNext(); int k = 0; while(k<size && runner != null) { sb.append(runner); sb.append(" "); k++; runner = runner.getNext(); } sb.append(" ]"); return sb.toString(); } }; /** Add item e to the back of the linked list. */ public void enqueue(E e) { if(isEmpty()) { Node<E> newtail = new Node<E>(e); // everybody point at the new node tail = newtail; // tail will link to itself in a 1 item list tail.setNext(tail); } else { Node<E> newtail = new Node<E>(e, tail.getNext()); // link the new tail tail.setNext(newtail); // advance tail pointer tail = tail.getNext(); } size++; } /** Remove item e from the front of the linked list. */ public E dequeue() { if(isEmpty()) { throw new Empty(); } else if(size==1) { E res = tail.getData(); tail = null; size--; return res; } else { // length of list is at least 2 items. // if length = 2, we will end up linking tail to itself, which is fine. Node<E> rm = tail.getNext(); Node<E> newhead = rm.getNext(); tail.setNext(newhead); size--; return rm.getData(); } } /** Rotate the deque by skipping the next item in line and putting it in back. */ public void rotate() { if(isEmpty()) { throw new Empty(); } // The whole point of using a circular linked list is to avoid creating // a new node to do this kind of operation. // So don't call enqueue(dequeue()) !!! // // Simply advance the tail pointer forward by one. tail = tail.getNext(); } }
Flags
Data Structures Part of Computer Science Notes
This is the staging ground for computer science notes as part of the 2017 CS Study Plan.
Classes of data structures: Abstract Data Types Array-based and Link-based memory management: ArrayLists and Linked Lists Algorithmic Analysis of Data Structures: Algorithmic Analysis of Data Structures Advanced data structures: Advanced Data Structures
|
Arrays Part of Computer Science Notes
Series on Data Structures Python: Arrays/Python · Arrays/Python/Sizeof · Arrays/Python/AppendCost · Arrays/Python/CaesarCipher · Arrays/Python/CompactArrays · Arrays/Python/DynamicArray Java: Arrays/Java · Arrays/Java/CaesarCipher · Arrays/Java/FisherYates · Arrays/Java/PythonList · Arrays/Java/Repeatedly_Remove Categories: Category:Python Arrays
|
Stacks and Queues Part of Computer Science Notes
Series on Data Structures
Stacks and Queues: Python StacksQueues/Python · StacksQueues/Python/ArrayStack · StacksQueues/Python/ArrayQueue · StacksQueues/Python/ArrayDeque StacksQueues/Python/LinkedStack
Stacks and Queues: Java StacksQueues/Java · StacksQueues/Java/ArrayStack · StacksQueues/Java/ArrayQueue · StacksQueues/Java/ArrayQueueFS · StacksQueues/Java/ArrayDeque StacksQueues/Java/LinkedStack · StacksQueues/Java/LinkedQueue · StacksQueues/Java/LinkedDeque
Applications Postfix_Expressions#Stacks · StacksQueues/Subsets · StacksQueues/Subsets/Java
|
Priority Queues and Heaps Part of Computer Science Notes
Series on Data Structures
Java: Priority Queues/Java · Priority Queues/ADT · Priority Queues/Sorted · Priority Queues/Unsorted Performance: Priority Queues/Timing and Performance Applications: Maximum Oriented Priority Queue · Priority Queues/Stack
Priority Queues/Heap · Priority Queues/Java · Priority Queues/Comparators
|
Linked List Part of Computer Science Notes
Series on Data Structures Java: Linked Lists/Java · Linked Lists/Java/Single · Linked Lists/Java/Double · Linked Lists/Java/Circular Performance: Linked Lists/Java/Timing · Linked Lists/Java/Reverse Python: Linked Lists/Python · Linked Lists/Python/Single
|
Trees Part of Computer Science Notes
Series on Data Structures Abstract data type: Trees/ADT Concrete implementations: Trees/LinkedTree · Trees/ArrayTree · SimpleTree
Tree Traversal Preorder traversal: Trees/Preorder Postorder traversal: Trees/Postorder In-Order traversal: Binary Trees/Inorder Breadth-First Search: BFS Breadth-First Traversal: BFT Depth-First Search: DFS Depth-First Traversal: DFT OOP Principles for Traversal: Tree Traversal/OOP · Tree Traversal/Traversal Method Template Tree operations: Trees/Operations Performance · Trees/Removal
Tree Applications Finding Minimum in Log N Time: Tree/LogN Min Search
Abstract data type: Binary Trees/ADT Concrete implementations: Binary Trees/LinkedBinTree · Binary Trees/ArrayBinTree Binary Trees/Cheat Sheet · Binary Trees/OOP · Binary Trees/Implementation Notes
|
Search Trees Part of Computer Science Notes
Series on Data Structures
Binary Search Trees · Balanced Search Trees Trees/OOP · Search Trees/OOP · Tree Traversal/OOP · Binary Trees/Inorder
(Note that heaps are also value-sorting trees with minimums at the top. See Template:PriorityQueuesFlag and Priority Queues.)
|
Maps and Dictionaries Part of Computer Science Notes
Series on Data Structures
Maps/Dictionaries Maps · Maps/ADT · Maps in Java · Maps/OOP · Maps/Operations and Performance Map implementations: Maps/AbstractMap · Maps/UnsortedArrayMap · Maps/SortedArrayMap Dictionary implementations: Dictionaries/LinkedDict · Dictionaries/ArrayDict
Hashes Hash Maps/OOP · Hash Maps/Operations and Performance Hash Maps/Dynamic Resizing · Hash Maps/Collision Handling with Chaining Hash functions: Hash Functions · Hash Functions/Cyclic Permutation Hash map implementations: Hash Maps/AbstractHashMap · Hash Maps/ChainedHashMap
Skip Lists · Java/ConcurrentSkipList · Java implementations: SkipList
Sets Sets · Sets/ADT · Sets in Java · Sets/OOP · Multisets
|