In: Computer Science
Can you fix my code and remove the errors in java language.
public class LinkedStack<T> implements Stack<T> {
private Node<T> top;
private int numElements = 0;
public int size() {
return (numElements);
}
public boolean isEmpty() {
return (top == null);
}
public T top() throws StackException {
if (isEmpty())
throw new StackException("Stack is empty.");
return top.info;
}
public T pop() throws StackException {
Node<T> temp;
if (isEmpty())
throw new StackException("Stack underflow.");
temp = top;
top = top.getLink();
return temp.getInfo();
}
public void push(T item) {
Node<T> newNode = new Node();
newNode.setInfo(item);
newNode.setLink(top);
top = newNode;
}
@Override
public T peek() throws StackException {
return null;
}
@Override
public void clear() {
}
@Override
public int search(T item) {
return 0;
}
}
////////////////////////////////////////////////////
public interface Stack<T> {
public int size(); /* returns the size of the stack */ public boolean isEmpty(); /* checks if empty */
public T top() throws StackException;
public T pop() throws StackException;
public void push(T item) throws StackException;
public T peek() throws StackException;
public void clear();
public int search(T item);
}
class StackException extends RuntimeException {
public StackException(String err) {
super(err);
}
}
}
//////////////////////////////////////////////////
public class Node<T> {
public T info;
private Node<T> link;
public Node() { }
public Node (T info, Node<T> link) {
this.info = info;
this.link = link;
}
public void setInfo(T info) {
this.info = info;
}
public void setLink(Node<T> link) {
this.link = link;
}
public T getInfo() {
return info;
}
public Node<T> getLink() {
return link;
}
}
postfixExpression = empty String
operatorStack = empty stack
while (not end of infixExpression) {
symbol = next token
if (symbol is an operand)
concatenate symbol to postfixExpression
else { // symbol is an operator
while (not operatorStack.empty() &&
precedence(operatorStack.peek(),symbol) { topSymbol = operatorStack.pop();
concatenate topSymbol to postfixExpression;
} // end while
if (operatorStack.empty() || symbol != )’'/U2019' ) operatorStack.push(symbol);
else // pop the open parenthesis and discard it topSymbol = operatorStack.pop();
} // end else
} // end while
// get all remaining operators from the stack
while (not operatorStack.empty) {
topSymbol = operatorStack.pop();
concatenate topSymbol to postfixExpression
} // end while
return postfixExpression
//////////////////////////////////////////////////
import java.util.*;
class StackLinklist {
Node head=new Node(); //head pointer of linked list
public boolean isEmpty() //check whether stack empty or
not
{
if(head == null)
return true;
return false;
}
public void push(char x) //add element eo stack
{
Node t=new Node();
if (t != null) {
t.op=x;
t.next=head;
head=t;
}
else{
System.out.print("Stack overflow"); //heap overflow
return;
}
}
public char topmost() // return top most caharater of
stack
{
if (!isEmpty()) {
return head.op;
}
else {
System.out.println("Stack is empty");
return '\0';
}
}
public char pop() // remove the element
{
// underflow
if (head == null) {
System.out.print("\nStack Underflow");
return '\0';
}
char chp=head.op;
head = (head).next;
return chp;
}
private class Node { // class of linked list which denotes eac
node
char op;
Node next;
}
}
class InfixTOPostfixConversion
{
static String infixToPostfix(String str)
{
String r ="";
StackLinklist stack = new StackLinklist();
for (int i = 0; i<str.length(); ++i)
{
char c = str.charAt(i);
if (c == '(') //if opening bracket occur,add it into stack
stack.push(c);
else if (c == ')') //case for closing bracket
{
while (!stack.isEmpty() && stack.topmost() != '(')
r+= stack.pop();
if (!stack.isEmpty() && stack.topmost() != '(')
return "invalid expression";
else
stack.pop();
}
// case of operands
else if((c>='a' && c<='z') || (c>='A' &&
c<='Z') || (c>='0' &&c<='9'))
r+=c;
else // case of operators, operartors will be poped baed on
precedence
{
while (!stack.isEmpty() && Precedence(stack.topmost())
>= Precedence(c)){
if(stack.topmost() == '(')
return "invalid expression";
else
r+= stack.pop();
}
stack.push(c);
}
}
while (!stack.isEmpty()){
if(stack.topmost() == '(')
return "invalid expression";
r+= stack.pop();
}
return r;
}
public static void main(String[] args)
{
Scanner ob=new Scanner(System.in);
while(true){
int flag=0;
System.out.println("press 1 to enter infix string, press 2 to
exit");
int n=ob.nextInt();
switch(n){
case 1:
System.out.println("Enter the infix expression");
ob.nextLine();
String s=ob.nextLine();
String r=infixToPostfix(s);
if(r.equals("invalid expression"))
System.out.println(r);
else
System.out.println("Postfix Expression of given expression is
"+r);
break;
case 2:
flag=1;
break;
default:
System.out.println("please enter avalid input");
}
if(flag==1)
break;
System.out.println();
System.out.println();
}
}
static int Precedence(char ch) // function for deciding operator
precedence
{
int p=-1;
if(ch=='^'){
p=1000;
}
else if(ch=='*'){
p=500;
}
else if(ch=='/'){
p=500;
}
else if(ch=='+'){
p=100;
}
else if(ch=='-'){
p=100;
}
return p;
}
}
Following java program provides 2 options to user : 1 or 2.
Option 1, user is asked to enter the infix expression, as a
result, postfix equivalent of expression is displayed.
Option 2, exits the program.
PFB source code for all shared files.
Corrections : There was extra code was added in the end of Node
class, which was not part of any block. Hence removed it.
Code is tested , also sharing sample output below.
--------------------
LinkedStack.java
--------------------
public class LinkedStack<T> implements Stack<T> {
private Node<T> top;
private int numElements = 0;
public int size() {
return (numElements);
}
public boolean isEmpty() {
return (top == null);
}
public T top() throws StackException {
if (isEmpty())
throw new StackException("Stack is empty.");
return top.info;
}
public T pop() throws StackException {
Node<T> temp;
if (isEmpty())
throw new StackException("Stack underflow.");
temp = top;
top = top.getLink();
return temp.getInfo();
}
public void push(T item) {
Node<T> newNode = new Node();
newNode.setInfo(item);
newNode.setLink(top);
top = newNode;
}
@Override
public T peek() throws StackException {
return null;
}
@Override
public void clear() {
}
@Override
public int search(T item) {
return 0;
}
}
--------------------
--------------------
Node.java
--------------------
public class Node<T> {
public T info;
private Node<T> link;
public Node() {
}
public Node(T info, Node<T> link) {
this.info = info;
this.link = link;
}
public void setInfo(T info) {
this.info = info;
}
public void setLink(Node<T> link) {
this.link = link;
}
public T getInfo() {
return info;
}
public Node<T> getLink() {
return link;
}
}
--------------------
--------------------
Stack.java
--------------------
public interface Stack<T> {
public int size();
/* returns the size of the stack */ public boolean isEmpty(); /* checks if empty */
public T top() throws StackException;
public T pop() throws StackException;
public void push(T item) throws StackException;
public T peek() throws StackException;
public void clear();
public int search(T item);
}
class StackException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public StackException(String err) {
super(err);
}
}
--------------------
--------------------
StackLinlist.java
--------------------
import java.util.*;
class StackLinklist {
Node head = new Node(); // head pointer of linked list
public boolean isEmpty() // check whether stack empty or
not
{
if (head == null)
return true;
return false;
}
public void push(char x) // add element eo stack
{
Node t = new Node();
if (t != null) {
t.op = x;
t.next = head;
head = t;
} else {
System.out.print("Stack overflow"); // heap overflow
return;
}
}
public char topmost() // return top most caharater of
stack
{
if (!isEmpty()) {
return head.op;
} else {
System.out.println("Stack is empty");
return '\0';
}
}
public char pop() // remove the element
{
// underflow
if (head == null) {
System.out.print("\nStack Underflow");
return '\0';
}
char chp = head.op;
head = (head).next;
return chp;
}
private class Node { // class of linked list which denotes eac
node
char op;
Node next;
}
}
class InfixTOPostfixConversion {
static String infixToPostfix(String str) {
String r = "";
StackLinklist stack = new StackLinklist();
for (int i = 0; i < str.length(); ++i) {
char c = str.charAt(i);
if (c == '(') // if opening bracket occur,add it into stack
stack.push(c);
else if (c == ')') // case for closing bracket
{
while (!stack.isEmpty() && stack.topmost() != '(')
r += stack.pop();
if (!stack.isEmpty() && stack.topmost() != '(')
return "invalid expression";
else
stack.pop();
}
// case of operands
else if ((c >= 'a' && c <= 'z') || (c >= 'A'
&& c <= 'Z') || (c >= '0' && c <=
'9'))
r += c;
else // case of operators, operartors will be poped baed on
precedence
{
while (!stack.isEmpty() && Precedence(stack.topmost())
>= Precedence(c)) {
if (stack.topmost() == '(')
return "invalid expression";
else
r += stack.pop();
}
stack.push(c);
}
}
while (!stack.isEmpty()) {
if (stack.topmost() == '(')
return "invalid expression";
r += stack.pop();
}
return r;
}
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
while (true) {
int flag = 0;
System.out.println("press 1 to enter infix string, press 2 to
exit");
int n = ob.nextInt();
switch (n) {
case 1:
System.out.println("Enter the infix expression");
ob.nextLine();
String s = ob.nextLine();
String r = infixToPostfix(s);
if (r.equals("invalid expression"))
System.out.println(r);
else
System.out.println("Postfix Expression of given expression is " +
r);
break;
case 2:
flag = 1;
break;
default:
System.out.println("please enter avalid input");
}
if (flag == 1)
break;
System.out.println();
System.out.println();
}
}
static int Precedence(char ch) // function for deciding operator
precedence
{
int p = -1;
if (ch == '^') {
p = 1000;
} else if (ch == '*') {
p = 500;
} else if (ch == '/') {
p = 500;
} else if (ch == '+') {
p = 100;
} else if (ch == '-') {
p = 100;
}
return p;
}
}
--------------------
--------------------
sample output
--------------------
--------------------
Let me know, if you face any issue.