03 stacks and_queues_using_arrays
DESCRIPTION
TRANSCRIPT
![Page 1: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/1.jpg)
Stacks• Stack is a data structure that can be
used to store data which can later be retrieved in the reverse or last in first out (LIFO) order.
• Stack is an ordered-list in which all the insertions and deletions are made at one end to maintain the LIFO order. (Stack is LIFO data structure)
![Page 2: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/2.jpg)
![Page 3: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/3.jpg)
Stacks• The operations defined on a stack are:
• Push - Store onto a stack• Pop - retrieve from stack• Top - examine the top element in
the stack• Is_empty - check if the stack is empty• Is_Full - check if the stack is full
• A stack can be very easily implemented using arrays.
• Can also be implemented using linked list • Stack is implemented by maintaining a pointer
to the top element in the stack. This pointer is called the stack pointer.
• Linked implementation will be discussed later.
![Page 4: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/4.jpg)
Stacks – Array Implementation
If a stack is implemented using arrays, the following two conventions can be used:
• A stack can grow upwards, i.e., from index 0 to the maximum index, or it can grow downwards, i.e., from the maximum index to index 0.
• Stack pointer can point to the last element inserted into the stack or it can point to the next available position.
![Page 5: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/5.jpg)
6
8
3
12
6
5
4
3
2
1
0
7
8
9
Growing Downwards Initial state: stk_ptr = MAX - 1
• stk_ptr points to the next empty location
• Pop – first increment stk_ptr and then take data out
• Push – first add data to the stack, then decrement stk_ptr
![Page 6: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/6.jpg)
6
8
3
12
6
5
4
3
2
1
0
7
8
9
Growing Downwards Initial state: stk_ptr = MAX
• stk_ptr points to the last element added to the stack
• Pop – first take data out and then increment stk_ptr
• Push – first decrement the stk_ptr and then add data
![Page 7: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/7.jpg)
6
4
5
2
7
6
5
4
3
2
1
0
7
8
9
Growing Upwards Initial state: stk_ptr = 0
• Push – first add data to the stack then increment stk_ptr
• Pop – first decrement stk_ptr and then take data out
• stk_ptr points to the next empty location
![Page 8: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/8.jpg)
6
4
5
2
7
6
5
4
3
2
1
0
7
8
9
Growing Upwards Initial state: stk_ptr = -1
• Push – first increment the stk_ptr and then add data
• Pop – first take data out and then decrement stk_ptr
• stk_ptr points to the last element added to the stack
![Page 9: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/9.jpg)
Stacks – Array Implementation
class Stack {
private:
int maxSize; // maximum storage
capacity
int stk_ptr; // stack pointer
int *stackArray; // array used to implement stack
public:
Stack(int s ); // constructor
~Stack() {delete [ ] stackArray; } // destructor
bool push (int); // add an element to the stack
bool pop(int &); // remove an element from stack
bool isFull(); // check if the stack is full
bool isEmpty(); // check if the stack is empty
};
![Page 10: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/10.jpg)
bool Stack::push(int n)
{
if (! isFull() ) {
stackArray[stk_ptr] = n;
stk_ptr = stk_ptr + 1;
return true;
}
else return false;
}
bool Stack::pop(int &n)
{
if (! IsEmpty ()) {
stk_ptr = stk_ptr – 1;
n = stackArray[stk_ptr];
return true;
}
else return false;
}
bool Stack::isEmpty()
{
return (stk_ptr == 0);
}
Stack::Stack(int s)
{
maxSize = s;
stk_ptr = 0;
stackArray = new int[maxSize];
}bool Stack::isFull()
{
return (stk_ptr == maxSize);
}
![Page 11: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/11.jpg)
Applications of stack
• Is used in recursion
• Used in expression evaluation
• Used for string comparisons
• Used in function calling
• Used in memory manipulation to store addresses
• Used in tree manipulation
• etc.
![Page 12: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/12.jpg)
Multiple Stack
• More than one stacks can be implemented on a single one dimensional array
• Size of each array can be equal or different
• Number of arrays can be fixed or varying
• Simple implementation consists of two arrays of . Size of each array can not be predicted
![Page 13: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/13.jpg)
Queues• Queue is a data structure that can be used to store data which can
later be retrieved in the first in first out (FIFO) order.
• Queue is an ordered-list in which all the insertions and deletions are made at two different ends to maintain the FIFO order.
• The operations defined on a Queue are:
• Add/ Insert - Store onto a Queue
• Remove/ Delete - retrieve (delete) from Queue
• isEmpty - check if the Queue is empty
• isFull - check if the Queue is full
• A Queue can be very easily implemented using arrays.
• Queue is implemented by maintaining one pointer to the front element in the Queue and another pointer pointing to the rear of the Queue.
• Insertions are made at the rear and deletions are made from the front.
![Page 14: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/14.jpg)
Uses of Queue
• Many real life applications (banks, bill paying in
shopping malls, etc)
• Process Scheduling
• Memory Management
• Trees traversing
• etc
![Page 15: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/15.jpg)
Queues – Array Implementation
class Queue {public:
Queue(int s = 10); // constructor - default size = 10~Queue() {delete [ ] QueueArray; } // destructorbool add (int n);bool remove (int &n);bool isFull() {return size == MaxSize;}bool isEmpty() {return size == 0; }
private:int maxSize; // max Queue sizeint front, rear;int *QueueArray;int size; // no. of elements in the Queue
};
![Page 16: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/16.jpg)
Queue::Queue(int s)
{
if (s <= 0) maxSize = 10; else maxSize = s;
QueueArray = new int[maxSize];
size = 0;
rear = 0; // points to the last element
front = 0; // points to first element
}
![Page 17: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/17.jpg)
bool Queue::add(int n)
{
if (! isFull() ) {
QueueArray[rear] = n;
rear++;
size++;
return true;
}
else return false;
}
![Page 18: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/18.jpg)
bool Queue::remove(int &n)
{
if (! isEmpty() {
n = QueueArray[front];
front++;
size--;
return true;
}
else return false;
}
![Page 19: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/19.jpg)
• Assume maxSize = 5
• Initial conditionsize = 0; front = 0; rear = 0;
• Add 3, remove 1size = 2; front = 1; rear = 3;
![Page 20: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/20.jpg)
• Add 2 more, remove 1 moresize = 3; front = 2; rear = 5;
• Question: Is the Queue Full?
• Where to add the next element?
• Push everything backsize = 3; front = 0; rear = 3;
• Cost? • O (size)
![Page 21: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/21.jpg)
Circular Implementation
bool Queue::add(int n){
if (! isFull() ) { QueueArray[rear] = n; rear++;if (rear == maxSize)
rear = 0; size++;return true;
}else return false;
}
01
2
3
MS-1
![Page 22: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/22.jpg)
Circular Implementation
bool Queue::remove(int &n){
if (! isEmpty() { n = QueueArray[front]; front++;if (front == maxSize)
front = 0;size--;return true;
}else return false;
}
01
2
3
MS-1
![Page 23: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/23.jpg)
bool Queue::add(int n){
if (! isFull() ) { QueueArray[rear] = n; rear++;if (rear == maxSize)
rear = 0; size++;return true;
}else return false;
}
01
2
3
MS-1
bool Queue::isFull() {return size == MaxSize;}bool Queue::isEmpty() {return size == 0; }
Queue::Queue(int s){
if (s <= 0) MaxSize = 10; else MaxSize = s;QueueArray = new int[MaxSize];size = 0; rear = 0; front = 0;
}
bool Queue::remove(int &n){
if (! isEmpty() ) { n = QueueArray[rear]; front++;if (front == maxSize)
front = 0; size--;return true;
}else return false;
}
![Page 24: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/24.jpg)
bool Queue::add(int n){
if (! isFull() ) { QueueArray[rear] = n; rear++;if (rear == maxSize)
rear = 0; size++;return true;
}else return false;
}
bool Queue::remove(int &n){
if (! isEmpty() { n = QueueArray[front]; front++;if (front == maxSize)
front = 0;size--;return true;
}else return false;
}
01
2
3
MS-1
Add: rear = (rear + 1) % maxSize; Remove: front = (front + 1) % maxSize;
What happens if we try to do it clockwise?
![Page 25: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/25.jpg)
Double Ended Queue (Dequeue)
• We can insert at or delete from queue at either end
• Can be used as queue as well as stack
• Two types - Input restricted dequeue
- output restricted dequeue
![Page 26: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/26.jpg)
Input restricted dequeue (algorithm)
remove (queue, n, f, r, side)
1. If (f= -1 or r= -1) then
print (“queue is empty”)
return (dummy value)
2. If (f = r) then
n = queue [f]
f = -1, r = -1
3. If (side = ‘front) then
n = queue [f]
f = f + 1
4. If (side = ‘rear’) then
n = queue [r]
r = r – 1
5. Return (n)
![Page 27: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/27.jpg)
Output restricted dequeue (algorithm)
Add( queue, f, r, n, size, side)
1. If (side = ‘front’ and f = 0) or (side = ‘rear’ and r = size-1)
print (“ queue full”)
return
2. If (side = ‘front’) then
if ( f = -1) then
f = 0, r = 0
else
f = f – 1
queue [f] = n
3. If (side = ‘rear’) then
r = r + 1
queue [r] = n
if (f = -1) then
f = 0, r = 0
4. return
![Page 28: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/28.jpg)
Priority Queue
• Items are inserted according to priority• Priority determines the order in which items exists in the
queue• Simple example is mail sorting, process control systems• Ascending Priority Queue ( queue which remove lowest
priority item first)• Descending priority Queue (queue which remove highest
priority item first)• Implementation is like multiple stack i.e more than one
queues can exist on a single one dimensional array
![Page 29: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/29.jpg)
Use of Stack in the Implementation of
Subprograms • In a typical program, the subprogram
calls are nested.• Some of these subprogram calls may be
recursive.• Address of the next instruction in the
calling program must be saved in order to resume the execution from the point of subprogram call.
• Since the subprogram calls are nested to an arbitrary depth, use of stack is a natural choice to preserve the return address.
![Page 30: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/30.jpg)
Activation Records
• An activation record is a data structure which keeps important information about a sub program.
• In modern languages, whenever a subprogram is called, a new activation record corresponding to the subprogram call is created, and pushed into the stack.
• The information stored in an activation record includes the address of the next instruction to be executed, and current value of all the local variables and parameters. i.e. the context of a subprogram is stored in the activation record.
• When the subprogram finishes its execution and returns back to the calling function, its activation record is popped from the stack and destroyed-restoring the context of the calling function.
![Page 31: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/31.jpg)
Activation Records
![Page 32: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/32.jpg)
Recursive Functions• Recursion is a very powerful algorithm design tool.• A subprogram is called recursive if it directly or
indirectly calls itself.
• A recursive program has two parts:• The End Condition.• The Recursive Step.
• The end condition specifies where to stop.
• With each recursive step you should come closer to the end condition.
• In other words, with a recursive step, you apply the same algorithm on a scaled down problem and its process is repeated until the end of condition is reached.
![Page 33: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/33.jpg)
Recursion – Some Examples
int linear_search (int a[], int from, int to, int key){
if (from <= to) { //end conditionif (key == a[from]) return from;
//end conditionelse
return linear_search(a, from+1, to, key)//recursive step
}else
return -1;}
![Page 34: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/34.jpg)
int binary_search (int a[], int high, int low, int key){
int mid = (high + low)/2;if (high >=low) {
if (key == a[mid]) return mid; //end conditionelse if (key < a[mid])
return binary_search(a, mid -1, low, key); //recursive step
elsereturn binary_search(a, high, mid + 1, key);
//recursive step}else
return-1; // end condition}
![Page 35: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/35.jpg)
How Does Recursion WorkSome More Recursive Functions
and Their Simulation
int factorial (int n){
int temp;
if (n<0) return 0; //end conditionelse if (n <=1) return 1; //end conditionelse {
temp = factorial(n-1); //recursive stepreturn n*temp;
}}
![Page 36: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/36.jpg)
Simulation of factorial(4)
• if (n<0) return 0;• else if (n <=1) return 1;• else {• temp = factorial (n-1);• return n*temp;
}
n = 4
1.2.3.4.
n = 3
1.2.3.4.
n = 2
1.2.3.4.
n = 1
1.2. return 1
temp = 15. return 2*1
1.2. return 1
temp = 25. return 3*2
1.2. return 1
temp = 65. return 4*6
1.2. return 1
![Page 37: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/37.jpg)
Fibonacci Sequence0,1,1,2,3,5,8,13,…
int fibonacci (int n){
int temp1, temp2;if (n<=0) return 0; //end conditionelse if (n<=2) return 1; //end conditionelse{
temp1 = fibonacci(n - 1); // recursive step temp2 = fibonacci(n - 2); //recursive step return temp1 + temp2; //same as return fib(N-1)+fib(N-2)
}}
![Page 38: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/38.jpg)
Simulation of fibonacci(4)
1. if (n<=0) return 0;• else if (n<=2) return 1;
else {• temp1 = fibonacci(n - 1);• temp2 = fibonacci(n - 2);
• return temp1 + temp2;
}
n = 21.1. return 1
n = 41.2.3.
n = 31.2.3. temp1 = 14.
1.2. return 1
n = 11.2. return 1
temp2 = 15. return 2
1.2. return 1
temp1 = 24.
1.2. return 1
n = 21.2. return 1
temp2 = 15. return 3
1.2. return 1
![Page 39: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/39.jpg)
Tower of Honoi• Invented by French mathematician Lucas in 1880s.• In the town of Honoi, monks are playing a game with:
– 3 diamond needles fixed on a brass plate.– One needle contains 64 pure gold disks of different
diameters. – Plates are put on top of each other in the order of
their diameters with the largest plate lying at the bottom on the brass plate.
• Priests are supposed to transfer all the disks from one needle to the other such that:– Only one disk can be moved at a time.– At no time a disk of larger diameter should be put on
a disk of smaller diameter.
![Page 40: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/40.jpg)
From Using To
3 Disks
• How much time would it take? Estimate…..• According to the legend, that will mark the end of time!
![Page 41: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/41.jpg)
Tower of HonoiRecursive Solution
End Condition:
Recursive Step:
![Page 42: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/42.jpg)
Tower of Honoivoid TOH (int from, int to, int using, int n){
if (n>0) // end condition – stop when there is // nothing to be moved
{TOH (from, using, to, n-1);
// recursive step// move n-1 plates from the starting// disk to the auxiliary disk
move (to, from);// move the nth disk to the destination
TOH (using, to, from, n-1);// recursive step// move n-1 plates from the auxiliary// disk to the destination disk
}}
![Page 43: 03 stacks and_queues_using_arrays](https://reader034.vdocument.in/reader034/viewer/2022051514/54b730624a7959772f8b4706/html5/thumbnails/43.jpg)
1. if (n>0) 2. {• TOH (from, using, to, n-1);• move (to, from);• TOH (using, to, from, n-1);1. }
Simulation of TOH(1, 2, 3, 3)
1 2
3
from:1, to:2, using:3, n:3
f = 1, t = 2, u = 3, n = 3
statement: 3
f = 1, t = 2, u = 3, n = 1
statement: 3f = 1, t = 3, u = 2,
n = 2statement: 3
f = 1, t = 3, u = 2, n = 0
statement: 1f = 1, t = 2, u = 3,
n = 1statement: 4
f = 1, t = 2, u = 3, n = 1
statement: 5
f = 3, t = 2, u = 1, n = 0
statement: 1
f = 1, t = 3, u = 2, n = 2
statement: 4
f = 1, t = 3, u = 2, n = 2
statement: 5
f = 2, t = 3, u = 1, n = 1
statement: 3
f = 2, t = 1, u = 3, n = 0
statement: 1f = 2, t = 3, u = 1,
n = 1statement: 4
f = 2, t = 3, u = 1, n = 1
statement: 5
f = 1, t = 3, u = 2, n = 0
statement: 1
f = 1, t = 2, u = 3, n = 3
statement: 4
f = 1, t = 2, u = 3, n = 3
statement: 5
f = 3, t = 2, u = 1, n = 2
statement: 3
f = 3, t = 1, u = 2, n = 1
statement: 3
f = 3, t = 2, u = 1, n = 0
statement: 3f = 3, t = 1, u = 2,
n = 1statement: 4
f = 3, t = 1, u = 2, n = 1
statement: 5
f = 2, t = 1, u = 3, n = 0
statement: 1
f = 3, t = 2, u = 1, n = 2
statement: 4
f = 3, t = 2, u = 1, n = 2
statement: 5
f = 1, t = 2, u = 3, n = 1
statement: 3
f = 1, t = 3, u = 2, n = 0
statement: 1f = 1, t = 2, u = 3,
n = 1statement: 4
f = 3, t = 2, u = 1, n = 1
statement: 5
f = 1, t = 2, u = 3, n = 0
statement: 5