Skip to content

Commit 010a654

Browse files
committed
#Modification 15
1 parent 4e69287 commit 010a654

10 files changed

Lines changed: 489 additions & 0 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: Stack_Queue_Problem_03","url":"c:\\Users\\AmanSoni\\Desktop\\Java Program's\\Java-Eclipse\\Placement Prepration\\src\\stack_and_queue\\Stack_Queue_Problem_03.java","tests":[{"id":1609222893914,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\AmanSoni\\Desktop\\Java Program's\\Java-Eclipse\\Placement Prepration\\src\\stack_and_queue\\Stack_Queue_Problem_03.java","group":"local","local":true}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package stack_and_queue;
2+
import java.util.*;
3+
4+
public class Infix_To_Postfix {
5+
6+
static int Prec(char ch){
7+
switch(ch){
8+
case '+':
9+
case '-':
10+
return 1;
11+
12+
case '*':
13+
case '/':
14+
return 2;
15+
16+
case '^':
17+
return 3;
18+
}
19+
20+
return -1;
21+
}
22+
23+
static String infixToPostfix(String exp){
24+
25+
String result = new String(" ");
26+
Stack<Character> stack = new Stack<>();
27+
28+
for(int i = 0 ; i < exp.length() ; ++i){
29+
char c = exp.charAt(i);
30+
31+
if(Character.isLetterOrDigit(c))
32+
result += c;
33+
34+
else if(c == '(')
35+
stack.push(c);
36+
37+
else if (c == ')'){
38+
while (!stack.isEmpty() && stack.peek() != '(')
39+
result += stack.pop();
40+
41+
stack.pop();
42+
}
43+
44+
else{
45+
while(!stack.isEmpty() && Prec(c) <= Prec(stack.peek())){
46+
result += stack.pop();
47+
}
48+
}
49+
50+
stack.push(c);
51+
}
52+
53+
while (!stack.isEmpty()) {
54+
55+
if(stack.peek() == '(')
56+
return "Invalid Expression";
57+
58+
result += stack.pop();
59+
}
60+
61+
return result;
62+
}
63+
64+
public static void main(String[] args) {
65+
66+
Scanner sc = new Scanner(System.in);
67+
String exp = sc.nextLine();
68+
69+
@SuppressWarnings("unused")
70+
Infix_To_Postfix post = new Infix_To_Postfix();
71+
72+
System.out.println(infixToPostfix(exp));
73+
sc.close();
74+
}
75+
76+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package stack_and_queue;
2+
import java.util.Scanner;
3+
import java.util.Stack;
4+
5+
public class Parenthesis_Checker_Problem {
6+
7+
@SuppressWarnings("resource")
8+
public static void main(String[] args) {
9+
10+
Scanner sc = new Scanner(System.in);
11+
int t = sc.nextInt();
12+
while(t-- != 0) {
13+
14+
String s = sc.nextLine();
15+
Stack<Character> stack = new Stack<>();
16+
17+
boolean isBalanced = true;
18+
19+
for(int i = 0 ; i < s.length() ; i++) {
20+
21+
char ch = s.charAt(i);
22+
if(ch == '(' || ch == '{' || ch == '[') {
23+
stack.push(ch);
24+
continue;
25+
}
26+
27+
if(stack.isEmpty()) {
28+
isBalanced = false;
29+
break;
30+
}
31+
32+
if(ch == ')') {
33+
if(stack.peek() == '(') {
34+
stack.pop();
35+
}else {
36+
isBalanced = false;
37+
break;
38+
}
39+
}
40+
41+
if(ch == '}') {
42+
if(stack.peek() == '{') {
43+
stack.pop();
44+
}else {
45+
isBalanced = false;
46+
break;
47+
}
48+
}
49+
50+
if(ch == ']') {
51+
if(stack.peek() == '[') {
52+
stack.pop();
53+
}else {
54+
isBalanced = false;
55+
break;
56+
}
57+
}
58+
59+
}
60+
61+
if(!stack.isEmpty()) {
62+
isBalanced = false;
63+
}
64+
65+
if(isBalanced) {
66+
System.out.println("isBalanced");
67+
}else {
68+
System.out.println("not balanced");
69+
}
70+
71+
}
72+
73+
}
74+
75+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package stack_and_queue;
2+
import java.util.*;
3+
4+
/*
5+
* Implement Stack from Scratch Array Implementation
6+
*/
7+
public class Stack_Queue_Problem_01_i<Item> implements Iterable<Item> {
8+
9+
@SuppressWarnings("unchecked")
10+
private Item[] a = (Item[]) new Object[1]; // Stack Items
11+
private int N = 0; // number of items
12+
13+
public boolean isEmpty() {
14+
return N == 0;
15+
}
16+
17+
public int size() {
18+
return N;
19+
}
20+
21+
private void resize(int max) {
22+
@SuppressWarnings("unchecked")
23+
Item[] temp = (Item[]) new Object[max];
24+
for (int i = 0; i < N; i++)
25+
temp[i] = a[i];
26+
a = temp;
27+
}
28+
29+
public void push(Item item) {
30+
if (N == a.length)
31+
resize(2 * a.length);
32+
a[N++] = item;
33+
34+
}
35+
36+
public Item pop() {
37+
Item item = a[--N];
38+
a[N] = null;
39+
40+
if (N > 0 && N == a.length / 4)
41+
resize(a.length / 2);
42+
43+
return item;
44+
}
45+
46+
public Iterator<Item> iterator() {
47+
return new ReverseArrayIterator();
48+
}
49+
50+
private class ReverseArrayIterator implements Iterator<Item> {
51+
public int i = N;
52+
53+
public boolean hasNext() {
54+
return i > 0;
55+
}
56+
57+
public Item next() {
58+
return a[--i];
59+
}
60+
61+
public void remove() {
62+
63+
}
64+
}
65+
66+
public static void main(String[] args) {
67+
68+
}
69+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package stack_and_queue;
2+
import static java.lang.System.exit;
3+
4+
//Java program to Implement a stack using singly linked list
5+
6+
//Create Stack Using Linked list
7+
class StackUsingLinkedlist {
8+
9+
// A linked list node
10+
private class Node {
11+
12+
int data; // integer data
13+
Node link; // reference variable Node type
14+
}
15+
16+
// create global top reference variable global
17+
Node top;
18+
19+
// Constructor
20+
StackUsingLinkedlist() {
21+
this.top = null;
22+
}
23+
24+
// Utility function to add an element x in the stack
25+
public void push(int x) // insert at the beginning
26+
{
27+
// create new node tempt and allocate memory
28+
Node temp = new Node();
29+
30+
// initialize data into data field
31+
temp.data = x;
32+
33+
// put top reference into link
34+
temp.link = top;
35+
36+
// update top reference
37+
top = temp;
38+
}
39+
40+
// Utility function to check if the stack is empty or not
41+
public boolean isEmpty() {
42+
return top == null;
43+
}
44+
45+
// Utility function to return top element in a stack
46+
public int peek() {
47+
// check for empty stack
48+
if (!isEmpty()) {
49+
return top.data;
50+
} else {
51+
System.out.println("Stack is empty");
52+
return -1;
53+
}
54+
}
55+
56+
// Utility function to pop top element from the stack
57+
public void pop() // remove at the beginning
58+
{
59+
// check for stack underflow
60+
if (top == null) {
61+
System.out.print("\nStack Underflow");
62+
return;
63+
}
64+
65+
// update the top pointer to point to the next node
66+
top = (top).link;
67+
}
68+
69+
public void display() {
70+
// check for stack underflow
71+
if (top == null) {
72+
System.out.printf("\nStack Underflow");
73+
exit(1);
74+
} else {
75+
Node temp = top;
76+
while (temp != null) {
77+
78+
// print node data
79+
System.out.printf("%d->", temp.data);
80+
81+
// assign temp link to temp
82+
temp = temp.link;
83+
}
84+
}
85+
}
86+
}
87+
88+
//main class
89+
public class Stack_Queue_Problem_01_ii {
90+
public static void main(String[] args) {
91+
// create Object of Implementing class
92+
StackUsingLinkedlist obj = new StackUsingLinkedlist();
93+
// insert Stack value
94+
obj.push(11);
95+
obj.push(22);
96+
obj.push(33);
97+
obj.push(44);
98+
99+
// print Stack elements
100+
obj.display();
101+
102+
// print Top element of Stack
103+
System.out.printf("\nTop element is %d\n", obj.peek());
104+
105+
// Delete top element of Stack
106+
obj.pop();
107+
obj.pop();
108+
109+
// print Stack elements
110+
obj.display();
111+
112+
// print Top element of Stack
113+
System.out.printf("\nTop element is %d\n", obj.peek());
114+
}
115+
}

0 commit comments

Comments
 (0)