You are on page 1of 19

Stack

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 3.1 Week No: 3 Semester: Fall 20_21


Lecturer: Kaniz Fatema, Assistant Professor , kaniz.fatema@aiub.edu
Lecture Outline

1. Stack
i. Definition ix. Dynamic Stack
ii. Implementation in C++: Operations & More x. Object Oriented Approach
iii. Checking for Underflow xi. Generic Stack
iv. Checking for Overflow 2. Books
3. References
v. Adding Elements
vi. Removing Elements
vii. Getting Top Value
viii. Showing All Elements
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


int A[5], top=0;
If(isFull()) {}
A[top++] = element;
// A[top]=element; top ++;
1. Push 10 in the stack.
2. Push 12 in the stack.
For(i=Top-1;i>=0;i--){
3. Push 14 in the stack.
Cout<<A[i];
4. Push 16 in the stack.
}
5. Push 18 in the stack.
6. Push 20 in the stack.
Top--;
7. Push 22 in the stack.
Top=0 8. Push 24 in the stack.
A[4] 18 9. Pop element
10. Pop element
A[3] 16 11. Pop element
12. Push 26 stack
A[2] 26 13. Pop element
14. Pop element
A[1] 12 15. Pop element
16. Push 28
A[0] 28 17. Pop element
18. Pop element
10 20 30 40 50 60
10 20 40 50 60 60
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;
//Stack[Top] = Element; Top++;
return true;
} 7 Considering MaxSize = 7
There are 3 elements inside Stack
6
So next element will be pushed at index 3
5

Top 4

Top 3 14
2 13

1 12

0 11
Removing Elements

void pop(){
// removes top element from stack and puts it in
if( isEmpty() ) { cout << "Stack empty\n";}
else {Stack[Top]=0;
Top--;}
}
7 Considering MaxSize = 7
6 There are 4 elements inside Stack
So element will be popped from index 3
5

Top 4

Top 3 14
2 13

1 12

0 11
A[0] A[1] A[2] A[3] A[4] A[5] A[6]
100 101 102 103 104

A[0] A[1] A[2] A[3] A[4] A[5] A[6]


100 101 102 103 104

A[0] A[1] A[2] A[3] A[4] A[5] A[6]


102 103 104
Getting Top Value
int topElement(){
// gives the top element
return Stack[ Top - 1 ];
}

7 Considering MaxSize = 7
There are 4 elements inside Stack
6 So top element will be at index 3

5
Top
4 15

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
So top element will be at index 3
5

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;}
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();
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];
Top = 0; //start the stack
}

MyStack::~MyStack(){
delete [] Stack; //release the memory for stack
}
Dynamic Stack: Runtime Resizing
 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 ];
//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;
}
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)

You might also like