ds answer 1
ds answer 1
IAT-I
PART-A
Ans: A linear data structure is a type of data structure in which elements are stored sequentially.
ANS: A non-linear data structure is a type of data structure where elements are not stored in a
sequential order.
3. What is abstract data type? What are all not concerned in an ADT?
ANS: An Abstract Data Type (ADT) is a high-level description of a data structure that specifies
the operations that can be performed on the data, without specifying the implementation
details.
Data structures are specialized formats for organizing, storing, and managing data in a computer
so that it can be accessed and modified efficiently.
5. Define Stack.
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle.
ANS :A- B C - D +
7. Define Queue.
A queue is a linear data structure that follows the First In, First Out (FIFO) principle.
ANS:The queue is empty if the size is 0, or the front pointer equals the rear pointer (depending
on the implementation).
9. Define Circular Queue.
ANS:A circular queue is a type of queue where the last position is connected back to the first
position, making the queue circular.
ANS:
Expression evaluation
Function call management
Undo / redo mechanism
Balanced parantheses checking
PART-B
11. (a) Explain in detail about Singly linked list and its all operations.
Singly Linked List:
A Singly Linked List is a linear data structure where each node contains two parts:
1. Data: Stores the value or information.
2. Next: A pointer/reference to the next node in the sequence.
The last node’s "next" pointer points to NULL, marking the end of the list. The list can grow or
shrink dynamically.
Operations in Singly Linked List:
1. Insertion:
o At the beginning: A new node is created, and its next is set to the current head.
The head is then updated to the new node.
2. Deletion:
o From the beginning: The head pointer is moved to the second node, effectively
removing the first node.
3. Traversal: Traverse the list from the head to print or process each node's data.
4. Search: Traverse the list to find a node with a given value, returning the node if found.
5. Reversing: Reverse the order of nodes by changing the direction of the next pointers.
(Or)
11.(B) Explain the insertion operation in linked list. How nodes are inserted after a specified
node.
Insertion Operation in Linked List:
The insertion operation in a linked list involves adding a new node at a specific position in the
list. There are three common types of insertion:
1. Insertion at the Beginning:
o Create a new node.
2. Insertion at the End:
o Create a new node.
3. Insertion after a Specified Node:
o Traverse to the specified node (say X).
o Create a new node.Example for Insertion after a Specified Node:
Before insertion: 1 -> 2 -> 4 -> NULL
Insert 3 after node 2:
o Set node 2's next to point to the new node (with data 3).
o Set the new node's next to point to node 4.
After insertion: 1 -> 2 -> 3 -> 4 -> NULL
12. (a) Explain in detail about Circular linked list and all its operations.
Circular Linked List:
A Circular Linked List is a type of linked list where the last node points back to the first node,
forming a circle. It can be singly circular (where each node points to the next) or doubly circular
(where nodes point both to the next and the previous).
Operations:
1. Insertion:
o At the beginning: Insert a new node, set its next to the head, and update the last
node’s next to the new node.
o At the end: Insert a new node after the last node, update the last node’s next to
the new node, and point the new node's next to the head.
o After a specified node: Insert the new node after the given node by adjusting
pointers.
2. Deletion:
o From the beginning: Update the head to the second node and adjust the last
node’s next.
o From the end: Traverse to the second-last node and update its next to the head.
o At a specific position: Adjust pointers to remove the node.
3. Traversal: Start from the head and continue until you reach the head again.
(or)
(b) Explain the operation of traversing linked list. Write the algorithm and give an example.
13. (a) Write an algorithm for Push and Pop operations on Stack using Linked list.
ANS:Push Operation (Using Linked List):
1. Create a new node with the given value.
2. Set the new node's next to the current top.
3. Update top to the new node.
Pop Operation (Using Linked List):
1. Check if the stack is empty.
2. If not empty, store the data of the top node.
3. Update top to point to the next node.
4. Return the stored data (the popped value).
Example:
Push: Adds a new node at the top.
Pop: Removes the top node and returns its value.
(or)
(b) Explain the linked list implementation of stack ADT in detail?
ANS:Linked List Implementation of Stack ADT:
In this implementation, a stack is represented by a singly linked list, where the top of the
stack is the head of the list.
Operations:
1. Push:
o Create a new node.
o Set the new node's next to the current top.
o Update the top to the new node.
2. Pop:
o If the stack is not empty, store the data of the top node.
o Update the top to the next node.
o Return the stored data (popped value).
3. Peek/Top:
o Return the data of the top node without removing it.
4. isEmpty:
o Check if the top is NULL (empty stack).
14. (a) Explain in detail about priority queue ADT in detail? (13)
Priority Queue ADT:
A Priority Queue is an abstract data type (ADT) that operates like a regular queue but with an
additional priority feature. In a priority queue, each element is associated with a priority.
Elements with higher priority are dequeued before those with lower priority, regardless of their
order in the queue.
Key Operations:
1. Insert: Adds an element to the queue with a specified priority.
2. Delete/Remove (Dequeue): Removes and returns the element with the highest priority.
3. Peek/Top: Returns the element with the highest priority without removing it.
4. isEmpty: Checks if the priority queue is empty.
Types of Priority Queues:
1. Min-priority queue: The element with the smallest priority value is dequeued first.
2. Max-priority queue: The element with the largest priority value is dequeued first.
(Or)
(OR)
(b) Explain the addition and deletion operations performed on a circular queue with algorithms.
Circular Queue Operations:
1. Enqueue (Addition):
Adds an element at the rear of the queue.
Algorithm:
1. Check if the queue is full: (rear + 1) % size == front.
2. If full, return "Queue Overflow".
3. Otherwise, insert the element at rear.
4. Update rear: rear = (rear + 1) % size.
2. Dequeue (Deletion):
Removes an element from the front of the queue.
Algorithm:
1. Check if the queue is empty: front == rear.
2. If empty, return "Queue Underflow".
3. Otherwise, remove the element at front.
4. Update front: front = (front + 1) % size.
PART-C
16.(a) Implement the concept of singly linked list using array with example.
Singly Linked List Using Array
In this array-based implementation of a singly linked list, we use two arrays:
1. data[]: Stores the values of the nodes.
2. next[]: Stores the index of the next node, or -1 for the last node.
Operations:
Insert at End: Add a node to the end.
Traverse: Print all nodes.
(or)
16(b) Write an ADT to implement stack of size N using an array. The elements in the stack are to
be integers. The operations to be supported are PUSH, POP and DISPLAY. Take into account the
exceptions of stack overflow and stack underflow.
class Stack:
def __init__(self, size):
self.stack = [None] * size
self.top = -1
self.size = size
def push(self, value):
if self.top == self.size - 1:
print("Stack Overflow")
else:
self.top += 1
stack.display()
class Stack:
def __init__(self, size):
self.stack = [None] * size
self.top = -1
self.size = size