Professional Documents
Culture Documents
LEC - 2 Stack
LEC - 2 Stack
*Stack
*Basic principles
*Operation of stack
*Implementation Stack using Array
*Applications of stack
What is a stack?
push
pop STACK
isEmpty
4
isFull
Push (ItemType newItem) Pop (ItemType& item)
Function: Adds newItem to Function: Removes topItem from
the top of the stack. stack.
Preconditions: Stack has Preconditions: Stack has been
been initialized and is not initialized and is not empty.
full. Postconditions: Top element has
Postconditions: newItem is at been removed from stack.
the top of the stack.
Stack overflow
The condition resulting from trying to push an element onto a full stack.
if(!stack.IsFull())
stack.Push(item);
Stack underflow
The condition resulting from trying to pop an empty stack.
if(!stack.IsEmpty())
stack.Pop(item);
Common Uses for Stacks
Stacks are one of the most USEFUL data structures in Computer Science.
9
void push(int n) void pop()
{
if (full()) {
{ if(empty())
cout<< "stack is full\n"; {
} cout<<"stack is empty\n";
else }
{ else
data[++top]=n; {
} cout<< data [top--];
}
}
}
Push:
•Increment the variable top so that Pop:
it can refer to the next memory •Decrement the value of the top
allocation •Return the topmost element
•Copy the item to the array index
value equal to the top
•Repeat step 1 and 2 until stack
overflows
1- Balanced brackets check Application
Check that all brackets match:
●Every opening bracket has a closing bracket
●Every closing bracket has an opening bracket
●Nested brackets match up: no!”])[(“
bool balanced(string exp)
{
stack open_brakets; bool pair(char open,char close)
for (int i=0; i< exp.length();i++) {
{
if (exp[i]=='(' ||exp[i]=='{'||exp[i]=='[') if (open=='(' && close==')') return true;
open_brakets.push(exp[i]); else if (open=='{' && close=='}') return true;
else if (open=='[' && close==']') return true;
else if(exp[i]==')' ||exp[i]=='}'||exp[i]==']') else return false;
{ }
if (open_brakets.empty())
return false;
int top_val()
else if (pair(open_brakets.top_val(), exp[i])== false)
return false; {
if (empty()) return -1;
open_brakets.pop(); return data[top];
}
} }
if (open_brakets.empty()) return true;
else return false;
}
2- Infix to postfix Conversion
Since people are more used to
Stacks can also be used to convert infix expressions infix notation…
to postfix expressions:
You can let the user type in an
Postfix notation is another way of infix expression…
EX : (3+2)+7/2 * ((7+3)*2)
stack
OUTPUT: 32+72/73+2**+
string infix_to_postfix(string exp)
int priority(char c)
{ while(!stk.empty()&& priority(exp[i])
{
stack stk; <= priority(stk.top()))
if (c=='-' || c=='+')
string output=""; {
return 1;
for(int i=0; i<exp.length();i++) output +=stk.top();
else if (c=='*' || c=='/')
{ stk.pop();
return 2;
if(exp[i]==' ') continue; }
else if (c=='^')
if (isdigit (exp[i])||isalpha(exp[i])) stk.push(exp[i]);
return 3;
output += exp[i]; }
else
else if(exp[i]=='(') }
return 0;
stk.push('(');
}
else if (exp[i] ==')')
{ while(!stk.empty())
int main() while(stk.top()!= '(') {
{ { output +=stk.top();
string infixexpression= output +=stk.top(); stk.pop();
("(3+2)+7/2*((7+3)*2)"); stk.pop(); }
cout<< infix_to_postfix } return output;
(infixexpression)<<endl; stk.pop();// remove '(' }
return 0; }
} else
{
Postfix Evaluation Algorithm
Inputs: postfix expression string
Output: number representing answer