-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathID4.cpp
More file actions
86 lines (75 loc) · 1.98 KB
/
ID4.cpp
File metadata and controls
86 lines (75 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
using namespace std; //Required Headers
struct node{
char data;
node *next;
node *pre;
};
//Structure to create a new node of Stack
bool isEmpty(node *TOP){
if(TOP==NULL){
return true;
}
else{return false;}
} //function to check if the stack is empty
void push(node *&TOP,node *&BOT,char data){
if(isEmpty(TOP)){
node *new_node = new node;
new_node->data=data;
new_node->pre=NULL;
new_node->next=NULL;
TOP=new_node;
BOT=new_node;
}
else{
node *new_node = new node;
new_node->data=data;
TOP->next=new_node;
new_node->next=NULL;
new_node->pre=TOP;
TOP=new_node;
}
} //function to inset elements in stack.
char pop(node *&TOP,node *&BOT){
if(isEmpty(TOP)){return 0;}
else if(TOP==BOT){
int data = TOP->data;
delete(TOP);
TOP=BOT=NULL;
return data;
}
else{
int data=TOP->data;
TOP=TOP->pre;
delete(TOP->next);
return data;
}
} //function to pop elements in stack.
int main(){
int T; //Number of test cases
string str; //String input;
cin>>T;
cin.ignore(100, '\n'); //To ignore the 'ENTER' pressed during cin being read as empty string;
while(T!=0){
T--;
node *TOP=NULL;
node *BOT=NULL;
getline(cin,str);
int len =str.length();
for(int i=0;i<len;i++){
char sym = str[i];
if(sym==')'){ // if closing brackets pop all elements while we encounter opening bracket in stack
char p= pop(TOP,BOT);
while(p!='('){
cout<<p;
p=pop(TOP,BOT);
}
}
else if((sym>=40&&sym<=47) || sym==94){ //if elements having ascii in between 40 to 47 that is '(' to '/' or ^ put in stack.
push(TOP,BOT,sym);
}
else{cout<<sym;}
}
cout<<endl;
}
}