Data Structure
Data Structure
Q1. a) Explain the classification of data structures with suitable examples. Discuss linear and non-
linear data structures in detail.
A data structure is a way of organizing and storing data so that it can be accessed and modified
efficiently. Data structures are classified into different types based on the way data elements are
organized and related to each other.
--------------------------------------------------
Primitive data structures are the basic data types provided by programming languages. They store
single values.
Examples:
• Integer
• Float
• Character
• Boolean
Example:
int a = 10;
char ch = 'A';
--------------------------------------------------
Non-primitive data structures are more complex and can store multiple values.
In linear data structures, elements are arranged in a sequential manner. Each element is connected
to the next element.
Characteristics:
• Easier to implement.
a) Array
An array is a collection of elements of the same data type stored in contiguous memory locations.
Example:
Representation:
Index : 0 1 2 3 4
Data : 10 20 30 40 50
Advantages:
• Easy to traverse.
Disadvantages:
• Fixed size.
--------------------------------------------------
b) Linked List
A linked list consists of nodes where each node contains data and a pointer to the next node.
Representation:
Advantages:
• Dynamic size.
Disadvantages:
--------------------------------------------------
c) Stack
Operations:
• Push
• Pop
• Peek
Representation:
Top
|30|
|20|
|10|
Applications:
• Function calls
• Expression evaluation
• Undo operation
--------------------------------------------------
d) Queue
Operations:
• Enqueue
• Dequeue
Representation:
Front → 10 20 30 40 ← Rear
Applications:
• CPU scheduling
• Printer queue
===================================
In non-linear data structures, elements are not arranged sequentially. One element can be connected
to multiple elements.
Characteristics:
• Hierarchical relationship.
a) Tree
Tree Structure:
/\
B C
/\
D E
Terminology:
• A → Root node
• B, C → Child nodes
• D, E → Leaf nodes
Applications:
• File systems
• Database indexing
• XML/HTML parsing
--------------------------------------------------
b) Graph
Representation:
A ----- B
\ /
\/
Applications:
• Social networks
• Computer networks
====================================
• Easier to implement
• More complex
• Examples: Tree, Graph
CONCLUSION
Data structures play an important role in organizing and managing data efficiently. They are broadly
classified into primitive and non-primitive structures. Non-primitive structures are further divided into
linear and non-linear data structures. Linear structures store data sequentially, whereas non-linear
structures represent hierarchical relationships. Choosing the appropriate data structure improves the
efficiency of programs and algorithms.
b) Define Abstract Data Type (ADT). Explain the advantages of ADTs and the factors involved in
selecting a suitable data structure.
An Abstract Data Type (ADT) is a logical model of a data structure that specifies the type of data
stored and the operations that can be performed on the data without describing how the data is
implemented.
Examples of ADTs:
• Stack
• Queue
• List
• Tree
• Graph
• Push
• Pop
• Peek
• Arrays
• Linked Lists
Operations:
Representation:
Top
|30|
|20|
|10|
The user only knows the operations, not the internal implementation.
====================================
1. Data Hiding
ADT hides the internal implementation details and exposes only necessary operations.
Example:
A user can use Stack operations without knowing whether it is implemented using array or linked list.
2. Modularity
3. Reusability
Example:
• CPU scheduling
• Printer management
4. Easy Maintenance
Example:
Changing Stack implementation from array to linked list does not change Stack operations.
Since data is accessed only through defined operations, accidental modification is reduced.
6. Simplifies Programming
====================================
Choosing a suitable data structure is important for improving program efficiency and performance.
1. Time Efficiency
Operations include:
• Searching
• Insertion
• Deletion
• Traversal
Example:
2. Space Efficiency
Example:
Arrays require fixed memory, whereas linked lists use extra memory for pointers.
3. Nature of Data
Example:
4. Frequency of Operations
Example:
5. Size of Data
Example:
6. Simplicity of Implementation
7. Data Relationships
Example:
CONCLUSION
An Abstract Data Type (ADT) is a logical representation of a data structure that defines data and
operations without showing implementation details. ADTs provide advantages such as data hiding,
reusability, modularity, and easy maintenance. Selecting a suitable data structure depends on factors
like time efficiency, space efficiency, nature of data, and frequency of operations. Proper selection
improves the performance and efficiency of programs.
Data structures are used to organize and store data efficiently. Various operations are performed on
data structures to manipulate and process data according to user requirements.
1. Traversal
2. Insertion
3. Deletion
4. Searching
5. Sorting
6. Merging
====================================
1. TRAVERSAL
Traversal means visiting each element of a data structure exactly once to process or display the data.
Example:
Array:
Traversal Output:
10 20 30 40
Representation:
Index : 0 1 2 3
Data : 10 20 30 40
Algorithm Steps:
• Displaying elements
• Updating values
2. INSERTION
Insertion means adding a new element into a data structure at a specified position.
Example:
Original Array:
Insert 30 at position 3:
New Array:
Steps:
Applications:
3. DELETION
Example:
Original Array:
Delete 30:
New Array:
Steps:
• Find the element to delete.
Applications:
4. SEARCHING
Example:
Array:
Types of Searching:
• Linear Search
• Binary Search
Applications:
5. SORTING
Example:
Original Array:
Ascending Order:
Types of Sorting:
• Bubble Sort
• Selection Sort
• Insertion Sort
• Quick Sort
Applications:
• Ranking students
6. MERGING
Merging means combining two or more data structures into a single structure.
Example:
Array 1:
[10, 20]
Array 2:
[30, 40]
Merged Array:
Applications:
• Database management
====================================
1. Stack Operations
Operations:
• Push
• Pop
• Peek
Representation:
Top
|30|
|20|
|10|
Push 40:
|40|
|30|
|20|
|10|
Applications:
• Function calls
• Undo operations
2. Queue Operations
Operations:
• Enqueue
• Dequeue
Representation:
Front → 10 20 30 40 ← Rear
Applications:
• CPU scheduling
• Printer queue
====================================
1. Tree Traversal
• Inorder
• Postorder
Tree Example:
/\
B C
/\
D E
Inorder Traversal:
DBEAC
Applications:
• Expression trees
• File systems
2. Graph Traversal
Graph Representation:
A ----- B
\ /
\/
Applications:
• Social networks
• Path finding
CONCLUSION
Operations on data structures help in organizing, processing, and managing data efficiently. Common
operations include traversal, insertion, deletion, searching, sorting, and merging. Different data
structures such as arrays, stacks, queues, trees, and graphs support various operations according to
their design and applications. Efficient use of these operations improves the performance and
reliability of computer programs.
b) Discuss the importance of data structures in problem solving and software development.
Data structures are methods of organizing and storing data efficiently so that it can be accessed and
modified easily. They play a vital role in problem solving and software development because they
improve the performance, efficiency, and reliability of programs.
Choosing the correct data structure helps in reducing complexity and developing optimized software
applications.
====================================
Data structures organize data systematically, making processing easier and faster.
Example:
Appropriate data structures allow quick searching, insertion, and deletion operations.
Example:
Example:
• Binary Search works faster on sorted arrays.
Example:
Examples:
Examples:
• Searching algorithms
• Sorting algorithms
• Traversal algorithms
====================================
Database systems use trees and indexing structures for faster retrieval.
Modern software applications handle huge amounts of data. Data structures help in managing this
data efficiently.
Examples:
• Banking systems
• E-commerce websites
Data structures such as stacks, queues, and linked lists can be reused in multiple programs.
Examples:
• Searching records
• Sorting data
• Indexing
Example:
• Memory management
• Process scheduling
• File organization
Examples:
• Navigation systems
• Artificial Intelligence
• Machine Learning
• Computer Networks
====================================
1. Stack
Representation:
Top
|30|
|20|
|10|
Applications:
• Function calls
• Undo operations
2. Queue
Front → 10 20 30 40 ← Rear
Applications:
• CPU scheduling
• Printer queue
3. Tree
Representation:
/\
B C
/\
D E
Applications:
• File systems
• Database indexing
4. Graph
Representation:
A ----- B
\ /
\/
Applications:
• Social networks
• Google Maps
• Network routing
CONCLUSION
Data structures are essential for efficient problem solving and software development. They help in
organizing data, reducing execution time, improving memory usage, and simplifying complex
problems. Data structures also enhance software performance, support algorithm design, and are
widely used in operating systems, databases, networking, and real-time applications. Choosing the
appropriate data structure improves the efficiency, scalability, and reliability of software systems.
Q3. a) Define a singly linked list. Explain its structure and write algorithms for insertion and deletion
operations.
A singly linked list is a linear dynamic data structure in which each node contains two parts:
1. Data
The nodes are connected sequentially, and the last node points to NULL.
====================================
Node Structure:
[data | next]
Representation:
Where:
Characteristics:
• Sequential access
====================================
1. Insertion at Beginning
Original List:
[20|•] → [30|NULL]
Insert 10:
New List:
Algorithm:
Step 5: STOP
Pseudo Code:
HEAD = NEWNODE
--------------------------------------------------
2. Insertion at End
Original List:
[10|•] → [20|NULL]
Insert 30:
New List:
Algorithm:
Step 6: STOP
====================================
1. Deletion at Beginning
Original List:
Delete 10:
New List:
[20|•] → [30|NULL]
Algorithm:
Step 4: STOP
--------------------------------------------------
2. Deletion at End
Original List:
Delete 30:
New List:
[10|•] → [20|NULL]
Algorithm:
Step 1: Traverse till second last node
Step 5: STOP
====================================
====================================
====================================
2. Memory management
3. Polynomial representation
CONCLUSION
A singly linked list is an important dynamic linear data structure consisting of nodes connected using
pointers. It allows efficient insertion and deletion operations and is widely used in computer science
applications where dynamic memory allocation is required.
b) Explain traversal and searching operations in a singly linked list with suitable examples.
1. Data
The nodes are connected sequentially, and the last node points to NULL.
====================================
Representation:
Where:
====================================
TRAVERSAL OPERATION
Traversal means visiting each node of the linked list exactly once in order to process or display the
data.
During traversal, the nodes are accessed one by one starting from the first node until NULL is
reached.
Example:
Linked List:
Traversal Output:
10 20 30 40
Step 3: STOP
Pseudo Code:
PTR = HEAD
END WHILE
Explanation:
Applications of Traversal:
1. Displaying elements
2. Counting nodes
====================================
SEARCHING OPERATION
Searching means finding whether a particular element exists in the linked list.
The nodes are checked one by one until the required element is found or the end of the list is
reached.
Example:
Linked List:
Search Element = 30
Result:
Element Found
--------------------------------------------------
Example 2:
Search Element = 50
Result:
====================================
STOP
Pseudo Code:
PTR = HEAD
PRINT "FOUND"
EXIT
END WHILE
IF PTR = NULL
====================================
Linked List:
Search Item = 30
Step 1:
Step 2:
Step 3:
====================================
====================================
DISADVANTAGES
====================================
2. Memory management
4. Polynomial representation
CONCLUSION
Traversal and searching are important operations in a singly linked list. Traversal helps in accessing
and processing all nodes, while searching helps in locating a required element in the list. These
operations are widely used in various computer science and software development applications.
Q4.a) What is a circular linked list? Explain its advantages and applications with neat diagrams.
====================================
Node Structure:
[data | next]
Representation:
┌─────────────────────┐
↓ │
↑_____________________|
==================================================
====================================
┌─────────────────────┐
↓ │
↑_____________________|
Traversal:
10 → 20 → 30 → Back to 10
Unlike singly linked lists, traversal does not stop at NULL because the last node connects to the first
node.
====================================
1. Continuous Traversal
The list can be traversed continuously without restarting from the beginning.
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
Circular linked lists are widely used for implementing circular queues.
====================================
DISADVANTAGES OF CIRCULAR LINKED LIST
3. Debugging is difficult
====================================
1. CPU Scheduling
Used in Round Robin Scheduling where processes execute repeatedly in circular order.
--------------------------------------------------
--------------------------------------------------
3. Multiplayer Games
--------------------------------------------------
4. Circular Queues
--------------------------------------------------
--------------------------------------------------
6. Operating Systems
====================================
CONCLUSION
A circular linked list is a linked list in which the last node connects back to the first node, forming a
circular structure. It allows continuous traversal and efficient cyclic processing. Due to its advantages,
it is widely used in CPU scheduling, playlists, multiplayer games, and circular queue implementations.
b) Compare singly linked lists and circular linked lists. Explain insertion and deletion operations in
circular linked lists.
A linked list is a dynamic linear data structure consisting of nodes connected using pointers. Singly
linked lists and circular linked lists are important types of linked lists used in data management.
====================================
A singly linked list is a linear data structure in which each node contains data and a pointer to the next
node. The last node points to NULL.
Representation:
====================================
A circular linked list is a linked list in which the last node points back to the first node instead of NULL.
Representation:
┌─────────────────────┐
↓ │
[10|•] → [20|•] → [30|•]
↑_____________________|
====================================
--------------------------------------------------
2. Traversal
--------------------------------------------------
3. Structure
Linear structure.
Circular structure.
--------------------------------------------------
4. Memory Utilization
--------------------------------------------------
5. Applications
====================================
Insertion means adding a new node into the circular linked list.
1. Insertion at Beginning
Original List:
┌─────────────────┐
↓ │
[20|•] → [30|•]
↑_______________|
Insert 10:
New List:
┌──────────────────────┐
↓ │
↑______________________|
Algorithm:
Step 7: STOP
--------------------------------------------------
2. Insertion at End
Original List:
┌─────────────────┐
↓ │
[10|•] → [20|•]
↑_______________|
Insert 30:
New List:
┌──────────────────────┐
↓ │
↑______________________|
Algorithm:
====================================
1. Deletion at Beginning
Original List:
┌──────────────────────┐
↓ │
↑______________________|
Delete 10:
New List:
┌─────────────────┐
↓ │
[20|•] → [30|•]
↑_______________|
Algorithm:
Step 6: STOP
--------------------------------------------------
2. Deletion at End
Original List:
┌──────────────────────┐
↓ │
↑______________________|
Delete 30:
New List:
┌─────────────────┐
↓ │
[10|•] → [20|•]
↑_______________|
Algorithm:
Step 5: STOP
====================================
====================================
1. CPU scheduling
2. Circular queues
3. Music playlists
4. Multiplayer games
CONCLUSION
A singly linked list is a linear linked structure where the last node points to NULL, whereas a circular
linked list connects the last node back to the first node. Circular linked lists support continuous
traversal and are useful for cyclic applications. Insertion and deletion operations are efficiently
performed by adjusting node pointers properly.
Q5. a) Explain the structure of a doubly linked list. Write algorithms for insertion and deletion
operations.
A doubly linked list is a linear dynamic data structure in which each node contains:
2. Data
====================================
Node Structure:
Representation:
NULL ← [10|•|•] ⇄ [20|•|•] ⇄ [30|•|NULL]
Where:
Characteristics:
====================================
Insertion means adding a new node into the doubly linked list.
1. Insertion at Beginning
Original List:
Insert 10:
New List:
Algorithm:
Step 7: STOP
Pseudo Code:
HEAD = NEWNODE
--------------------------------------------------
2. Insertion at End
Original List:
Insert 30:
New List:
Algorithm:
Step 6: STOP
====================================
1. Deletion at Beginning
Original List:
New List:
Algorithm:
Step 5: STOP
--------------------------------------------------
2. Deletion at End
Original List:
New List:
Step 5: STOP
====================================
====================================
====================================
3. Music playlists
CONCLUSION
A doubly linked list is a dynamic linear data structure in which each node contains pointers to both
previous and next nodes. It supports bidirectional traversal and efficient insertion and deletion
operations. Due to its flexibility, it is widely used in browser navigation, playlists, and memory
management applications.
b) Discuss the advantages and disadvantages of doubly linked lists over singly linked lists.
Ans. ADVANTAGES AND DISADVANTAGES OF DOUBLY LINKED LISTS OVER SINGLY LINKED
LISTS
Linked lists are dynamic data structures used to store and manage data efficiently. Singly linked lists
and doubly linked lists are important types of linked lists used in computer science applications.
====================================
A singly linked list is a linear data structure in which each node contains:
1. Data
Representation:
====================================
2. Data
Representation:
1. Bidirectional Traversal
In a doubly linked list, traversal is possible in both forward and backward directions.
Example:
--------------------------------------------------
2. Easy Deletion of Nodes
Deletion operation is easier because the previous node can be accessed directly.
In singly linked lists, traversal from the beginning is needed to find the previous node.
--------------------------------------------------
3. Efficient InsertionInsertion before or after a node is simpler and faster because both previous and
next pointers are available.
--------------------------------------------------
4. Better Navigation
Applications:
--------------------------------------------------
Examples:
• Music playlists
• Navigation systems
====================================
• Previous pointer
• Next pointer
--------------------------------------------------
2. Complex Structure
--------------------------------------------------
4. Difficult Implementation
Programming and debugging are more difficult because of additional pointer operations.
====================================
1. Number of Pointers
--------------------------------------------------
2. Traversal
--------------------------------------------------
3. Memory Usage
--------------------------------------------------
More difficult
--------------------------------------------------
5. Complexity
Simple implementation
Complex implementation
====================================
4. Image viewers
CONCLUSION
A doubly linked list provides several advantages over a singly linked list, such as bidirectional
traversal, easy insertion, and efficient deletion operations. However, it also has disadvantages like
extra memory usage and complex implementation. Therefore, the choice between singly and doubly
linked lists depends on the requirements of the application.
Q6. a) Define Stack ADT. Explain stack operations with algorithms and examples.
A Stack Abstract Data Type (ADT) is a linear data structure that follows the LIFO (Last In First Out)
principle, where the element inserted last is removed first.
====================================
STRUCTURE OF STACK
Representation:
Top
|40|
|30|
|20|
|10|
Where:
====================================
CHARACTERISTICS OF STACK
====================================
STACK OPERATIONS
1. PUSH
2. POP
3. PEEK (TOP)
4. DISPLAY
====================================
1. PUSH OPERATION
Example:
Initial Stack:
Top
|30|
|20|
|10|
Push 40:
Top
|40|
|30|
|20|
|10|
Step 5: STOP
Pseudo Code:
IF TOP = MAX - 1
ELSE
TOP = TOP + 1
STACK[TOP] = ITEM
END IF
--------------------------------------------------
2. POP OPERATION
Example:
Initial Stack:
Top
|40|
|30|
|20|
|10|
Pop Operation:
Removed Element = 40
New Stack:
Top
|30|
|20|
|10|
Pseudo Code:
IF TOP = -1
ELSE
ITEM = STACK[TOP]
TOP = TOP - 1
END IF
--------------------------------------------------
3. PEEK OPERATION
Example:
Top
|40|
|30|
|20|
Peek Element = 40
Algorithm for PEEK:
Pseudo Code:
IF TOP = -1
ELSE
PRINT STACK[TOP]
END IF
--------------------------------------------------
4. DISPLAY OPERATION
Display operation prints all elements of the stack from TOP to bottom.
Example:
Top
|40|
|30|
|20|
|10|
Display Output:
40 30 20 10
====================================
ADVANTAGES OF STACK
1. Simple implementation
====================================
DISADVANTAGES OF STACK
3. Searching is difficult
====================================
APPLICATIONS OF STACK
2. Expression evaluation
4. Browser history
5. Parenthesis matching
CONCLUSION
A Stack ADT is a linear data structure that follows the LIFO principle. The main stack operations are
push, pop, peek, and display. Stacks are widely used in computer science applications such as
recursion, expression evaluation, browser navigation, and memory management.
b) Discuss array implementation and linked list implementation of stacks with suitable examples.
Definition of Stack:
A stack is a linear data structure that follows the LIFO (Last In First Out) principle, where the last
inserted element is removed first.
Examples:
• Stack of plates
====================================
In array implementation, stack elements are stored in a linear array and a variable TOP keeps track of
the topmost element.
====================================
Representation:
Top
|40|
|30|
|20|
|10|
Array Representation:
Index : 0 1 2 3
Data :10 20 30 40
TOP = 3
====================================
Example:
Initial Stack:
|30|
|20|
|10|
Push 40:
|40|
|30|
|20|
|10|
Algorithm:
Step 5: STOP
Pseudo Code:
IF TOP = MAX - 1
PRINT "OVERFLOW"
ELSE
TOP = TOP + 1
STACK[TOP] = ITEM
END IF
====================================
Example:
Initial Stack:
|40|
|30|
|20|
|10|
After POP:
|30|
|20|
|10|
Removed Element = 40
Algorithm:
Step 5: STOP
====================================
1. Simple implementation
====================================
DISADVANTAGES OF ARRAY IMPLEMENTATION
1. Fixed size
====================================
In linked list implementation, stack elements are stored dynamically using nodes. Each node
contains:
1. Data
====================================
Representation:
TOP
====================================
Example:
Initial Stack:
Push 40:
Algorithm:
Step 1: Create NEWNODE
Step 5: STOP
Pseudo Code:
TOP = NEWNODE
====================================
Example:
Initial Stack:
After POP:
[30|•] → [20|NULL
Deleted Element = 40
Algorithm:
====================================
1. Dynamic size
====================================
====================================
====================================
1. Memory Allocation
Array:
Linked List:
--------------------------------------------------
2. Size
Array:
Fixed size
Linked List:
Flexible size
--------------------------------------------------
3. Memory Usage
Array:
Linked List:
--------------------------------------------------
4. Implementation
Array:
Simple
Linked List:
Complex
--------------------------------------------------
5. Overflow Condition
Array:
Linked List:
====================================
APPLICATIONS OF STACK
2. Expression evaluation
5. Parenthesis matching
CONCLUSION
Stacks can be implemented using arrays or linked lists. Array implementation is simple and faster but
has fixed size limitations. Linked list implementation provides dynamic memory allocation and efficient
memory utilization but requires extra memory for pointers. The choice of implementation depends on
application requirements.
Q7. a) Explain stack applications in detail, including recursion and expression evaluation.
Definition of Stack:
A stack is a linear data structure that follows the LIFO (Last In First Out) principle, where the last
inserted element is removed first.
Representation:
Top
|40|
|30|
|20|
|10|
Stacks are widely used in computer science and software development because of their efficient
insertion and deletion operations.
====================================
APPLICATIONS OF STACK
1. Recursion
2. Expression Evaluation
3. Parenthesis Matching
6. Backtracking
7. Memory Management
====================================
1. RECURSION
Recursion is a process in which a function calls itself repeatedly until a stopping condition is reached.
Example:
Factorial of 3
Factorial Formula:
3! = 3 × 2 × 1
Recursive Function:
fact(3)
fact(2)
fact(1)
Stack Representation:
Top
↓
|fact(1)|
|fact(2)|
|fact(3)|
Execution Process:
1. fact(3) is pushed
2. fact(2) is pushed
3. fact(1) is pushed
Popping Order:
Applications of Recursion:
1. Factorial calculation
2. Fibonacci series
3. Tree traversal
4. Tower of Hanoi
====================================
2. EXPRESSION EVALUATION
1. Infix Expression
2. Prefix Expression
3. Postfix Expression
--------------------------------------------------
a) INFIX EXPRESSION
Example:
A+B
--------------------------------------------------
b) PREFIX EXPRESSION
Example:
+AB
--------------------------------------------------
c) POSTFIX EXPRESSION
Example:
AB+
Example:
Expression:
53+2*
Step 1:
Push 5
Stack:
|5|
--------------------------------------------------
Step 2:
Push 3
Stack:
|3|
|5|
--------------------------------------------------
Step 3:
Operator +
Pop 3 and 5
5+3=8
Push 8
Stack:
|8|
--------------------------------------------------
Step 4:
Push 2
Stack:
|2|
|8|
--------------------------------------------------
Step 5:
Operator *
Pop 2 and 8
8 × 2 = 16
Push 16
Final Stack:
|16|
Result = 16
ALGORITHM FOR POSTFIX EVALUATION
Step 3: If operator,
Perform operation
2. Reduces complexity
====================================
3. PARENTHESIS MATCHING
Example:
(A + B) * (C + D)
Process:
Applications:
• Expression validation
====================================
Example:
• Ctrl + Z → Undo
• Ctrl + Y → Redo
====================================
5. BROWSER HISTORY
Example:
====================================
6. BACKTRACKING
Applications:
1. Maze solving
2. Path finding
3. Puzzle solving
====================================
7. MEMORY MANAGEMENT
Stacks are used in managing function calls and local variables in memory.
====================================
ADVANTAGES OF STACK
1. Simple implementation
====================================
DISADVANTAGES OF STACK
3. Searching is difficult
CONCLUSION
Stacks are important linear data structures widely used in computer science applications. They are
essential in recursion, expression evaluation, parenthesis matching, browser history, undo operations,
and memory management. Due to their LIFO behavior, stacks efficiently manage nested and
sequential operations in software systems.
b) Write algorithms for PUSH and POP operations. Explain stack overflow and underflow conditions.
Definition of Stack:
A stack is a linear data structure that follows the LIFO (Last In First Out) principle, where the last
inserted element is removed first.
All insertion and deletion operations are performed at one end called TOP.
Representation:
Top
|40|
|30|
|20|
|10|
====================================
PUSH OPERATION
PUSH operation inserts a new element into the top of the stack.
Example:
Initial Stack:
Top
|30|
|20|
|10|
Push Element = 40
After PUSH:
Top
|40|
|30|
|20|
|10|
====================================
Algorithm:
Step 5: STOP
Pseudo Code:
IF TOP = MAX - 1
ELSE
TOP = TOP + 1
STACK[TOP] = ITEM
END IF
====================================
POP OPERATION
Example:
Initial Stack:
Top
|40|
|30|
|20|
|10|
After POP:
Top
|30|
|20|
|10|
Deleted Element = 40
====================================
Algorithm:
Step 2: If TOP = -1
STOP
Step 6: STOP
Pseudo Code:
IF TOP = -1
ELSE
ITEM = STACK[TOP]
TOP = TOP - 1
END IF
====================================
Condition:
TOP = MAX - 1
Example:
If stack size = 4
Top
|40|
|30|
|20|
|10|
STACK OVERFLOW
Causes:
Effects:
1. Program interruption
====================================
Stack Underflow occurs when a deletion (POP) operation is performed on an empty stack.
Condition:
TOP = -1
Example:
Empty Stack:
TOP = -1
STACK UNDERFLOW
Causes:
Effects:
1. Runtime errors
====================================
ADVANTAGES OF STACK
1. Simple implementation
3. Useful in recursion
====================================
APPLICATIONS OF STACK
3. Browser history
4. Expression evaluation
5. Parenthesis matching
CONCLUSION
PUSH and POP are the fundamental operations of a stack. PUSH inserts elements into the stack,
while POP removes elements from the stack according to the LIFO principle. Stack overflow occurs
when insertion is attempted on a full stack, whereas stack underflow occurs when deletion is
attempted on an empty stack. These operations are essential in recursion, memory management,
and expression evaluation applications.
Q8. a) Define Queue ADT. Explain queue operations with algorithms and examples.
A Queue Abstract Data Type (ADT) is a linear data structure that follows the FIFO (First In First Out)
principle, where the element inserted first is removed first.
Insertion is performed at the REAR end and deletion is performed at the FRONT end.
• Printer queue
====================================
STRUCTURE OF QUEUE
Representation:
Front → 10 20 30 40 ← Rear
Where:
====================================
CHARACTERISTICS OF QUEUE
====================================
QUEUE OPERATIONS
The main operations performed on a queue are:
1. ENQUEUE
2. DEQUEUE
3. PEEK (FRONT)
4. DISPLAY
====================================
1. ENQUEUE OPERATION
WORKING OF ENQUEUE
Example:
Initial Queue:
Front → 10 20 30 ← Rear
Insert 40
New Queue:
Front → 10 20 30 40 ← Rear
Algorithm:
STOP
Step 6: STOP
Pseudo Code:
IF REAR = MAX - 1
ELSE
IF FRONT = -1
FRONT = 0
END IF
REAR = REAR + 1
QUEUE[REAR] = ITEM
END IF
====================================
2. DEQUEUE OPERATION
WORKING OF DEQUEUE
Example:
Initial Queue:
Front → 10 20 30 40 ← Rear
Delete Element = 10
New Queue:
Front → 20 30 40 ← Rear
Algorithm:
STOP
Step 6: STOP
Pseudo Code:
ELSE
ITEM = QUEUE[FRONT]
FRONT = FRONT + 1
END IF
====================================
3. PEEK OPERATION
Example:
Front → 10 20 30 ← Rear
Peek Element = 10
Algorithm:
====================================
4. DISPLAY OPERATION
Example:
Front → 10 20 30 40 ← Rear
Display Output:
10 20 30 40
Algorithm:
====================================
Condition:
REAR = MAX - 1
====================================
Condition:
====================================
ADVANTAGES OF QUEUE
4. Easy implementation
====================================
DISADVANTAGES OF QUEUE
====================================
APPLICATIONS OF QUEUE
1. CPU scheduling
2. Printer spooling
4. Data buffering
====================================
CONCLUSION
A Queue ADT is a linear data structure that follows the FIFO principle. The main operations are
enqueue, dequeue, peek, and display. Queues are widely used in scheduling, buffering, networking,
and resource management applications because they process data in the order of arrival.
Definition of Queue:
A queue is a linear data structure that follows the FIFO (First In First Out) principle, where the
element inserted first is removed first.
Insertion is performed at the REAR end and deletion is performed at the FRONT end.
Representation:
Front → 10 20 30 40 ← Rear
Queues are widely used in computer science for scheduling, buffering, and resource management.
TYPES OF QUEUES
1. Simple Queue
2. Circular Queue
3. Priority Queue
====================================
1. SIMPLE QUEUE
A simple queue is a linear queue in which insertion takes place at the REAR and deletion takes place
at the FRONT.
====================================
Representation:
Front → 10 20 30 40 ← Rear
Operations:
• Enqueue
• Dequeue
====================================
1. Easy implementation
3. Efficient scheduling
====================================
1. Printer queue
3. CPU scheduling
====================================
2. CIRCULAR QUEUE
A circular queue is a queue in which the last position is connected back to the first position, forming a
circular structure.
====================================
Front
10 20 30
↑ ↓
50 ← 40
Rear
====================================
3. Faster operations
====================================
1. CPU scheduling
3. Multimedia streaming
4. Keyboard buffering
====================================
3. PRIORITY QUEUE
A priority queue is a queue in which elements are processed according to their priority rather than
insertion order.
====================================
Example:
Priority Element
1 A
2 B
3 C
====================================
====================================
====================================
2. Emergency services
3. Network routing
====================================
A deque (Double Ended Queue) is a queue in which insertion and deletion can occur at both FRONT
and REAR ends.
====================================
STRUCTURE OF DEQUE
Representation:
Front ⇄ 10 20 30 40 ⇄ Rear
TYPES OF DEQUE
====================================
ADVANTAGES OF DEQUE
2. Efficient processing
====================================
APPLICATIONS OF DEQUE
1. Browser history
3. Palindrome checking
4. Task scheduling
====================================
1. Simple Queue
• FIFO order
• Insertion at rear
• Deletion at front
--------------------------------------------------
2. Circular Queue
• Circular structure
--------------------------------------------------
3. Priority Queue
--------------------------------------------------
4. Deque
====================================
ADVANTAGES OF QUEUES
1. Efficient scheduling
CONCLUSION
Queues are important linear data structures used for orderly processing of data. Different types of
queues such as simple queue, circular queue, priority queue, and deque are designed for different
applications. They are widely used in operating systems, scheduling, networking, buffering, and real-
time processing systems.
Q9. a) Explain array implementation of queues with enqueue and dequeue algorithms.
Definition of Queue:
A queue is a linear data structure that follows the FIFO (First In First Out) principle, where the
element inserted first is removed first.
Insertion is performed at the REAR end and deletion is performed at the FRONT end.
====================================
Initially:
FRONT = -1
REAR = -1
====================================
Representation:
Front → 10 20 30 40 ← Rear
Array Representation:
Index : 0 1 2 3
Data :10 20 30 40
FRONT = 0
REAR = 3
====================================
ENQUEUE OPERATION
====================================
WORKING OF ENQUEUE
Example:
Initial Queue:
Front → 10 20 30 ← Rear
Insert 40
New Queue:
Front → 10 20 30 40 ← Rear
====================================
Algorithm:
STOP
Step 3: If FRONT = -1
Set FRONT = 0
Step 6: STOP
Pseudo Code:
IF REAR = MAX - 1
ELSE
IF FRONT = -1
FRONT = 0
END IF
REAR = REAR + 1
QUEUE[REAR] = ITEM
END IF
====================================
DEQUEUE OPERATION
WORKING OF DEQUEUE
Example:
Initial Queue:
Front → 10 20 30 40 ← Rear
Delete Element = 10
New Queue:
Front → 20 30 40 ← Rear
====================================
Algorithm:
STOP
Step 6: STOP
Pseudo Code:
ELSE
ITEM = QUEUE[FRONT]
FRONT = FRONT + 1
END IF
====================================
Condition:
REAR = MAX - 1
Example:
Queue Size = 4
Front → 10 20 30 40 ← Rear
QUEUE OVERFLOW
====================================
Definition:
Example:
Empty Queue:
FRONT = -1
REAR = -1
QUEUE UNDERFLO
====================================
1. Simple implementation
====================================
DISADVANTAGES OF ARRAY IMPLEMENTATION OF QUEUE
1. Fixed size
====================================
APPLICATIONS OF QUEUE
1. CPU scheduling
2. Printer queue
4. Data buffering
====================================
CONCLUSION
In array implementation of queue, elements are stored in a linear array using FRONT and REAR
pointers. Enqueue operation inserts elements at the rear, while dequeue operation removes elements
from the front according to FIFO principle. Array queues are simple and efficient but suffer from fixed
size limitations and memory wastage.
A circular queue is a linear data structure in which the last position is connected back to the first
position, forming a circular structure.
It follows the FIFO (First In First Out) principle and efficiently utilizes memory by reusing empty
spaces created after deletion.
====================================
Representation:
FRONT
10 20 30
↑ ↓
50 ← 40
REAR
In a circular queue:
====================================
====================================
Example:
Queue Size = 5
FRONT = 0
REAR = 4
FRONT
10 20 30
↑ ↓
50 ← 40
REAR
--------------------------------------------------
Deleted:
10 and 20
--------------------------------------------------
New elements are inserted into freed spaces at the beginning of array.
====================================
ENQUEUE OPERATION
Enqueue operation inserts an element into the circular queue at the REAR end.
====================================
Algorithm:
STOP
Step 4: Otherwise
Pseudo Code:
ELSE
IF FRONT = -1
FRONT = 0
REAR = 0
ELSE
END IF
QUEUE[REAR] = ITEM
END IF
====================================
DEQUEUE OPERATION
Dequeue operation removes an element from the FRONT end of the circular queue.
====================================
Algorithm:
Step 2: If FRONT = -1
STOP
Else
Step 6: STOP
Pseudo Code:
IF FRONT = -1
ELSE
ITEM = QUEUE[FRONT]
IF FRONT = REAR
FRONT = -1
REAR = -1
ELSE
END IF
END IF
====================================
====================================
1. Complex implementation
2. Difficult pointer management
====================================
1. CPU scheduling
3. Multimedia streaming
4. Keyboard buffering
====================================
Simple Queue:
• Linear structure
Circular Queue:
• Circular structure
CONCLUSION
A circular queue is an improved form of queue in which the last position connects back to the first
position. It efficiently utilizes memory by reusing vacant spaces and supports FIFO processing.
Circular queues are widely used in scheduling, buffering, and real-time processing applications.
Stacks and queues are important linear data structures used for storing and processing data
efficiently. Both support insertion and deletion operations, but they differ in the order in which
elements are processed.
====================================
STACK
A stack is a linear data structure that follows the LIFO (Last In First Out) principle, where the last
inserted element is removed first.
====================================
STRUCTURE OF STACK
Representation:
Top
|40|
|30|
|20|
|10|
Operations:
1. PUSH
2. POP
3. PEEK
Example:
Push 50:
Top
|50|
|40|
|30|
|20|
|10|
After POP:
Top
|40|
|30|
|20|
|10|
====================================
QUEUE
A queue is a linear data structure that follows the FIFO (First In First Out) principle, where the first
inserted element is removed first.
====================================
STRUCTURE OF QUEUE
Representation:
Front → 10 20 30 40 ← Rear
Operations:
1. ENQUEUE
2. DEQUEUE
3. PEEK
Example:
Enqueue 50:
Front → 10 20 30 40 50 ← Rear
After DEQUEUE:
Front → 20 30 40 50 ← Rear
Deleted Element = 10
====================================
1. Principle Followed
Stack:
Queue:
--------------------------------------------------
Stack:
Queue:
--------------------------------------------------
3. Order of Processing
Stack:
Queue:
--------------------------------------------------
4. Main Operations
Stack:
Queue:
--------------------------------------------------
5. Pointers Used
Stack:
Queue:
Uses FRONT and REAR pointers
--------------------------------------------------
6. Structure Representation
Stack:
Vertical arrangement
Queue:
Linear arrangement
--------------------------------------------------
7. Complexity
Stack:
Simple implementation
Queue:
====================================
ADVANTAGES OF STACK
1. Simple implementation
====================================
ADVANTAGES OF QUEUE
2. Efficient scheduling
====================================
APPLICATIONS OF STACK
1. Function call management
2. Recursion
3. Expression evaluation
5. Browser history
6. Parenthesis matching
====================================
APPLICATIONS OF QUEUE
1. CPU scheduling
2. Printer queue
5. Data buffering
====================================
Stack Example:
Stack of plates:
--------------------------------------------------
Queue Example:
CONCLUSION
Stacks and queues are fundamental linear data structures used for efficient data management. A
stack follows the LIFO principle and is mainly used in recursion, expression evaluation, and undo
operations. A queue follows the FIFO principle and is widely used in scheduling, buffering, and
resource management systems. The choice between stack and queue depends on the application
requirements.
b) Discuss real-life applications of stacks and queues in computer science.
Stacks and queues are important linear data structures widely used in computer science and software
development. They help in organizing, processing, and managing data efficiently according to specific
principles.
A stack follows the LIFO (Last In First Out) principle, while a queue follows the FIFO (First In First
Out) principle.
====================================
STACK
A stack is a linear data structure in which insertion and deletion occur at one end called TOP.
Representation:
Top
|40|
|30|
|20|
|10|
Main Operations:
1. PUSH
2. POP
3. PEEK
====================================
Example:
--------------------------------------------------
2. Recursion
Example:
Factorial calculation
Fibonacci series
Stack stores:
• Function calls
• Return addresses
• Local variables
--------------------------------------------------
3. Expression Evaluation
• Postfix evaluation
• Prefix evaluation
Example:
Expression:
53+2*
Result = 16
--------------------------------------------------
• Ctrl + Z → Undo
• Ctrl + Y → Redo
--------------------------------------------------
5. Browser History
Applications:
• Back button
• Forward button
--------------------------------------------------
6. Parenthesis Matching
Example:
(A + B) * (C + D)
Used in:
• Compiler design
• Syntax checking
--------------------------------------------------
7. Backtracking Algorithms
Applications:
1. Maze solving
2. Puzzle solving
3. Path finding
--------------------------------------------------
8. Memory Management
Operating systems use stacks for:
• Function execution
• Interrupt handling
====================================
QUEUE
A queue is a linear data structure in which insertion occurs at REAR and deletion occurs at FRONT.
Representation:
Front → 10 20 30 40 ← Rear
Main Operations:
1. ENQUEUE
2. DEQUEUE
3. PEEK
====================================
1. CPU Scheduling
Example:
--------------------------------------------------
2. Printer Queue
--------------------------------------------------
Applications:
• Railway booking
--------------------------------------------------
--------------------------------------------------
5. Data Buffering
Examples:
• Keyboard buffering
• Video streaming
• Audio streaming
--------------------------------------------------
--------------------------------------------------
Applications:
--------------------------------------------------
8. Task Scheduling
• Operating systems
• Real-time systems
====================================
Stack:
Queue:
====================================
CONCLUSION
Stacks and queues are fundamental data structures widely used in computer science applications.
Stacks are mainly used in recursion, expression evaluation, browser history, and undo operations,
whereas queues are used in scheduling, buffering, networking, and resource management systems.
Their efficient processing methods improve the performance and reliability of software applications.
Ans. A data structure is a method of organizing and storing data in a computer so that it can be
accessed and modified efficiently.
Examples:
• Array
• Linked List
• Stack
• Queue
Ans. An Abstract Data Type (ADT) is a logical model that defines data and the operations performed
on it without specifying implementation details.
Examples:
• Stack ADT
• Queue ADT
Ans. A linked list is a dynamic linear data structure consisting of nodes connected using pointers.
Representation:
Representation:
[data | next]
6. What is the Difference Between Singly Linked List and Doubly Linked List?
Representation:
[10|•] → [20|NULL]
Representation:
Ans. A stack is a linear data structure in which insertion and deletion occur at one end called TOP.
Principle:
Representation:
Top
|30|
|20|
|10|
Condition:
TOP = MAX - 1
Ans. A queue is a linear data structure in which insertion occurs at REAR and deletion occurs at
FRONT.
Principle:
Representation:
Front → 10 20 30 ← Rear
Ans. Enqueue:
Dequeue: