Stack and queue abstract data types implementation with Python.
Stack
- LIFO structure
- Graph algorithm: depth-first search can be implemented with Stack
- Finding Euler-cycles in a Graph
- Finding strongly connected components in a graph
- If we use recursion, the OS will use stacks
- Most important application of stacks: stack memory
- it is a special region of the memory (in the RAM)
- it keeps track of the point to which each active subroutine should return control when it finishes executing
- store temporary variables created by each function
- stack memory is limited
Heap Memory
- The heap is a region that is not automatically arranged for you
- This is a large region of memory, unlike stack memory
- C: malloc() and calloc() function, with pointers
- Java: reference types and objects are on the Heap
- We have to deallocate these memory chunks as it it not managed automatically. If not, memory leak!
- Slower than stack memory because of pointers
stack memory | heap memory |
---|---|
limited in size | no size limit |
fast access | slow access |
local variables | objects |
space is managed efficiently by CPU | memory may be fragmented |
variables cannot be resized | variables can be resized // realloc() |
Queue
- FIFO structure
- It can be implemented with linked lists
- application: BFS
- Operational research applications or stochastic models relies heavily on queues
Implementation
Stack
1 | # stack is an abstract data type (interface), LIFO |
Queue
1 | # queue is an abstract data type (interface) FIFO |