I'm doing the implementation in java, however I have some problems with how to integrate generics and lambda in the deque:
Deque.java
:
import java.util.ArrayList;
public class Deque {
private Element head;
private Element tail;
public Deque() {
head = tail = null;
}
public boolean isEmpty() {
return head == null && tail == null;
}
public boolean isFull() {
return false;
}
public ArrayList<Integer> List() throws EmptyListException { //generics and exception
ArrayList<Integer> list = new ArrayList<Integer>();
if(head == null)
throw new EmptyListException("The list is empty!");
else{
Element elementFirst = getFirst();
while(elementFirst != null){
int value = elementFirst.getValue();
list.add(value);
elementFirst = elementFirst.getNext();
}
return list;
}
}
public void addFirst(Object o) {
System.out.println("Addited in first: " + o);
Element e = new Element(o);
e.setNext(head);
if(head != null) {
head.setPrev(e);
}
head = e;
if(tail == null) {
tail = e;
}
}
public void addLast(Object o) throws Exception {
System.out.println("Addited in last: " + o);
Element e = new Element(o);
e.setPrev(tail);
if(tail != null) {
tail.setNext(e);
}
tail = e;
if(head == null) {
head = e;
}
}
public Object removeFirst() throws Exception {
if(this.isEmpty()) {
throw new Exception ("The Queue is empty!");
} else {
Object o = head.getValue();
head = head.getNext();
if(head == null) {
tail = null;
} else {
head.setPrev(null);
}
System.out.println("Removed in first: " + o);
System.out.println("New head: " + head.getValue());
return o;
}
}
public Object removeLast() throws Exception {
if(this.isEmpty()) {
throw new Exception ("The Queue is empty!");
} else {
Object o = tail.getValue();
tail = tail.getPrev();
if(tail == null) {
head = null;
} else {
tail.setNext(null);
}
System.out.println("Removed in last: " + o);
System.out.println("New tail: " + tail.getValue());
return o;
}
}
public void clear() {
System.out.println("");
System.out.println("Start clear... ");
while (!this.isEmpty()) {
try {
this.removeFirst();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("End clear");
System.out.println("");
}
public void listing() {
Element e = head;
System.out.println("");
System.out.println("Start listing... ");
while (e != null) {
System.out.println("Value: " + e.getValue());
e = e.getNext();
}
System.out.println("End listing");
System.out.println("");
}
}
Element.java
:
public class Element {
private Element next;
private Element prev;
private Object value;
public Element (Object v) {
value = v;
}
public void setNext(Element e) {
this.next = e;
}
public Element getNext() {
return next;
}
public void setPrev(Element e) {
this.prev = e;
}
public Element getPrev() {
return prev;
}
public void setValue(Object value) {
this.value = value;
}
public Object getValue() {
if(value != null) {
System.out.println("the value is null!");
return false;
}
return value;
}
}
Main.java
:
public class Main {
public static void main(String[] args) throws Exception {
Deque deque = new Deque();
do{
System.out.println("Select the option\n 1->Add first:\n 2->Add last:\n3->List:\n 4->Remove first:\n 5->Remove last:\n 6->Clear:\n 7->Exit:\n");
option = scanner.next();
if(option == 1){
System.out.println("Enter with the value: ");
value = scanner.next();
deque.addFirst(value);
}
else if(option == 2){
System.out.println("Enter with the value: ");
value = scanner.next();
deque.addLast(value);
}
else if(option == 3){
ArrayList<String> myList = new ArrayList<String>();
try {
myList = deque.List();
//deque.listing();
}
catch (EmptyListException e) {
e.printStackTrace();
}
}
else if(option == 4){
deque.removeFirst();
}
else if(option == 5){
deque.removeLast();
}
else if(option == 6){
deque.clear();
}
else {
System.out.println("Invalid option");
}
}
while(option != "7");
}
}