Created
January 14, 2014 06:19
-
-
Save kanrourou/8413913 to your computer and use it in GitHub Desktop.
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 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