Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DoublyLinkedList;
- import java.util.NoSuchElementException;
- public class DLQueue {
- private Node first;
- private Node last;
- public DLQueue(){
- first = null;
- last = null;
- }
- /**
- * adds an element to the stack
- * @param data - the element to add
- */
- public void push(Object data){
- Node newNode = new Node();
- newNode.data = data;
- newNode.next = first;
- first = newNode;
- }
- /**
- * returns the last element added to the stack
- * @return an element removed, of the type Object
- */
- public Object pop(){
- if(first == null){throw new NoSuchElementException();}
- Object temp = first.data;
- first = first.next;
- return temp;
- }
- /**
- * returns the last element added to the stack
- * @return an element of the type Object
- */
- public Object peek(){
- if(first == null){throw new NoSuchElementException();}
- return first.data;
- }
- /**
- * adds an element to the list
- * @return
- */
- public void add(Object data){
- Node newNode = new Node();
- newNode.data = data;
- newNode.next = first;
- newNode.previous = null;
- if (first == null) {
- last = newNode;
- }
- else {
- first.previous = newNode;
- }
- first = newNode;
- }
- /**
- * removes an element in the current position of the iterator
- * @return - the element that is removed
- */
- public Object remove(){
- if(first==null){
- throw new NoSuchElementException();
- }
- else if(first == last){
- last = null;
- }
- Object temp = first.data;
- first = first.next;
- return temp;
- }
- class Node{
- public Object data;
- public Node next;
- public Node previous;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement