Lesson 3 - Stacks and Queues

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/30

encourage image

There are no tags or description

Looks like no tags are added yet.

31 Terms

1
New cards

Stack

is a linear data structure that follows the LIFO (Last In, First Out) principle. The last element added to the stack is the first one to be removed.

2
New cards
  • A stack of books

  • The Undo button in Word

  • The Back button in a web browser

  • stack of bracelets


Real-world examples of stacks:

3
New cards

TOP

A stack has only ONE accessible end, called what?

4
New cards

BASE or BOTTOM

The other end of a stack is called what?

  • it is closed - you cannot add or remove elements there directly.


5
New cards

push(x)

Adds element x to the top of the stack

6
New cards

pop()

Removes and returns the top element

7
New cards

peek() / top()

Returns the top element WITHOUT removing it

8
New cards

isEmpty()

Checks whether the stack has zero elements

9
New cards

isFull()

Checks whether the stack has reached its maximum capacity (array-based only)

10
New cards

size()

returns the size of the stack.

11
New cards

Array-Based Stack

uses a fixed-size array plus a variable (commonly called 'top') that keeps track of the index of the top element.

12
New cards

Linked-List-Based Stack

stack uses a singly linked list, where the HEAD of the list always represents the TOP of the stack.

13
New cards

Queues

is a linear data structure that follows the FIFO (First In, First Out) principle. The first element added to the queue is the first one to be removed.

14
New cards

rear

Position of the last entry in the queue, that is, the one most recently added

15
New cards

front

Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue

16
New cards

head

other term for front

17
New cards

tail

other term for rear

18
New cards

Size

refers to the current number of elements in the queue.

19
New cards

Capacity

refers to the maximum number of elements the queue can hold.

20
New cards
  • People lining up

  • Customer service call centers

  • Cars waiting in a single-lane toll booth.


Real-world examples of queues:

21
New cards

FIFO Principle

states that the first element added to the Queue will be the first one to be removed or processed.

22
New cards

enqueue(x)

Adds element x to the rear of the queue

23
New cards

dequeue()

Removes and returns the front element

24
New cards

front() / peek()

Returns the front element WITHOUT removing it

25
New cards

Returns the front element WITHOUT

Checks whether the queue has zero elements

26
New cards

isFull()

Checks whether the queue has reached its maximum capacity (array-based only)

27
New cards
  • Linear (Simple) Queue

  • Circular Queue

  • Priority Queue

  • Double-Ended Queue (Deque)


Types of Queues

28
New cards

Linear (Simple) Queue

an array-based queue, once the rear reaches the last index, no more elements can be added - even if front slots were freed by dequeues - unless elements are shifted.

29
New cards

Circular Queue

Solves the wasted-space problem of a linear queue by connecting the rear back to the front. When the rear reaches the end of the array, it wraps around to index 0 if that slot is free.

30
New cards

Priority Queue

Elements are removed based on PRIORITY rather than arrival order.

31
New cards

Double-Ended Queue (Deque)

Allows insertion and removal from BOTH ends - front and rear. A deque can behave like a stack, a queue, or both at once.