reading-notes

Stacks and Queues

Stacks

A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner.

FILO Concept

This means that the first item added in the stack will be the last item popped out of the stack.

LIFO Concept

This means that the last item added to the stack will be the first item popped out of the stack.

Stack methods and operations:

Stack Visual

stack

the picture above shows the structure of a stack,the top is last node pushed into the stack and the first node that would be popped out, if it’s popped out, the top of a stack will be top.next

Stack methods and time complexity

Push

pushing a node into a stack will always have a constant time complexity or O(1), because it will always take the same amount of time no matter what

Pop

pop also has a constant time complexity, because you will always remove from the top, there is no loops.

pushandpop

it’s always better to check if the stack empty by using isEmpty() method before using pop, so an exception will be raised if there is no nodes to remove in the stack.

Pseudo code for pop

ALGORITHM pop()

// INPUT <-- No input

// OUTPUT <-- value of top Node in stack

// EXCEPTION if stack is empty

Node temp <-- top

top <-- top.next

temp.next <-- null

return temp.value

### Peek Method

peek returns the value inside the top of the stack Typically, you would check isEmpty before conducting a peek. This will ensure that an exception is not raised.

Pseudo code for peek

ALGORITHM peek()

// INPUT <-- none

// OUTPUT <-- value of top Node in stack

// EXCEPTION if stack is empty

return top.value

### isEmpty method

the time complexity for this method is O(1) // constant

Pseudo code for peek

ALGORITHM isEmpty()

// INPUT <-- none

// OUTPUT <-- boolean

return top = NULL

How can we implement a stack in Python?

*there are two ways for implementing stack in python, which are:

Queue

queue is a linear data structure that stores items in First In First Out (FIFO) manner. With a queue the least recently added item is removed first.

Queue concepts

FIFO

This means that the first item in the queue will be the first item out of the queue.

LILO

This means that the last item in the queue will be the last item out of the queue.

Queue Visualization

the following picture shows the structure of a queue

queue

Queue methods and operations

Enqueue and Dequeue

these two methods will always have a constant time complexity because you will always add or remove the first node .

enqueueanddequeue

ALGORITHM enqueue(value)

// INPUT <-- value to add to queue (will be wrapped in Node internally)

// OUTPUT <-- none

node = new Node(value)

rear.next <-- node

rear <-- node

ALGORITHM dequeue()

// INPUT <-- none

// OUTPUT <-- value of the removed Node

// EXCEPTION if queue is empty

Node temp <-- front

front <-- front.next

temp.next <-- null

return temp.value

### Peek

ALGORITHM peek()

// INPUT <-- none

// OUTPUT <-- value of the front Node in Queue

// EXCEPTION if Queue is empty

return front.value

IsEmpty

Time complexity // O(1)

ALGORITHM isEmpty()

// INPUT <-- none

// OUTPUT <-- boolean

return front = NULL

Applications of Queue Data Structure

  1. When a resource is shared among multiple consumers.
  2. When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. 3) In Operating systems:

4) In Networks:

   a) Queues in routers/ switches 
   b) Mail Queues

5) Variations