Skip to content

Instantly share code, notes, and snippets.

@akaliacius
Created January 20, 2017 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akaliacius/8da15fc4a507a3bdd5cda99a41357261 to your computer and use it in GitHub Desktop.
Save akaliacius/8da15fc4a507a3bdd5cda99a41357261 to your computer and use it in GitHub Desktop.
Implementation of Stack datastructure
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