Skip to content

Instantly share code, notes, and snippets.

@pmiguel
Last active December 18, 2015 21:29
Show Gist options
  • Save pmiguel/5847461 to your computer and use it in GitHub Desktop.
Save pmiguel/5847461 to your computer and use it in GitHub Desktop.
/**
*
* @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