Java Programming
Stacks
Queues
Linked List
Programming Problems
Stack - Implementation. You will be able to use the push, pop and peek of Stack concept.
Post-Fix calculator - When an arithmetic expression is presented in the postfix form, you can use a stack to evaluate the expression to get the final value. For example: the expression 3 + 5 * 9 (which is in the usual infix form) can be written as 3 5 9 * + in the postfix. More interestingly, post form removes all parentheses and thus all implicit precedence rules.
Program Implementation Requirements
Use the stack concept to create a post-fix calculator.
LinkedList/Queue - Implementation.
New concert tickets are available. You have to enter the names of the people in order to form a line. However, you will later find out that a few people are not eligible to buy tickets because they represent scalpers instead of actual concert goers. You will have to remove those people from the line. A special raffle will be done and the result of the raffle will insert a person to the front of the line. You will have to issue tickets to the first 10 people in line, but there are 15 people total in line. The others will have to put into a waitlist queue for the next available ticket.
Program Implementation Requirements
Use the Linked List (Original Line) and a Queue (waitlist) concept to create this program. The class name will be ConcertTickets. You will have the following methods: insertIntoLine(Person p), removeFromLine(Person p), addToWaitList(Person p), assignedTicket().
.
Iam doing the first part of the question since the user has not asked for a specific question.
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
System.out.println(postfixCalc("3 5 9 * +")); // 48
System.out.println(postfixCalc("1 2 3 * + 4 +")); // 11
System.out.println(postfixCalc("5 2 4 * + 7 -")); // 6
System.out.println(postfixCalc("7 8 + 3 2 + /")); // 3
}
// Evaluates the given postfix expression string and returns the
result.
// condition is that: expression consists only of integers, +-*/,
and spaces in
// proper postfix format such as "3 5 9 * +"
public static int postfixCalc(String exp) {
Stack<Integer> s1 = new
Stack<Integer> ();
Scanner token = new
Scanner(exp);
while (token.hasNext()) {
if
(token.hasNextInt()) {
s1.push(token.nextInt());
} else {
int numb2 = s1.pop();
int numb1 = s1.pop();
String str = token.next();
if (str.equals("+")) {
s1.push(numb1 + numb2);
} else if (str.equals("-")) {
s1.push(numb1 - numb2);
} else if (str.equals("*")) {
s1.push(numb1 * numb2);
} else {
s1.push(numb1 / numb2);
}
// "+", "-",
"*", "/"
}
}
return s1.pop();
}
}
Output will be :
48
11
6
3
Get Answers For Free
Most questions answered within 1 hours.