KEMBAR78
Stacks in algorithems & data structure | PPT
AAllggoorriitthhmmss aanndd DDaattaa 
SSttrruuccttuurreess 
Ahmad Khan 
Lecture 4 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012
Stack 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
2
Objectives 
Understand all the aspects of a stack as data type including : 
Last In, First Out (LIFO) data access 
Push, Pop, and other stack operations 
Contiguous implementation of a stack 
Learn realization of stack using arrays(: Contiguous stack) 
Learn to choose appropriate realization suitable for practical applications 
Learn and implement the multi-stacks 
Use of stacks in expression conversion, recursion, reversing data and many 
more 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
3
 A stack is an ordered list in which all insertions and deletions 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
4 
are made at one end, called the top 
 In computer programming processing of function calls and 
their terminations uses stack. Stack is used to remember the 
place where the call was made; so that it can return there after 
the function is complete
Following are some examples in which where we need to use stacks 
are generally used: 
Handling function calls in programs very often restricts the access 
only at one end. In such implementation we need to use stacks. We 
can keep track of the return address to earlier function after 
furnishing/finishing a function call using stacks 
If we intend to store a group of data together in a sequential 
manner in computer’s memory, then arrays can be one of the 
possible data structures. 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
5
Arrays 
 An array is a finite ordered collection of homogeneous data 
elements which provides direct access (or random access) to any 
of its elements. 
 An array as a data structure is defined as a set of pairs (index, 
value) such that with each index a value is associated. 
index — indicates the location of an element in an array. 
value - indicates the actual value of that data element. 
 Declaration of an array in ‘C++’: 
int Array_A[20]; 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
6
Examples of Stacks 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
7 
Following figure shows some examples of stack 
1.Stack of Book 
2.Stack of chairs 
3.Stack of cups
Primitive Operations 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
8 
1) The Stack Full Condition (Stack Capacity =3)
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
9 
2) The Pop Operation
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
10 
3) The Empty Stack
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
11 
4) The Get Top Operation
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
12 
Representation Of Stack S Using Sequential 
Organization (Arrays)
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
13 
……….. C B A 
top 
 The elements are stored in the stack from the first location onwards 
 The first element is stored at 0th location of the array ‘Stack’ which means at 
STACK[0], the second element at STACK[1], ith element at Stack [i–1], and nth 
element at Stack[n–1] 
 Associated with the array will be an integer variable, top, which points to the top 
element in the stack 
 The initial value of top is −1, when the stack is empty 
 It can hold elements from index 0 and maximum it can grow up to n−1 as this is the 
static stack using arrays
14 
Implementation of Stack 
Stack Class 
class STACK 
{ 
private: 
int Stack[50]; 
int MaxCapacity; 
int top; 
public: 
STACK( ) { MaxCapacity= 50; top=-1;} 
int GetTop( ); 
int Pop( ); 
void Push( int Element); 
int Empty( ); 
int CurrSize( ); 
int IsFull (); 
}; 
Data Structures in C++ by 
Dr. Varsha Patil Oxford University Press © 2012 14
15 
Implementation of Stack 
GetTop, Pop, Empty 
int STACK :: GetTop () 
{ 
if (!Empty()) 
return (Stack[top]); 
} 
int STACK :: Pop () 
{ 
if (!Empty()) 
return (Stack[top--]); 
} 
int STACK :: Empty () 
{ 
if (top==-1) 
return 1; 
else 
return 0; 
} 
Data Structures in C++ by 
Dr. Varsha Patil Oxford University Press © 2012 15
16 
Implementation of Stack 
IsFull, CurrSize, Push 
int STACK :: IsFull () 
{ 
if (top==MaxCapacity-1) 
return 1; 
else 
return 0; 
} 
int STACK :: CurrSize() 
{ 
return(top+1); 
} 
void STACK :: Push (int Element) 
{ 
if (!IsFull()) 
Stack[++top]=Element; 
} 
Data Structures in C++ by 
Dr. Varsha Patil Oxford University Press © 2012 16
STACK S; 
S.Push(1); 
S.Push(2); 
cout<<"t Top element of stack = "<<S.GetTop()<<endl; 
cout<<"t After pop = "<<S.Pop()<<endl; 
cout<<"t After pop = "<<S.Pop()<<endl; 
getch(); 
17 
Implementation of Stack 
main 
int main( ) 
{ 
} 
Data Structures in C++ by 
Dr. Varsha Patil Oxford University Press © 2012 17

