AMERICAN INTERNATIONAL UNIVERSITY-BANGLADESH
CSC 2106: : DATA STRUCTURE (THEORY)
Lecture: # 9 Week: # 5 Semester: Fall 2023-2024
Stack
Instructor: Dr. M. Firoz Mridha, Associate Professor
Department of Computer Science, Faculty of Science & Technology.
[email protected]. Room no: DN0116
Lecture Outline
Stack
Getting Top Value
Definition
Showing All Elements
Implementation in C++: Operations
& More Dynamic Stack
Checking for Underflow Object Oriented Approach
Checking for Overflow Generic Stack
Adding Elements
Books
Removing Elements
References
Topics discussed: 1) The definition of Stacks. 2)
Some real-life examples of Stacks. 3) Stack as an
ADT. 4) Primary Stack operations: i. push (data) ii.
pop () iii. top ()
Stack
Definition
A stack or LIFO (last in, first out) is an abstract data type that serves as a
collection of elements, with two principal operations:
push adds an element to the collection;
pop removes the last (top of the stack) element that was added.
Bounded capacity
If the stack is full and does not contain enough space to accept an entity
to be pushed, the stack is then considered to be in an overflow state.
A pop either reveals previously concealed items or results in an empty
stack – which means no items are present in stack to be removed.
Non-Bounded capacity
Dynamically allocate memory for stack. No overflow.
Implementation in C++: Operations & More
int Stack[100], Top=0, MaxSize=100;//Stack[] holds
the elements; Top is the index of Stack[] always
holding the whereabouts of the first/top element of
the stack
bool isEmpty(); //returns True if stack has no element
bool isFull(); //returns True if stack full
bool push(int Element); //inserts Element at the top of
the stack
bool pop(); //deletes top element from stack into
Element
int topElement(); //gives the top element in Element
void show(); //prints the whole stack
Checking for Underflow
bool isEmpty(){
/*returns True if stack is empty*/
return (Top == 0);
}
7 Considering MaxSize = 7
6
Top 0
Checking for Overflow
bool isFull(){
/*returns True if stack is full*/
return (Top == MaxSize);
}
Top 7 Considering MaxSize = 7
6 17
5 16
4 15
3 14
2 13
1 12
0 11
Adding Elements
bool push(int Element ){
// inserts Element at the top of the stack
if( isFull( ) ) { cout << "Stack is Full\n"; return
false; }
// push element if there is space
Stack[ Top ] = Element;
Top++;
return true;
}
7 Considering MaxSize = 7
6
There are 3 elements inside Stack, So
5 next element will be pushed at index 3
Top 4
Top 3 14
2 13
1 12
0 11
Removing Elements
bool pop(){
// removes top element from stack and puts it in
if( isEmpty() ) { cout << "Stack empty\n"; return false; }
Top--;
return true;
}
7
Considering MaxSize = 7
6
There are 4 elements inside Stack, So
5 element will be popped from index 3
Top 4
Top 3 14
2 13
1 12
0 11
Getting Top Value
int topElement(){
// gives the top element
return Stack[ Top - 1 ];
}
7 Considering MaxSize = 7
6 There are 4 elements inside Stack,
So top element will be at index 3
5
Top 4
3 14
2 13
1 12
0 11
Showing All Elements
void show(){
//prints the whole stack from top to bottom
if(isEmpty()) { cout << "Stack empty\n";
return; }
for( int i=Top-1; i>=0; i-- ) cout << Stack[i] <<
endl; 7
} Considering MaxSize = 7
6
There are 4 elements inside Stack,
5 So top element will be at index 3
Top 4
3 14
2 13
1 12
0 11
Object Oriented Approach
class MyStack{
int Stack[100], Top, MaxSize;
public:
//Initializing stack
MyStack( int Size = 100 ){ MaxSize = Size; Top = 0;}//
constructor
bool isEmpty();
bool isFull();
bool push(int Element);
bool pop();
int topElement();
void show();
};
Dynamic Stack
class MyStack{
int *Stack, Top, MaxSize;
public:
MyStack(int);
~MyStack();//destructor
bool isEmpty();
bool isFull();
bool push(int Element);
bool pop();
bool topElement();
void show();
void resize( int size); //Resize the stack
};
Dynamic Stack: Constructor & Destructor
The Constructor will create the array dynamically, Destructor will release it.
MyStack::MyStack( int Size = 100 ){
MaxSize = Size; //get Size
Stack = new int[MaxSize]; //create array accordingly
Top = 0; //start the stack
}
MyStack::~MyStack(){
delete [] Stack; //release the memory for stack
}
Dynamic Stack: Resizing in runtime
Resize creates a new array dynamically, copies all the element from the previous
stack, releases the old array, and makes the pointer Stack point to the new array.
By default increase 100, user can define the additional size. Use negative size to
decrease the array.
void resize( int Size = 100 ){
//creates a new stack with a new capacity, MaxSize + Size
int *tempStk = new int[ MaxSize + Size ];// dynamic memory
allocation
//copy the elements from old to new stack
for( int i=0; i<MaxSize; i++ ) tempStk[i] = Stack[i];
MaxSize += Size; //MaxSize increases by Size
delete [] Stack; //release the old stack
Stack = tempStk; //assign Stack with new stack
}
void push( int Element ){
//inserts Element at the top of the stack
if( isFull( ) ) resize( ); //increase size if full
Stack[ Top++ ] = Element;
Generic Stack
template <typename T>
class MyStack{
T *Stack;
int Top, MaxSize;
public:
MyStack( int );
~MyStack();
bool isEmpty();
bool isFull();
bool push(T);
bool pop();
bool topElement();
void show();
void resize(int); //resize the stack
};
Books
“Schaum's Outline of Data Structures with C++”. By John R. Hubbard
“Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
“Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
“Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
“Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
“C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
“Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,
References
“Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008.
[Chapter 1: 1.1]
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
https://www.cs.usfca.edu/~galles/visualization/StackArray.html (This is a great site
for visualizing stack operations)