-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.txt
More file actions
158 lines (133 loc) · 2.47 KB
/
Copy pathstack.txt
File metadata and controls
158 lines (133 loc) · 2.47 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
Stack:
------
LIFO
top - used to point the index of topmost element in stack
top = -1 (initial)
Function:
---------
PUSH() -insert
1) if(top==size-1)
print "stack overflow"
2) else top++
3) stack[top]=value
POP() -remove
1) if(top==-1)
print "Underflow"
2) else top--
PEEK() - top element
Exception:
Overflow - trying to PUSH when stack is full
Underflow -trying to POP when stack is empty
Implementation using array:
---------------------------
#include <iostream>
using namespace std;
int stack[5], n=5, top=-1;
void push(int val) {
if(top==n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top==-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
if(top==-1)
print balanced
else
print not balanced
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
Linked List imple:
-------------------
push() - insertion at front
pop() - Deletion at front
PUSH()
------
1) create node
2) nn ->data =value
3) nn ->addr = NULL
4) if(top==NULL)//0 node
top=nn
else
nn->addr = top
top=nn
POP()
-----
1) if(top==NULL)
print "Underflow"
2) else
top=top->addr
Infix to postfix:
--------------------
#include<iostream>
#include<ctype.h>
using namespace std;
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
cin>>exp;
cout<<"\n";
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
cout<<*e;
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
cout<<x;
}
else
{
while(priority(stack[top]) >= priority(*e))
cout<<pop();
push(*e);
}
e++;
}
while(top != -1)
{
cout<<pop()<<" ";
}return 0;
}