Stack and Queue
Stack and Queue
Algorithms
Think of a data structure like a recipe for cooking a meal. Just like a recipe with a list
of ingredients and instructions on preparing them, a data structure organises and
stores data in a specific way to make it easy to find, use and change. For example,
an array is like a set of drawers, each one labelled and numbered, where you can
put and take out data in a specific order. A linked list is like a chain of shopping carts,
where each cart contains a small piece of data and a pointer that points to the next
cart, allowing you to move forward and backward through the data. A queue is like a
line at a concert, where new data goes to the back, and the oldest data is removed
from the front. A stack is like a tower of plates, where you can only add or remove
the topmost plate. Finally, a tree is like a family tree, where data is organised in a
hierarchical order with a parent-child relationship. Each data structure has unique
characteristics and can be used for different tasks, just like how different recipes are
used for different dishes.
What is Stack?
A stack is a data structure that follows the Last In First Out (LIFO) principle. This
means that the last element added to the stack is the first one to be removed. The
stack is referred to as a linear data structure that is sometimes compared to a stack
of plates where only the top plate can be added or removed.
A stack has two primary operations:
1. Push: This operation is used to add an element to the top of the stack.
2. Pop: This operation removes the top element from the stack.
What is meant by Top of the Stack?
The "top of the stack" refers to the element that is most recently added to the stack.
A stack is a Last-In-First-Out (LIFO) data structure, meaning that the last element
added to the stack is the first to be removed. Therefore, the top of the stack is the
element at the end of the stack, also known as the "top" element. The element can
be accessed or removed from the stack next.
Push:
Adds an item to the stack. If the stack is full, then it is said to be an Overflow
condition.
Top:
Returns the top element of the stack.
Complexity Analysis:
Time Complexity
Types of Stacks:
Register Stack: A specific kind of stack that is kept in the CPU's registers is known
as a register stack. Due to their location within the CPU and ease of access, these
kinds of stacks operate incredibly quickly. They are commonly used for the
short-term storing of data or for keeping track of the locations of function calls.
Memory Stack:A memory stack, on the other hand, is a type of stack that is stored
in the computer's main memory. These types of stacks are slower than register
stacks because they are located outside of the CPU, but they can store more data
than register stacks. They are often used for more permanent storage of data, such
as program execution context.
● Using array
● Using linked list
This implementation uses a Python list as the underlying array for the stack. The
push() method adds an item to the stack by appending it to the list. The pop()
method removes and returns the last item from the list. The peek() method returns
the last item from the list without removing it. The is_empty() method returns True if
the list is empty, and False otherwise. The get_stack() method returns the current
stack.
Advantages of array implementation:
● Dynamic size: Linked list-based stack can adjust its size according to the
number of elements, and it can grow or shrink as needed.
● No wasted space: Linked list-based stack does not waste space when it's not
full and does not need to be resized.
● No memory fragmentation: Linked list-based stack does not cause memory
fragmentation as it only uses the memory it needs.
● Dynamic memory allocation: Linked list-based stack supports dynamic
memory allocation. It can handle large amounts of data.
● Support for large data types: Linked list-based stack can handle large data
types such as images or videos.
● Cache-friendly: Linked list-based stack is cache-friendly as it only needs to
access the current node and the next node; this reduces cache misses.
● Simple to understand: The linked list-based implementation is simple and
easy to understand as it represents the stack.
● Support for multiple data types: A linked list-based stack can store various
data types, for example, integers, strings, or objects.
Advantages of Stack:
Disadvantages of Stack:
This means that when an element is added to the queue, it is placed at the rear of
the queue, and when an element is removed from the queue, it is removed from the
front of the queue.
The element that has been in the queue the longest is the one that is removed first.
This principle ensures that elements are removed in the order they were added,
making it a first-in, first-out data structure
Imagine a line at a store as a real-world example; the person who is first in line will
get service. That is how the queue works, the first element added to the queue is the
first one to be removed. Like a queue at a store, the first person who joins the queue
will be the first one to leave the queue.
Characteristics of Queue:
Types of Queue:
2. Circular Queue: A circular queue is a variation of the simple queue, where the
rear pointer wraps around to the front when it reaches the end of the array,
allowing for efficient memory use.
Enqueue():
Enqueue is a basic operation in queue data structure, it is used to add an element to
the rear of the queue.
Front():
The front() operation is used to access the element at the front of the queue without
removing it.
The algorithm for the front operation is as follows:
1. Check if the queue is empty. If it is, return an error message or raise an
exception.
2. Return the data of the front node.
Queue Representation:
A queue can be represented in several ways, but the most common representations
are:
1. Array-based representation: A queue is represented by an array. The array's
first and last elements are the front and back of the queue, respectively.
Enqueue and dequeue operations are performed by moving the front and rear
pointers, respectively.
2. Linked List representation: A linked list is used to represent a queue. The front
of the queue is the head of the list, and the rear is the tail of the list. Enqueue
and dequeue operations are performed by adding and removing elements
Applications of Queue:
Queues have several applications in various fields; here are a few examples of how
queues are used in real-world scenarios:
1. Operating Systems: Queues are used to manage processes and tasks.
2. Networking: Queues are used in networking to manage incoming and
outgoing data packets.
3. Simulation: Queues simulate real-world systems such as bank teller lines,
printing queues, and more.
4. Web servers: Queues are used to handle incoming requests in web servers
and manage the server load.
5. BFS(Breadth First Search) Algorithm: Queue is used as a fundamental data
structure to implement the BFS algorithm.
6. Graphics processing: Queues are used to manage the rendering order of 3D
graphics.
7. Real-time systems: Queues are used to manage real-time events and to
ensure that events are processed in the order they occur.
8. Priority Queue: Queues can be used to implement priority queues. In this kind
of queue, the element with the highest priority is processed first.
Advantages of Queue:
Disadvantages of Queue:
1. Queues have a fixed size, so they can become full and not be able to insert
elements.
2. Queues are not suitable for cases where random access is required.
3. A queue can cause memory waste if the queue is too big and the elements
are dequeued too quickly.
4. If a priority queue is not implemented carefully, it can lead to starvation, where
lower priority elements are never served.
5. Queues can be a source of latency when the dequeue is slow, and the
enqueue is fast.
6. Queues can be a source of contention when multiple processes try to access
the same queue simultaneously.
7. Queues are not suitable for cases where elements' order is unimportant or
elements need to be accessed in random order.
If you're looking to get into Web Development,
then AlmaBetter is the best place to start your
journey.
Link: https://link.almabetter.com/9w63