Last active
December 18, 2015 21:29
-
-
Save pmiguel/5847461 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
/** | |
* | |
* @author Pedro | |
*/ | |
public class Stack { | |
private Node top; | |
private int nElems; | |
public Stack() { | |
top = null; | |
nElems = 0; | |
} | |
public void push(char c) { | |
Node n = new Node(c); | |
n.setBellowNode(top); | |
top = n; | |
nElems++; | |
} | |
public void pop() { | |
top = top.getBellowNode(); | |
nElems--; | |
} | |
public void popN(int n) { | |
while(n != 0 || top != null) { | |
pop(); | |
n--; | |
} | |
} | |
public void print() { | |
Node tmp = top; | |
while(tmp != null) { | |
System.out.print("->" + tmp.getChar()); | |
tmp = tmp.getBellowNode(); | |
} | |
} | |
private class Node { | |
private Node bellow; | |
private char c; | |
public Node(char c) { | |
this.c = c; | |
bellow = null; | |
} | |
public void setBellowNode(Node n) { | |
this.bellow = n; | |
} | |
public Node getBellowNode() { | |
return bellow; | |
} | |
public char getChar() { | |
return c; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment