0% found this document useful (0 votes)
5 views

Stacks and Queues in Detail

Stacks and queues are linear data structures that manage data differently, with stacks using a Last In, First Out (LIFO) approach and queues using a First In, First Out (FIFO) approach. Basic operations for stacks include push, pop, and peek, while queues utilize enqueue, dequeue, and peek. Both structures have real-world applications, such as undo operations for stacks and print job management for queues.

Uploaded by

Morrice Nkhoma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Stacks and Queues in Detail

Stacks and queues are linear data structures that manage data differently, with stacks using a Last In, First Out (LIFO) approach and queues using a First In, First Out (FIFO) approach. Basic operations for stacks include push, pop, and peek, while queues utilize enqueue, dequeue, and peek. Both structures have real-world applications, such as undo operations for stacks and print job management for queues.

Uploaded by

Morrice Nkhoma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Stacks and Queues in Detail

Stacks and queues are both linear data structures that allow you to store and retrieve data in a
specific order. They both differ in how elements are added and removed.

1. 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.

Basic Operations of a Stack:

1. Push: Add an element to the top of the stack.

2. Pop: Remove the element from the top of the stack.

3. Peek (Top): Get the element at the top without removing it.

4. isEmpty: Check if the stack is empty.

5. Size: Get the number of elements in the stack.

Example of a Stack:

Consider a stack of books. If you add a book to the top of the stack, the book you added last will
be the first one you can remove.

Example Code (in Python):

class Stack:

def __init__(self):

self.stack = []

def push(self, item):

self.stack.append(item)

def pop(self):

if self.is_empty():

return "Stack is empty"


return self.stack.pop()

def peek(self):

if self.is_empty():

return "Stack is empty"

return self.stack[-1]

def is_empty(self):

return len(self.stack) == 0

def size(self):

return len(self.stack)

# Example Usage

stack = Stack()

stack.push(1)

stack.push(2)

stack.push(3)

print(stack.pop()) # Output: 3 (Last pushed element)

print(stack.peek()) # Output: 2 (Next element in the stack)

In this example:

• push(1), push(2), and push(3) add elements to the stack.

• pop() removes and returns the last added element, which is 3.

2. Queue
A queue is a data structure that follows the First In, First Out (FIFO) principle. This means that
the first element added to the queue is the first one to be removed.

Basic Operations of a Queue:

1. Enqueue: Add an element to the back (rear) of the queue.

2. Dequeue: Remove the element from the front of the queue.

3. Peek (Front): Get the element at the front without removing it.

4. isEmpty: Check if the queue is empty.

5. Size: Get the number of elements in the queue.

Example of a Queue:

Consider a line of people waiting for a bus. The first person to get in line is the first person to get
on the bus.

Example Code (in Python):

class Queue:

def __init__(self):

self.queue = []

def enqueue(self, item):

self.queue.append(item)

def dequeue(self):

if self.is_empty():

return "Queue is empty"

return self.queue.pop(0)

def peek(self):

if self.is_empty():

return "Queue is empty"


return self.queue[0]

def is_empty(self):

return len(self.queue) == 0

def size(self):

return len(self.queue)

# Example Usage

queue = Queue()

queue.enqueue(1)

queue.enqueue(2)

queue.enqueue(3)

print(queue.dequeue()) # Output: 1 (First enqueued element)

print(queue.peek()) # Output: 2 (Next element in the queue)

In this example:

• enqueue(1), enqueue(2), and enqueue(3) add elements to the queue.

• dequeue() removes and returns the first added element, which is 1.

Differences Between Stack and Queue

Feature Stack Queue

Order of
Last In, First Out (LIFO) First In, First Out (FIFO)
Elements

Insertion Elements are added at the top Elements are added at the rear

Elements are removed from the


Removal Elements are removed from the top
front
Feature Stack Queue

Undo operations, Expression


Examples Print jobs, Task scheduling
evaluation

Real-world Applications:

• Stack:

o Undo operations: The most recent action can be undone first.

o Expression evaluation: Used in algorithms for parsing expressions, like


converting infix to postfix.

o Function call stack: In programming, function calls are pushed onto a stack.

• Queue:

o Print jobs: Print jobs are handled in a FIFO order.

o Task scheduling: Jobs are processed in the order they arrive.

o Breadth-first search (BFS): A traversal algorithm for graphs uses a queue.

Visualization

• Stack:

• Top -> [3]

• [2]

• [1]

• Queue:

• Front -> [1] [2] [3] <- Rear

Let me know if you'd like more examples or have any questions!

You might also like