# Queues
1. Use the code below to implement a queue that holds strings.
2. Use your queue to simulate a lunch queue. Alice joins, then Bob and then Charles. Alice is served, then Bob. Dave, Ellen and Faisal join. Two more people are served. Georgina joins the queue. Two more people are served. Harry is the last to join and then everyone is served in their turn.
3. Now implement a generic queue.
# Stacks
1. Using the queue code as a model, write a Java implementation of a stack that holds data of type Integer.
2. Write down how you tested your implementation
3. Update your implementation to use Generic types
4. Write down how you tested your implementation
5. Implement a queue of objects of class Student. The Student class contains member variables representing the following: Forename, Surname, Gender, Date of Birth
# Extension
1. Write a Java implementation of doubly linked list. (You can find more details here: <https://en.wikipedia.org/wiki/Linked_list>)
2. Write a Java implementation of a circular linked list
# Queue of String Objects Implementation
```java
public class QQ {
public static void main(String[] args) {
// TODO code application logic here
MyQueue queue = new MyQueue("One");
queue.add("Two");
queue.add("Three");
queue.add("Four");
queue.add("Five");
queue.printQueue();
System.out.println("Item removed: " + queue.remove());
System.out.println("New Queue");
queue.printQueue();
queue.add("Six");
System.out.println("New Queue");
queue.printQueue();
}
}
```
```java
public class QNode {
private String data;
private QNode next;
QNode(String s)
{
data = s;
}
public String getData()
{
return data;
}
public QNode getNext()
{
return next;
}
public void setNext(QNode node)
{
next = node;
}
}
```
```java
public class MyQueue {
private QNode head;
private QNode tail;
MyQueue(String s)
{
head = new QNode(s);
tail = head;
}
public void add(String s)
{
QNode newTail = new QNode(s);
tail.setNext(newTail);
tail = newTail;
}
public String remove()
{
QNode oldHead = head;
head = head.getNext();
return oldHead.getData();
}
public void printQueue()
{
QNode current = head;
while (current.getNext()!= null)
{
System.out.println(current.getData());
current = current.getNext();
}
System.out.println(tail.getData());
}
}
```
# Generic Queue Implementation
``` java
public class QNode<T> {
private T data;
private QNode next;
QNode(T t)
{
data = t;
}
public T getData()
{
return data;
}
public QNode getNext()
{
return next;
}
public void setNext(QNode node)
{
next = node;
}
}
```
``` java
public class MyQueue<T> {
private QNode head;
private QNode tail;
MyQueue(T t)
{
head = new QNode(t);
tail = head;
}
public void add(T t)
{
QNode newTail = new QNode(t);
tail.setNext(newTail);
tail = newTail;
}
public T remove()
{
QNode oldHead = head;
head = head.getNext();
return (T)oldHead.getData();
}
public void printQueue()
{
QNode current = head;
while (current.getNext()!= null)
{
System.out.println(current.getData());
current = current.getNext();
}
System.out.println(tail.getData());
}
}
```
[[Java Binary Trees]]