3-Stack Queue PDF
3-Stack Queue PDF
the length of input -> such a program may copy the data in its
entirety to a location on the stack, and in so doing it may change
the return addresses for procedures that have called it. An attacker
can experiment to find a specific type of data that can be
provided to such a program such that the return address of the
current procedure is reset to point to an area within the stack itself
(and within the data provided by the attacker), which in turn
contains instructions that carry out unauthorized operations
„ buffer overflow attack ”
stack.push(12);
Push operation: put the given item to the top of the stack
Very simple operation, can be done in O(1)
stack.push(56);
12
Push operation: put the given item to the top of the stack
Very simple operation, can be done in O(1)
stack.push(56);
56
12
Push operation: put the given item to the top of the stack
Very simple operation, can be done in O(1)
stack.push(88);
56
12
Push operation: put the given item to the top of the stack
Very simple operation, can be done in O(1)
stack.push(88);
88
56
12
Pop operation: we take the last item we have inserted to the top of the stack (LIFO)
Very simple operation, can be done in O(1)
88
56
12
Pop operation: we take the last item we have inserted to the top of the stack (LIFO)
Very simple operation, can be done in O(1)
stack.pop();
88
56
12
Pop operation: we take the last item we have inserted to the top of the stack (LIFO)
Very simple operation, can be done in O(1)
56
12
Pop operation: we take the last item we have inserted to the top of the stack (LIFO)
Very simple operation, can be done in O(1)
stack.pop();
56
12
Pop operation: we take the last item we have inserted to the top of the stack (LIFO)
Very simple operation, can be done in O(1)
stack.pop();
56
12
Peek operation: return the item from the top of the stack without removing it
Very simple operation, can be done in O(1)
88
56
12
Peek operation: return the item from the top of the stack without removing it
Very simple operation, can be done in O(1)
stack.peek();
88
56
12
Peek operation: return the item from the top of the stack without removing it
Very simple operation, can be done in O(1)
stack.peek();
56
12
Applications
There are several situations when recursive methods are quite handy
For example: DFS, traversing a binary search tree, looking for an item
in a linked list ...
What’s happening in the background?
All the recursive algorithms can be transformed into a simple
method with stacks
IMPORTANT: if we use recursion, the OS will use stacks anyways !!!
Depth-first search
public void dfs(Vertex vertex) { public void dfs(Vertex vertex) {
What does it all have to do with stacks? The recursive function calls are pushed
onto the stack until we bump into the base case
if( n == 0 )
return 1;
return n * factorial(n-1);
}
if( n == 0 )
return 1;
return n * factorial(n-1);
}
if( n == 0 )
return 1;
return n * factorial(n-1);
}
4*factorial(3)
int result = factorial(4)
factorial(4)
Factorial: factorial(4)
if( n == 0 )
return 1;
return n * factorial(n-1);
} 3*factorial(2)
4*factorial(3)
int result = factorial(4)
factorial(4)
Factorial: factorial(4)
if( n == 0 )
return 1; 2*factorial(1)
return n * factorial(n-1);
} 3*factorial(2)
4*factorial(3)
int result = factorial(4)
factorial(4)
Factorial: factorial(4)
return n * factorial(n-1);
} 3*factorial(2)
4*factorial(3)
int result = factorial(4)
factorial(4)
Factorial: factorial(4)
if( n == 0 )
return 1; 2*1
return n * factorial(n-1);
} 3*factorial(2)
4*factorial(3)
int result = factorial(4)
factorial(4)
Factorial: factorial(4)
if( n == 0 )
return 1;
return n * factorial(n-1);
} 3*2*1
4*factorial(3)
int result = factorial(4)
factorial(4)
Factorial: factorial(4)
if( n == 0 )
return 1;
return n * factorial(n-1);
}
4*3*2*1
int result = factorial(4)
factorial(4)
Factorial: factorial(4)
Conclusion: recursive method calls are going to be piled up in the stack
if( n == 0 )
return 1;
return n * factorial(n-1);
}
queue.enqueue(10);
Enqueue operation: we just simply add the new item to the end
of the queue
queue.enqueue(10);
10
Enqueue operation: we just simply add the new item to the end
of the queue
queue.enqueue(4);
10
Enqueue operation: we just simply add the new item to the end
of the queue
queue.enqueue(4);
4 10
Enqueue operation: we just simply add the new item to the end
of the queue
queue.enqueue(20);
4 10
Enqueue operation: we just simply add the new item to the end
of the queue
queue.enqueue(20);
20 4 10
Dequeue operation: we just simply remove the item starting at the
beginning of the queue // FIFO structure
20 4 10
Dequeue operation: we just simply remove the item starting at the
beginning of the queue // FIFO structure
queue.dequeue();
20 4 10
Dequeue operation: we just simply remove the item starting at the
beginning of the queue // FIFO structure
queue.dequeue();
20 4 10
Dequeue operation: we just simply remove the item starting at the
beginning of the queue // FIFO structure
queue.dequeue();
20 4
Dequeue operation: we just simply remove the item starting at the
beginning of the queue // FIFO structure
queue.dequeue();
20 4
Dequeue operation: we just simply remove the item starting at the
beginning of the queue // FIFO structure
20
Applications