Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Algorithms/LinkedListRecursive/src/main/java/adt/stack/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package adt.stack;

/**
* The interface of a generic stack. The queue is able to store any kind of
* data.
*
*/
public interface Stack<T> {

/**
* Inserts a new element in the stack or returns an exception if the stack
* is full. Null elements are not allowed (the stack remains unchanged).
*
* @param element
* @throws StackOverflowException
*/
public void push(T element) throws StackOverflowException;

/**
* If the stack has elements, it removes the top of the stack and returns
* it; otherwise, it returns an exception.
*
* @return
* @throws StackUnderflowException
*/
public T pop() throws StackUnderflowException;

/**
* Returns (without removing) the top element of the stack or null if the
* stack is empty.
*
* @return
*/
public T top();

/**
* Returns true if the stack is empty or false, otherwise.
*
* @return
*/
public boolean isEmpty();

/**
* Returns true if the stack is full or false, otherwise.
*
* @return
*/
public boolean isFull();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package adt.stack;

public class StackOverflowException extends Exception {

public StackOverflowException() {
super("Stack is full");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package adt.stack;

import adt.linkedList.DoubleLinkedList;
import adt.linkedList.RecursiveDoubleLinkedListImpl;

public class StackRecursiveDoubleLinkedListImpl<T> implements Stack<T> {

protected DoubleLinkedList<T> top;
protected int size;

public StackRecursiveDoubleLinkedListImpl(int size) {
this.size = size;
this.top = new RecursiveDoubleLinkedListImpl<T>();
}

@Override
public void push(T element) throws StackOverflowException {
if (isFull())
throw new StackOverflowException();
top.insertFirst(element);
}

@Override
public T pop() throws StackUnderflowException {
if (isEmpty())
throw new StackUnderflowException();
T popped = top();
top.removeFirst();
return popped;
}

@Override
public T top() {
return ((RecursiveDoubleLinkedListImpl<T>) top).getData();
}

@Override
public boolean isEmpty() {
return top.isEmpty();
}

@Override
public boolean isFull() {
return top.size() == size;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package adt.stack;

public class StackUnderflowException extends Exception {

public StackUnderflowException() {
super("Stack is empty");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package adt.linkedList;

/**
* The interface of a double linked list with a head and a last (Deque).
*
* @param <T>
*/
public interface DoubleLinkedList<T> extends LinkedList<T> {

/**
* Inserts a new element in the first position (head) of the list. The
* "head" reference must be updated.
*
* @param element
*/
public void insertFirst(T element);

/**
* Removes the first element (head) of the list. The "head" reference must
* be updated.
*/
public void removeFirst();

/**
* Removes the last element (last) of the list. The "last" reference must be
* updated.
*/
public void removeLast();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package adt.linkedList;

/**
* The interface of a generic linked list.
*/
public interface LinkedList<T> {
/**
* @return true if the list is empty or false, otherwise
*/
public boolean isEmpty();

/**
* @return the number of elements on the list
*/
public int size();

/**
* Searches for a given element in the list.
*
* @param element
* the element being searched for
* @return the element if it is in the list or null, otherwise
*/
public T search(T element);

/**
* Inserts a new element at the end of the list. Null elements must be
* ignored.
*
* @param element
* the element to be inserted
*/
public void insert(T element);

/**
* Removes an element from the list. If the element does not exist the list
* is not changed.
*
* @param element
* the element to be removed
*/
public void remove(T element);

/**
* Returns an array containing all elements in the structure. The array does
* not contain empty spaces (or null elements). The elements are put into
* the array from the beginning to the end of the list.
*
* @return an array containing all elements in the structure in the order
* they appear
*/
public T[] toArray();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package adt.linkedList;

public class RecursiveDoubleLinkedListImpl<T> extends
RecursiveSingleLinkedListImpl<T> implements DoubleLinkedList<T> {

protected RecursiveDoubleLinkedListImpl<T> previous;

public RecursiveDoubleLinkedListImpl() {

}

public RecursiveDoubleLinkedListImpl(T data,
RecursiveSingleLinkedListImpl<T> next,
RecursiveDoubleLinkedListImpl<T> previous) {
super(data, next);
this.previous = previous;
}

@Override
public void insert(T element) {
if (element != null) {
if (isEmpty()) {
setData(element);
setNext(new RecursiveDoubleLinkedListImpl<T>());
((RecursiveDoubleLinkedListImpl<T>) getNext()).setPrevious(this);
if (getPrevious() == null) {
setPrevious(new RecursiveDoubleLinkedListImpl<T>());
getPrevious().setNext(this);
}
} else
getNext().insert(element);
}
}

@Override
public void insertFirst(T element) {
if (element != null) {
if (isEmpty()) {
setData(element);
setPrevious(new RecursiveDoubleLinkedListImpl<T>());
getPrevious().setNext(this);
if (getNext() == null) {
setNext(new RecursiveDoubleLinkedListImpl<T>());
((RecursiveDoubleLinkedListImpl<T>) getNext()).setPrevious(this);
} else
tradeHead(((RecursiveDoubleLinkedListImpl<T>) getNext()), this);
} else
getPrevious().insertFirst(element);
}
}

private void tradeHead(RecursiveDoubleLinkedListImpl<T> head, RecursiveDoubleLinkedListImpl<T> newHead) {
T dataHead = head.getData();
head.setData(newHead.getData());
newHead.setData(dataHead);
newHead.setNext(head.getNext());
head.setNext(newHead);
((RecursiveDoubleLinkedListImpl<T>) newHead.getNext()).setPrevious(newHead);
newHead.setPrevious(head);
head.setPrevious(new RecursiveDoubleLinkedListImpl<T>());
head.getPrevious().setNext(head);
}

@Override
public void removeFirst() {
if (!isEmpty()) {
if (getPrevious().isEmpty()) {
setData(getNext().getData());
setNext(getNext().getNext());
} else
getPrevious().removeFirst();
}
}

@Override
public void removeLast() {
if (!isEmpty()) {
if (getNext().isEmpty()) {
setData(getNext().getData());
setNext(getNext().getNext());
} else
((RecursiveDoubleLinkedListImpl<T>) getNext()).removeLast();
}
}

public RecursiveDoubleLinkedListImpl<T> getPrevious() {
return previous;
}
public void setPrevious(RecursiveDoubleLinkedListImpl<T> previous) {
this.previous = previous;
}

}
Loading