public static Integer calcs(String postFix) {
         StackInterface<Character> stack = new LinkedStack<Character>(); // instantiate Character stack
         Integer result = new Integer(0);
         Node<Integer> num1, num2;
         Integer int1 = new Integer(0); // declare address to each variable object
         Integer int2 = new Integer(0);
         num1 = new Node<Integer>(int1, null);
         num2 = new Node<Integer>(int2, null);
         num1.setNext(num2); // link two nodes   
       
         // print linked nodes  
         Node<Integer> pointer = num1;
         Integer number = pointer.getData();
         System.out.println("head = " + number);
         pointer = pointer.getNext();
         number = pointer.getData();
         System.out.println("tail = " + number);
         System.out.println();
      	
      	Node<Integer> node1 = new Node<Integer>(1, null);
         Node<Integer> node2 = new Node<Integer>(2, null);
      	node1.setNext(node2);
      	node2.setNext(node1);
      
         for(Node<Integer> i = node1; i != null; i = i.getNext()) {
      	  System.out.println(i.toString());
      	}

I'm trying to make PostFix calculator using Node.

How would I make it work to add, for example, if two integers are 3 and 5, which should equal to 8?

for(int i = 0; i < postFix.length(); i++) {
               Character chars = new Character(postFix.charAt(i)); // separate each character
               switch(chars) {
                  case '+':
                     stack.pop(); // pop operators
                     if(chars == '+') {
                        result = node1 + node2;
                     }
                     break;

I think my if statements are wrong or perhaps the arrangement of node.. can anyone help me out here?

Thanks!

This is the Node.java

public class Node<T> {

	// data fields (reference variables)
	// data stores an object of any class
	private T data;
	// next points to the next node
	private Node<T> next;

	/**
	 * Constructor - Used To Create EAch Object & Initialize DAta Fields.
	 * 
	 * @param data2
	 *            initializes the data reference variable.
	 * @param next2
	 *            initializes the next reference variable..
	 */
	public Node(T data2, Node<T> next2) {
		data = data2;
		next = next2;
	}

	/**
	 * Used to Display The Data Stored In EAch Node.
	 * 
	 * @return a String for the data
	 */
	public String toString() {
		return data.toString();
	}

	/**
	 * This Is An "Accessor" Method - Used To Get A Data Field.
	 * 
	 * @return the data
	 */
	public T getData() {
		return data;
	}

	/**
	 * This Is An "Accessor" Method - Used To Get A Data Field.
	 * 
	 * @return the address to the next node
	 */
	public Node<T> getNext() {
		return next;
	}

	/**
	 * This Is A "Mutator" Method - Used To Set A Data Field.
	 * 
	 * @param data2
	 *            is a pointer to an object.
	 */
	public void setData(T data2) {
		data = data2;
	}

	/**
	 * This Is A "Mutator" Method - Used To Set A Data Field.
	 * 
	 * @param next2
	 *            is a pointer to the next node.
	 */
	public void setNext(Node<T> next2) {
		next = next2;
	}

OK, do you understand what postfix computation is? Anyway, I will give you an example. Let say you want to add 3 and 5 in postfix, you must push 3 and 5 on to stack first, and then add them when you see a symbol.

// example
// your input is 3 5 +
// push 3 on to stack
// push 5 on to stack
// see +, pop last 2 stacks and compute them regarding the sign which is +
// push the result on to stack
// no more calculation, whatever on the top of the stack is the result

In your code, there is already a stack variable. You need to use it for computation. I am guessing that Node class is used as a link list, so you would know which number comes first.

Apparently, I still think that there are other things missing here. How do you compose your postfix sequence? If you use Node<Integer>, then you cannot really compose a complete postfix because the symbol is separated from the sequence. If you create as Node<String> then it is feasible to have a postfix string (sequence) before computation...

Edited 6 Years Ago by Taywin: n/a

// push 3 on to stack
// push 5 on to stack
switch(chars) {
  case '1':
  case '2':
  case '3':
  ..
  case '5':
    stack.push(chars);
    break;
}

// see +, pop last 2 stacks and compute them regarding the sign which is +
switch(symbols) {
  case '+':
  case '-':
  ...
    stack.pop()
}

My question here is do you use Nodes here to calculate? Or something like
Integer result = num1 + num2;
Do I declare Integer num1 and num2? I understand that the stack stores two integers but how does computer know which num is declared to which variable? It's just in the stack.push(input) right?

I'm not quite getting that portion.

// push the result on to stack
After the if-else statement/switch
if(...) {
}
stack.push(chars); // is this correct? Would I push 'chars' or the entire String?

// no more calculation, whatever on the top of the stack is the result

So is the code above the top of the stack = result?

OK, my assumption here is that you must use Node class to do this assignment. Also, I am guessing that you are passing in a link list of Nodes as a string instead of a String class. Therefore I am guessing that the Node class is declare as String (or Character if you want) instead of Integer.

// initial variable
Node<String> head = null;  // the head of the list
Node<String> currNode;     // a pointer of the list
Node<String> newNode;      // for creating new node
String str = "3 5 + 4 -";  // (3+5)-4
String[] tokens = str.split(" ");

// construct link list Node
for (int i=0; i<tokens.length; i++) {  // not sure you use .length for array size?
  if (head==null) {  // new list
    head = new Node<String>(tokens[i], null);  // create the head of the list
    currNode = head;   // step to the node
  }
  else {  // concatenate the list
    newNode = new Node<String>(token[i], null);  // create new node
    currNode.setNext(newNode);      // link the current node with the new node
    currNode = newNode;             // step to the new node
  }
}

// now go through the list
currNode = head;
while (currNode.getNext()!=null) {
  // check what is the data inside currNode
  String token = currNode.getData();
  switch (token) {
    case "0": case "1": case "2": case "3": case "4":
    case "5": case "6": case "7": case "8": case "9": // number
      // push the character token onto stack here
      break;
    case "+":  // do addition
      // Pop 2 of the stacks here.
      // Convert the char to integer and do addition
      // Convert it back to char and push back onto stack
      // Remember to handle error when the stack size is only 1 -> invalid postfix format
      break;
    case "-":  // do subtraction
      // similar to addition
      break;
    case "*":  // do multiplication
      // similar to addition
      break;
    case "/":  // do division
      // similar to addition
      break;
  }  // the rest of string which is not matched the condition will be ignored
  currNode = currNode.getNext();
}

// If all went well, the stack should be size 1 and it is the result value.
// Here is what the stack should be like for each iteration...
// Iter1 (3):  3
// Iter2 (5):  5
//             3
// Iter3 (+):  8
// Iter4 (4):  4
//             8
// Iter5 (-):  4
// done
//
// If the size of the stack after the process is not equal to 1,
// the postfix string may not be valid

On thing here... If you use switch as I did, you will be able to handle ONLY 1 digit number. You need to modify it in order to handle all kind of numbers.

Edited 6 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.