Upload files to ''

This commit is contained in:
2023-03-21 18:10:38 -09:00
parent 93f7003773
commit c3f2460d29
3 changed files with 134 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
public class LinkedQueue<T> implements Queue<T> {
private LinearNode<T> head = null;
private LinearNode<T> tail = null;
private int size = 0;
public void enqueue(T element) {
if (tail == null){ //O(1)
head = new LinearNode<T>(element);
tail = head;
size++;
}
else if (head == tail){ //O(1)
head.setNext(new LinearNode<T>(element));
tail = head.getNext();
size++;
}
else{ //O(1)
tail.setNext(new LinearNode<T>(element));
tail = tail.getNext();
size++;
}
}
public T dequeue() {
if (head == null){ //O(1)
return null;
}
else{ //O(1)
LinearNode<T> temp = head;
head = head.getNext();
size--;
return temp.getElement();
}
}
@Override
public T getFront() { //O(1)
if (head == null){
return null;
}
else{
return head.getElement();
}
}
@Override
public boolean isEmpty() { //O(1)
return (head == null);
}
public int size() { //O(1)
return size;
}
}
+66
View File
@@ -0,0 +1,66 @@
public class LinkedQueue2<T> implements Queue<T> {
private DoubleLinkedNode<T> head;
private DoubleLinkedNode<T> tail;
private int size = 0;
//O(1)
public void enqueue(T element) {
if (head == null){
head = new DoubleLinkedNode<T>(element);
tail = head;
size++;
}
else if (head == tail){
DoubleLinkedNode<T> temp = new DoubleLinkedNode<T>(element);
temp.setNext(head);
head = temp;
tail = head.getNext();
tail.setPrev(head);
size++;
}
else{
DoubleLinkedNode<T> temp = new DoubleLinkedNode<T>(element);
temp.setNext(head);
head = temp;
head.getNext().setPrev(head);
size++;
}
}
//O(1)
public T dequeue() {
if (head == null){
return null;
}
else if (head == tail){
T temp = head.getElement();
head = null;
tail = null;
size--;
return temp;
}
else{
T temp = tail.getElement();
tail = tail.getPrev();
tail.setNext(null);
size--;
return temp;
}
}
//O(1)
public T getFront() {
return tail.getElement();
}
//O(1)
public boolean isEmpty() {
return head == null;
}
//O(1)
public int size() {
return size;
}
}
+12
View File
@@ -0,0 +1,12 @@
public interface Queue<T> {
public void enqueue(T element);
public T dequeue();
public T getFront();
public boolean isEmpty();
public int size();
}