Follow the guidelines please, Netbeans (Java) The skeleton classes (showing the
ID: 3833949 • Letter: F
Question
Follow the guidelines please,
Netbeans (Java)
The skeleton classes (showing the major operation to be implemented. That is: 1. Linked list class (which in here works as a queue since you adding from the front and at the end) a. public void addToStart (char ltemToken) b. public void addToEnd (char ItemToken) C. public void outputList(); 2. Node class a. Public void setLink(Node newlink) b. Public void getltem(); 3. Stack class a. Push (char Item) b. Pop c. top Item d. output Stack() 4. Main (test) class You need to think about other examples to demonstrate that your implementation works. a. Here you need to output your results after the user input an expression. Deliverables l. ame you program asg 1-your-id (e.g., asgl-2011300912) 2. Source code: zip file of your project 3. Test runs: copy of your runsExplanation / Answer
Hello,
You will find the below class here
1. Node
2.Linkedlist (suing Queue of the Node objects)
3.StackX class ( using the linkedlist class objects)
4.ParsePost -- > postfix algorithm using all the above classes
5.The actual program PostfixApp used for postfix operations.
===================================================
import java.util.*;
import java.io.*; // for I/O
/* Class Node */
class Node
{
protected int data;
protected Node link;
/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public int getItem()
{
return data;
}
}
/* Class Linkedlist using queue */
class Linkedlist
{
protected Node front, rear;
public int size;
/* Constructor */
public Linkedlist()
{
front = null;
rear = null;
size = 0;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == null;
}
/* Function to get the size of the queue */
public int getSize()
{
return size;
}
/* Function to insert an element to the queue */
public void insert(int data)
{
Node nptr = new Node(data, null);
if (rear == null)
{
front = nptr;
rear = nptr;
}
else
{
rear.setLink(nptr);
rear = rear.getLink();
}
size++ ;
}
/* Function to remove front element from the queue */
public int remove()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
Node ptr = front;
front = ptr.getLink();
if (front == null)
rear = null;
size-- ;
return ptr.getItem();
}
/* Function to check the front element of the queue */
public int peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return front.getData();
}
/* Function to display the status of the queue */
public void display()
{
System.out.print(" Stack = ");
if (size == 0)
{
System.out.print("Empty ");
return ;
}
Node ptr = front;
while (ptr != rear.getLink() )
{
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
}
////////////////////////////////////////////////////////////////
class StackX
{
private int maxSize;
private Linkedlist stackArray;
private int top;
//--------------------------------------------------------------
public StackX(int size) // constructor
{
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
//--------------------------------------------------------------
public void push(int j) // put item on top of stack
{ stackArray.insert(j); ++top ; }
//--------------------------------------------------------------
public int pop() // take item from top of stack
{ top-- ; return stackArray.getItem(); }
//--------------------------------------------------------------
public int peek() // peek at top of stack
{ return stackArray.peek(); }
//--------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{ return (top == -1); }
//--------------------------------------------------------------
public boolean isFull() // true if stack is full
{ return (top == maxSize-1); }
//--------------------------------------------------------------
public int size() // return size
{ return top+1; }
//--------------------------------------------------------------
public void displayStack(String s)
{
stackArray.display()
}
//--------------------------------------------------------------
} // end class StackX
////////////////////////////////////////////////////////////////
class ParsePost
{
private StackX theStack;
private String input;
//--------------------------------------------------------------
public ParsePost(String s)
{ input = s; }
//--------------------------------------------------------------
public int doParse()
{
theStack = new StackX(20); // make new stack
char ch;
int j;
int num1, num2, interAns;
for(j=0; j<input.length(); j++) // for each char,
{
ch = input.charAt(j); // read from input
theStack.displayStack(""+ch+" "); // *diagnostic*
if(ch >= '0' && ch <= '9') // if it's a number
theStack.push( (int)(ch-'0') ); // push it
else // it's an operator
{
num2 = theStack.pop(); // pop operands
num1 = theStack.pop();
switch(ch) // do arithmetic
{
case '+':
interAns = num1 + num2;
break;
case '-':
interAns = num1 - num2;
break;
case '*':
interAns = num1 * num2;
break;
case '/':
interAns = num1 / num2;
break;
default:
interAns = 0;
} // end switch
theStack.push(interAns); // push result
} // end else
} // end for
interAns = theStack.pop(); // get answer
return interAns;
} // end doParse()
} // end class ParsePost
////////////////////////////////////////////////////////////////
class PostfixApp
{
public static void main(String[] args) throws IOException
{
String input;
int output;
while(true)
{
System.out.print("Enter postfix: ");
System.out.flush();
input = getString(); // read a string from kbd
if( input.equals("") ) // quit if [Enter]
break;
// make a parser
ParsePost aParser = new ParsePost(input);
output = aParser.doParse(); // do the evaluation
System.out.println("Evaluates to " + output);
} // end while
} // end main()
//--------------------------------------------------------------
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
//--------------------------------------------------------------
} // end class PostfixApp
////////////////////////////////////////////////////////////////