Adding Queue linked list client
This commit is contained in:
parent
09ec2b8bb0
commit
6774300a8a
@ -1,9 +1,7 @@
|
||||
package com.hithomelabs.clients;
|
||||
|
||||
import com.hithomelabs.princeton1.module4.ArrayQueue;
|
||||
import com.hithomelabs.princeton1.module4.ArrayStack;
|
||||
import com.hithomelabs.princeton1.module4.Queue;
|
||||
import com.hithomelabs.princeton1.module4.Stack;
|
||||
|
||||
public class ArrayQueueClient {
|
||||
public static void main(String[] args) {
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.hithomelabs.clients;
|
||||
|
||||
import com.hithomelabs.princeton1.module4.LinkedQueue;
|
||||
import com.hithomelabs.princeton1.module4.Queue;
|
||||
|
||||
public class LinkedQueueClient {
|
||||
public static void main(String[] args) {
|
||||
Queue<Integer> queue = new LinkedQueue<Integer>();
|
||||
for(int i = 0; i < 21; i++){
|
||||
queue.enqueue(i);
|
||||
}
|
||||
for(int i = 0; i < 13; i++){
|
||||
System.out.println(queue.dequeue());
|
||||
}
|
||||
|
||||
// Using object s, as stack is generic but every java Object has implementation of toString()
|
||||
for (Object s : queue)
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@ package com.hithomelabs.clients;
|
||||
import com.hithomelabs.princeton1.module4.LinkedStack;
|
||||
import com.hithomelabs.princeton1.module4.Stack;
|
||||
|
||||
public class LinkedArrayClient {
|
||||
public class LinkedStackClient {
|
||||
public static void main(String[] args) {
|
||||
Stack<String> stack = new LinkedStack<String>();
|
||||
stack.push("hello");
|
@ -0,0 +1,74 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class LinkedQueue<E> extends Queue<E>{
|
||||
|
||||
Node head;
|
||||
Node tail;
|
||||
int size;
|
||||
|
||||
private class Node{
|
||||
E value;
|
||||
Node next;
|
||||
|
||||
Node(E value){
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return size==0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E dequeue() {
|
||||
if(isEmpty())
|
||||
return null;
|
||||
E element = head.value;
|
||||
// Only a single element is present
|
||||
if (head == tail){
|
||||
tail = null;
|
||||
}
|
||||
head = head.next;
|
||||
--size;
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enqueue(E element) {
|
||||
Node newNode = new Node(element);
|
||||
if(isEmpty())
|
||||
head = newNode;
|
||||
else
|
||||
tail.next = newNode;
|
||||
tail = newNode;
|
||||
++size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<E>() {
|
||||
Node current = head;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return current != tail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
E element = current.value;
|
||||
current = current.next;
|
||||
return element;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user