0% found this document useful (0 votes)
5 views65 pages

dsa2

Uploaded by

zain05abid
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 views65 pages

dsa2

Uploaded by

zain05abid
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/ 65

DSA Module - 2

QUEUES AND LINKED LIST


Module 2
QUEUES: Queues, Circular Queues, Using Dynamic Arrays, Multiple
Stacks and queues.
LINKED LISTS : Singly Linked, Lists and Chains, Representing Chains in C,
Linked Stacks and Queues, Polynomials
Text Book: Chapter-3: 3.3, 3.4, 3.7 Chapter-4: 4.1 to 4.4
QUEUES: Queues, Circular Queues, Using Dynamic Arrays,
Multiple Stacks and queues

Prof A Majeed KM 3
Prof A Majeed KM 4
Prof A Majeed KM 5
Prof A Majeed KM 6
Prof A Majeed KM 7
Prof A Majeed KM 8
Queue Data Structure: Static and Dynamic

Prof A Majeed KM 9
Prof A Majeed KM 10
Prof A Majeed KM 11
Prof A Majeed KM 12
Prof A Majeed KM 13
Circular Queue

Prof A Majeed KM 14
Prof A Majeed KM 15
Prof A Majeed KM 16
Prof A Majeed KM 17
Prof A Majeed KM 18
Prof A Majeed KM 19
Prof A Majeed KM 20
Prof A Majeed KM 21
Prof A Majeed KM 22
Prof A Majeed KM 23
Multiple Stack and Multiple Queues

Prof A Majeed KM 24
Multiple Stacks:
• Definition:
– Multiple stacks refer to the concept of having more than one stack in a data structure.
– Each stack operates independently, and their operations do not interfere with each other.
• Implementation:
– Multiple stacks can be implemented using a single array by dividing it into multiple segments.
– Each stack has its own starting index and grows in a specific direction (e.g., left to right or right to left).
– Proper management of stack pointers is crucial to avoid overlap between stacks.
• Use Cases:
– In situations where different entities or processes require separate stacks to maintain their state.
– Memory management in certain algorithms or applications where multiple independent stacks are
needed.
• Advantages:
– Isolation: Each stack operates independently, providing a level of isolation between different
components or processes.
– Efficiency: Multiple stacks can be more efficient for certain algorithms that require distinct storage for
different purposes.
• Challenges:
– Overlapping: Care must be taken to prevent overlapping of stacks within the allocated memory space.
Prof A Majeed KM 25
– Dynamic resizing: Handling the resizing of individual stacks when needed can be complex.
• Multiple Queues:
• Definition:
– Multiple queues involve the use of more than one queue in a data structure, where each queue
functions independently.
• Implementation:
– Similar to multiple stacks, multiple queues can be implemented using a segmented array approach.
– Each queue has its own starting index and grows in a specific direction.
– Proper management of front and rear pointers is essential for each queue.
• Use Cases:
– Task scheduling in operating systems where different queues represent different priority levels.
– Simulating multiple parallel processes or workflows in certain applications.
• Advantages:
– Organization: Allows for the organization and separation of different types of data or tasks.
– Scalability: Easily scalable to accommodate varying loads in different queues independently.
• Challenges:
– Resource allocation: Ensuring fair resource allocation among different queues may require additional
considerations.
– Priority management: Proper management of priorities, if applicable, is crucial for the effective
functioning of multiple queues.

Prof A Majeed KM 26
Important Questions:
• What is queue? What are difference between stack and queue Write C
function for isfull(), isempty(), insert() and delete() operations on a
linear queue.
• List the disadvantages of linear queue and explain how it is solved in
circular queue. Give the algorithm to implement a circular queue with
suitable example.
• Explain the implementation of circular queues using arrays.
(dynamically allocated Array)
• Write C function for insert() and delete() operations on a circular
queue. List the advantages of circular queue over linear queue.
• Write a short note on Multiple stack and Queues

Prof A Majeed KM 27
LINKED LISTS :
Singly Linked Lists and Chains,
Representing Chains in C,
Linked Stacks and Queues,
Polynomials

Prof A Majeed KM 28
Types of linked list : Singly linked list
– The node contains a pointer to the next node means that
the node stores the address of the next node in the
sequence.
– A single linked list allows the traversal of data only in one
way. Below is the image for the same.
Types of linked list : Doubly linked list
• A doubly linked list or a two-way linked list is a more
complex type of linked list that contains a pointer to the
next as well as the previous node in sequence.
• This would enable us to traverse the list in the
backward direction as well. Below is the image for the
same:
Types of linked list : Circular Linked list
• A circular linked list is that in which the last node
contains the pointer to the first node of the list.
• While traversing a circular linked list, we can begin at any
node and traverse the list in any direction forward and
backward until we reach the same node we started.
Thus, a circular linked list has no beginning and no end.
Types of linked list: Doubly Circular linked list
• A Doubly Circular linked list or a circular two-way linked list is a
more complex type of linked list that contains a pointer to the
next as well as the previous node in the sequence.
• The difference between the doubly linked and circular doubly
list is the same as that between a singly linked list and a
circular linked list. The circular doubly linked list does not
contain null in the previous field of the first node.
Type of linked list : Header linked list
• A header linked list is a special
type of linked list that contains
a header node at the beginning
of the list.
• So, in a header linked
list START will not point to the
first node of the list
but START will contain the
address of the header node.
Below is the image for
Grounded Header Linked List:
Polynomial addition using CLL with header
Important Questions
• Define Linked Lists. Explain in detail, the operations
performed on Singly Linked Lists. With neat diagram, explain
the different types of linked lists.
• Write the function to perform the following on SLL: (usually using
integer data type)
• Creating an ordered SLL. (or create a list)
• Count number of elements (Length)
• Search an element • Display all the elements in SLL
• Deleting a node at rear end of SLL. • Insert a node at front end of SLL.

Prof A Majeed KM 62
Important Questions
• How can an ordinary queue be represented using a singly
linked list? Write C function for linked implementation of
ordinary queue insertion & deletion.
• Write C program to implement linked stacks.
• Write node structure for linked representation of
polynomial. Write a function to add 2 polynomials
using singly linked list (SLL).

Prof A Majeed KM 63
Important Questions
• Write the function to perform the following on CSLL: (usually using
integer data type)
• Creating an ordered CSLL. (or create a list)
• Count number of elements (Length)
• Search an element • Display all the elements in CSLL
• Deleting a node at rear end of CSLL. • Insert a node at front end of CSLL.
• Write node structure for linked representation of polynomial.
Write a function to add 2 polynomials using Circular singly
linked list (CSLL).
Prof A Majeed KM 64
• Any Questions?

• Thank You

Prof A Majeed KM 65

You might also like