You are on page 1of 4

Lecture Notes: 6

Introduction As an abstract data type, the stack is a container and has two basic operations: push and pop. Push adds a given node to the top of the stack leaving previous nodes below. Pop removes and returns the current top node of the stack. A frequently used metaphor is the idea of a stack of plates in a spring loaded cafeteria stack. In such a stack, only the top plate is visible and accessible to the user, all other plates remain hidden. As new plates are added (pushed), each new plate becomes the top of the stack, and hides each plate below. As plates are removed (popped) from the stack, they can be used, and second plate becomes the top of the stack. Two important principles are illustrated by this metaphor, the Last In First Out principle is one. The second is that the contents of the stack are hidden. Only the top plate is visible, so to see what is on the third plate, the first and second plates will have to be removed. Operations The stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top can return the current top element of the stack without removing it from the stack. Implementation /******File Name: DS.H*******/ #include<iostream.h> #include<conio.h> #include<stdlib.h> #define MaxSize 10

class Stack { private: int Array[MaxSize]; int Top; public: Stack() { Top = -1; } void Push(int); int Pop(); void Display(); };

Abstract Data Type Stack

Notes Compiled for III Sem B.Tech CSE C & D batch

Page 1

Lecture Notes: 6

/***********Implementation of ADT Stack************/

void Stack::Push(int Data) { if(Top < MaxSize-1) Array[++Top] = Data; else { cout<<"\n\t Stack is full..."; getch(); } } int Stack::Pop() { if(Top>=0) return Array[Top--]; else { return -1; } } void Stack::Display() { if(Top<0) cout<<"\n\tStack Empty..."; else { cout<<"\n\tElements in stack are : \n\n"; for(int i=Top;i>=0;i--) { cout<<"\t"<<Array[i]<<"\n"; } } } Stack as data type Stack.cpp #include "DS.h" void main() { Stack s; int Choice,Data; do { clrscr(); cout<<"Menu\n~~~~\n1.Push\n2.Pop\n3.Display\n4.Exit\n\tchoice: "; cin>>Choice; switch(Choice) {

Notes Compiled for III Sem B.Tech CSE C & D batch

Page 2

Lecture Notes: 6

case 1: cout<<"\nEnter the data to Push: "; cin>>Data; s.Push(Data); break; case 2: int Res = s.Pop(); if(Res>=0) cout<<"\n\t"<<Res<<" Popped out"; else cout<<"\n\tStack empty..."; getch(); break; case 3: s.Display(); getch(); break; case 4: cout<<"\n\tApplication closing.. \n\tPress any key to exit..."; getch(); break; default: cout<<"\n\tError in choice...\n\tEnter correct choice.."; getch(); break; } }while(Choice!=4); } Applications of Stack Balancing Braces Compilers make use of stack structures. This example shows a simple illustration of the way a stack can be used to check the syntax of a program. It uses the Stack class you have created. In the example, a Stack is used to check that the braces { and } used to mark out code blocks in a C++ source file are matched i.e. that there are the same number of { as }. The source file is read character by character. Each time a { is read an object (any object, it doesnt matter what) is pushed onto the stack. Each time a } is read an object is popped from the stack. If the stack is empty when a } is read then there must be a missing { . If the stack is not empty at the end of the file then there must be a missing } Algorithm to convert Infix to postfix Create an empty stack of operators. While no error has occurred and the end of the infix expression has not been reached, do the following: 1. Get the next input Token (constant, variable, arithmetic operator, left and right parenthesis) in the infix expression. 2. If Token is (i) a left parenthesis: Push it onto the stack
Notes Compiled for III Sem B.Tech CSE C & D batch

Page 3

Lecture Notes: 6

(ii) a right parenthesis: Pop and display stack elements until a left parenthesis is popped, but do not display it. (It is an error if the stack becomes empty with no left parenthesis found.) (iii) an operator: If the stack is empty or Token has a higher priority than the top stack, push Token onto the stack. Otherwise, pop and display the top stack element, then repeat the comparison of Token with the new top stack item. A left parenthesis in the stack is assumed to have a lower priority than that of operators. (iv) an operand: Display it. 3. When the end of the infix expression is reached, pop and display stack items until the stack is empty. Example (7+1)*(6+(3-2)) TOKEN ( 7 + 1 ) * ( 6 + ( 3 2 ) ) Stack After Operation ( ( (+ (+ Display 7 7 71 71+ 71+ 71+ 71+6 71+6 71+6 71+63 71+63 71+632 71+63271+632-+

* *( *( *(+ *(+( *(+( *(+(*(+(*(+ * 7 1 + 6 3 2 - + displayed Stack content * Add that to display 7 1 + 6 3 2 - + * Will be displayed as Postfix Expression

Notes Compiled for III Sem B.Tech CSE C & D batch

Page 4