Created
January 20, 2017 13:31
-
-
Save akaliacius/8da15fc4a507a3bdd5cda99a41357261 to your computer and use it in GitHub Desktop.
Implementation of Stack datastructure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
public class LinkedStack<E> implements Iterable<E> { | |
private Node<E> head = null; | |
private int size = 0; | |
@Override | |
public Iterator iterator(){return new StackIterator();} | |
private class StackIterator implements Iterator<E> { | |
private Node<E> current = head; | |
@Override | |
public boolean hasNext() { | |
return current.next != null; | |
} | |
@Override | |
public E next() { | |
E content = current.content; | |
current = current.next; | |
return content; | |
} | |
} | |
private class Node<E>{ | |
E content; | |
Node next; | |
} | |
public boolean isEmpty(){ | |
return head == null; | |
} | |
public void push(E content){ | |
Node n = new Node(); | |
n.content = content; | |
n.next = head; | |
head = n; | |
size++; | |
} | |
public E pop(){ | |
if(size == 0)return null; | |
E item = head.content; | |
Node temp = head.next; | |
head.next = null; | |
head = temp; | |
size--; | |
return item; | |
} | |
public int size(){ | |
return size; | |
} | |
public E peek(){ | |
return head.content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment