0% found this document useful (0 votes)
18 views21 pages

CH 4.5 - Linked Stack and Queue

The document discusses stack and queue data structures using linked lists, detailing their operations and implementations. It explains the Last In-First Out (LIFO) principle for stacks and the First In-First Out (FIFO) principle for queues, along with methods for adding and removing elements. Additionally, it covers the Radix Sort algorithm, which utilizes queues for sorting integers based on their digits.

Uploaded by

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

CH 4.5 - Linked Stack and Queue

The document discusses stack and queue data structures using linked lists, detailing their operations and implementations. It explains the Last In-First Out (LIFO) principle for stacks and the First In-First Out (FIFO) principle for queues, along with methods for adding and removing elements. Additionally, it covers the Radix Sort algorithm, which utilizes queues for sorting integers based on their digits.

Uploaded by

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

Faculty of Information Technology - Computer Science Department 1

Data Structures

Faculty of Information Technology - Computer Science Department 2


Chapter 4
Stack and Queue Data Structures Using Linked
List

Faculty of Information Technology - Computer Science Department 3


A Stack Abstract Data Structure (re-visited)

• Remember that a stack ADT is a Last In – First Out data


structure where the last element added to the stack
(using the push()operation) is the first one to be removed
(using the pop()operation).

• Previously an array-based implementation was used to


build a bounded stack ADT.

• Now, a linked-based implementation will be used to


build an unbounded stack ADT.

Faculty of Information Technology - Computer Science Department 4


A Stack Abstract Data Structure (re-visited)

• The stack operations for the Linked-based


implementation:
• push(): adds an element to the top of the stack
• pop(): removes the top element from the stack
• peak(): returns the top element of the stack
• isEmpty(): checks if the stack is empty

Faculty of Information Technology - Computer Science Department 5


A Linked-based Stack (Unbounded)
MyStack Top Always add and
• To create an unbounded Stack, a Linked-based remove elements
H from the top only
implementation using the unsorted linked lists will
next
be used.

• push() method adds a new node to the top of the C

stack (before the first node (addFirst)) next

• pop() method removes the top node (first node in the


K
stack (delFirst)).
next
• A top pointer is used that will always point to the top
NULL
of the stack (head of the list)
Faculty of Information Technology - Computer Science Department 6
Adding a new element to the Stack (push)
MyStack Top
1. newNode.next =top;
2. top = newNode; Y

MyStack Top
2 next
newNode MyStack Top

H H
Y Y
H
next next
next next
next

C
1 X C
1 X
NULL NULL
C
next next
next

K K
K
next next
next

NULL NULL
NULL

Faculty of Information Technology - Computer Science Department 7


Remove the element at the top of the Stack (pop)
MyStack Top MyStack Top
temp
Y Y
1.temp = top.element; 1
next
2 next MyStack Top

2.top = top.next;
H H H

next next next

C C C

next next next

K K K

next next next

NULL NULL NULL

Faculty of Information Technology - Computer Science Department


8
Linked Stack Class Implementation

9
Faculty of Information Technology - Computer Science Department
Queue Representation (re-visited)

• A Queue is a structure in which elements are added to the rear and removed
from the front. First-In-First-Out (FIFO) structure.

• The queue operations for the Linked-based implantation:


• enqueue(): adds an element to the end of the queue
• dequeue(): removes the first element from the queue
• isEmpty(): checks if the queue is empty

Faculty of Information Technology - Computer Science Department 10


A Linked-based Queue (Unbounded)

• To create an unbounded Queue, a Linked-based implementation using the unsorted


linked lists will be used.

• A front pointer will point to the first node of the queue, and a rear pointer will point to
the last node of the queue.

Faculty of Information Technology - Computer Science Department 11


Adding a new element to the Queue (enqueue)

1. rear.next = newNode;
2. rear = newNode;

Faculty of Information Technology - Computer Science Department 12


Remove the element from the front of the queue (dequeue)

1. temp = front.element;
2. front = front.next;
If the queue is empty set the rear to null.

Faculty of Information Technology - Computer Science Department 13


Linked Queue Class Implementation

14
Faculty of Information Technology - Computer Science Department
Linked Queue Class Implementation

Faculty of Information Technology - Computer Science Department 15


Queue Application: Radix Sort

 Radix sort is one of the sorting algorithms used to sort a list of integer numbers.

 In the radix sort algorithm, a list of integer numbers will be sorted based on the digits of
individual numbers.
 Sorting is performed from the least significant digit to the most significant digit.

 Radix sort algorithm requires the number of passes which are equal to the number of digits
present in the largest number among the list of numbers.
 For example, if the largest number is a 3-digit number then that list is sorted with 3 passes.

Faculty of Information Technology - Computer Science Department 16


Queue Application: Radix Sort

Step-by-Step Process
 The Radix sort algorithm is performed using the following steps:

Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9.

Step 2 - Consider the least significant digit of each number in the list which is to be sorted.

Step 3 - Insert each number into its respective queue based on the least significant digit.

Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have inserted.

Step 5 - Repeat starting from step 3 based on the next least significant digit until all the
numbers are grouped based on the most significant digit.
Faculty of Information Technology - Computer Science Department 17
Queue Application: Radix Sort

Example
 Sort the following integer numbers using Radix Sort Algorithm:

82, 901, 100, 12, 150, 77, 55, 23

Faculty of Information Technology - Computer Science Department 18


Queue Application: Radix Sort

Example

Faculty of Information Technology - Computer Science Department 19


Queue Application: Radix Sort

Example

Faculty of Information Technology - Computer Science Department 20


Queue Application: Radix Sort

Example

Faculty of Information Technology - Computer Science Department 21

You might also like