Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created January 14, 2014 06:19
Show Gist options
  • Save kanrourou/8413913 to your computer and use it in GitHub Desktop.
Save kanrourou/8413913 to your computer and use it in GitHub Desktop.
import java.util.Iterator;
public class LinkedListStackOfStrings{
private class Node{
private String item;
private Node next;
}
public class ListIterator implements Iterator{
private Node current=first;
public boolean hasNext(){
return current!=null;
}
public void remove(){
;
}
public String next(){
String s=current.item;
current=current.next;
return s;
}
}
private Node first=null;
public Iterator iterator(){
return new ListIterator();
}
public boolean isEmpty(){
if(first==null)
return true;
else
return false;
}
public void push(String s){
Node oldfirst=first;
first=new Node();
first.item=s;
first.next=oldfirst;
}
public String pop(){
if(!isEmpty()){
String s=first.item;
first=first.next;
return s;
}
else
throw new IllegalArgumentException("Invalid operation!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment