0
votes

For class, we are tasked with the assignment of converting an infix expression to postfix using a "doubly linked list stack implementation in abstraction". I was able to write a program that uses the Stack in order to make the conversion, but what is the purpose of the doubly linked list? What node of information are we adding to the list?

This is the stack class that was provided to us as an example. Why is the variable next type Stack? Shouldn't it be Node?

public class Stack {
  private int data;
  private Stack next;
  private static Stack top;

  //Initialize Stack
  public Stack () {
      top = null;
      next = null;
  }

  public Stack (int d, Stack node) {
      data = d;
      next = node;
  }

  public void push(int item) {
      top = new Stack(item, top);
  }

  public int peek () {
     if(top == null) {
         throw new EmptyStackException();
     } 
     return (top.data);          
  }

  public int pop () {
      int item;

      if(top == null) {
          throw new EmptyStackException();
      } else
          item = top.data;
      top = top.next;
      return (item);
  }

  public boolean empty () {
      return top == null;
  }
}

If I create a doubly linked list and node class, what data is in the node object?

2

2 Answers

0
votes

Data member of Node class would be: pointers to previous and next, along with the data.

0
votes

You have to convert an infix notation to postfix, which means converting something like 2 + 2 to 2 2 + In this case, Node will hold either the operator (+) or operand (2) and it will hold links to the previous and next nodes in the list. For example, if I represent a node using parenthesis and links using arrows, 2 + 2 looks like: NULL <- (2) <-> (+) <-> (2) -> NULL