Stacks in algorithems & data structure

  • 1.
    AAllggoorriitthhmmss aanndd DDaattaa SSttrruuccttuurreess Ahmad Khan Lecture 4 Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012
  • 2.
    Stack Oxford UniversityData Structures Using C++ by Dr Varsha Patil Press © 2012 2
  • 3.
    Objectives Understand allthe aspects of a stack as data type including : Last In, First Out (LIFO) data access Push, Pop, and other stack operations Contiguous implementation of a stack Learn realization of stack using arrays(: Contiguous stack) Learn to choose appropriate realization suitable for practical applications Learn and implement the multi-stacks Use of stacks in expression conversion, recursion, reversing data and many more Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 3
  • 4.
     A stackis an ordered list in which all insertions and deletions Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 4 are made at one end, called the top  In computer programming processing of function calls and their terminations uses stack. Stack is used to remember the place where the call was made; so that it can return there after the function is complete
  • 5.
    Following are someexamples in which where we need to use stacks are generally used: Handling function calls in programs very often restricts the access only at one end. In such implementation we need to use stacks. We can keep track of the return address to earlier function after furnishing/finishing a function call using stacks If we intend to store a group of data together in a sequential manner in computer’s memory, then arrays can be one of the possible data structures. Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 5
  • 6.
    Arrays  Anarray is a finite ordered collection of homogeneous data elements which provides direct access (or random access) to any of its elements.  An array as a data structure is defined as a set of pairs (index, value) such that with each index a value is associated. index — indicates the location of an element in an array. value - indicates the actual value of that data element.  Declaration of an array in ‘C++’: int Array_A[20]; Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 6
  • 7.
    Examples of Stacks Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 7 Following figure shows some examples of stack 1.Stack of Book 2.Stack of chairs 3.Stack of cups
  • 8.
    Primitive Operations OxfordUniversity Data Structures Using C++ by Dr Varsha Patil Press © 2012 8 1) The Stack Full Condition (Stack Capacity =3)
  • 9.
    Oxford University DataStructures Using C++ by Dr Varsha Patil Press © 2012 9 2) The Pop Operation
  • 10.
    Oxford University DataStructures Using C++ by Dr Varsha Patil Press © 2012 10 3) The Empty Stack
  • 11.
    Oxford University DataStructures Using C++ by Dr Varsha Patil Press © 2012 11 4) The Get Top Operation
  • 12.
    Oxford University DataStructures Using C++ by Dr Varsha Patil Press © 2012 12 Representation Of Stack S Using Sequential Organization (Arrays)
  • 13.
    Oxford University DataStructures Using C++ by Dr Varsha Patil Press © 2012 13 ……….. C B A top  The elements are stored in the stack from the first location onwards  The first element is stored at 0th location of the array ‘Stack’ which means at STACK[0], the second element at STACK[1], ith element at Stack [i–1], and nth element at Stack[n–1]  Associated with the array will be an integer variable, top, which points to the top element in the stack  The initial value of top is −1, when the stack is empty  It can hold elements from index 0 and maximum it can grow up to n−1 as this is the static stack using arrays
  • 14.
    14 Implementation ofStack Stack Class class STACK { private: int Stack[50]; int MaxCapacity; int top; public: STACK( ) { MaxCapacity= 50; top=-1;} int GetTop( ); int Pop( ); void Push( int Element); int Empty( ); int CurrSize( ); int IsFull (); }; Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 14
  • 15.
    15 Implementation ofStack GetTop, Pop, Empty int STACK :: GetTop () { if (!Empty()) return (Stack[top]); } int STACK :: Pop () { if (!Empty()) return (Stack[top--]); } int STACK :: Empty () { if (top==-1) return 1; else return 0; } Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 15
  • 16.
    16 Implementation ofStack IsFull, CurrSize, Push int STACK :: IsFull () { if (top==MaxCapacity-1) return 1; else return 0; } int STACK :: CurrSize() { return(top+1); } void STACK :: Push (int Element) { if (!IsFull()) Stack[++top]=Element; } Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 16
  • 17.
    STACK S; S.Push(1); S.Push(2); cout<<"t Top element of stack = "<<S.GetTop()<<endl; cout<<"t After pop = "<<S.Pop()<<endl; cout<<"t After pop = "<<S.Pop()<<endl; getch(); 17 Implementation of Stack main int main( ) { } Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 17