I am trying to create a postfix evaluator that uses my IntStack and IntNode class. I cannot figure it out. HELP PLEASE!
public class IntStack{
private IntNode top;
private int counter;
private IntNode pos;
public IntStack(){
top = null;
counter = 0;
}
public boolean isEmpty()
{
return (top == null);
}
//tell you the number of IntNodes in a list
public int size ()
{
return counter;
}
public void pop() //pop = removes in stack
{
IntNode pos = top;
int index = 0;
for (int i = 0; i< index-1; i++)
pos = pos.getNext();
pos.setNext(pos.getNext().getNext());
{
//System.out.println(pos.getItem() + " " + pos.getCount());
pos = pos.getNext();
}
counter--;
}
public void popAll() // removeAll = clear in stack
{
counter = 0;
top = null;
}
public int peek()
{
return top.getItem();
}
/* public int peek(int index)
{
IntNode pos = top;
for (int i = 0; i < index; i++)
{
pos = top.getNext();
}
return top.getItem();
}
*/
public void push(int i)//add = push in stack
{
//if it is empty it sets the head to the new IntNode
if (top == null) {
top = new IntNode(i, null);
}
else {
pos = top;
while (pos.getNext() != null)
{
// System.out.print(pos.getItem() + " " + pos.getCount());
pos = pos.getNext();
}
pos.setNext(new IntNode(i, null));
}
counter ++;
}
public void display()
{
if (counter == 0) {
System.out.println("Stack is Empty");
}
else {
pos = top;
for (int d = 0; d < counter; d++)
{
System.out.println(pos.getItem());
pos = pos.getNext();
}
}
}
}
*************************************************
public class IntNode{
private int item;
private IntNode next;
//Start Constructors
public IntNode (int newItem){
item = newItem;
next = null;
}//end constructor
public IntNode(int newItem, IntNode nextNode){
item = newItem;
next = nextNode;
}//end constructor
//end Constructors
public void setItem (int newItem){
item = newItem;
}//end setItem
public int getItem() {
return item;
}//end getItem
public void setNext(IntNode nextNode){
next = nextNode;
}//end setNode
public IntNode getNext(){
return next;
}
}