1) Define linear data structure and non-linear data structure
Factor Linear Data Structure Non-Linear Data Structure
Data Element In a linear data structure, data In a non-linear data structure, data
Arrangement elements are sequentially connected, elements are hierarchically
allowing users to traverse all elements connected, appearing on multiple
in one run. levels.
Implementation Linear data structures are relatively Non-linear data structures require a
Complexity easier to implement. higher level of understanding and
are more complex to implement.
Levels All data elements in a linear data Data elements in a non-linear data
structure exist on a single level. structure span multiple levels.
Traversal A linear data structure can be Traversing a non-linear data
traversed in a single run. structure is more complex, requiring
multiple runs.
Memory Utilization Linear data structures do not Non-linear data structures are more
efficiently utilize memory. memory-friendly.
2) State any four applications of queue
1. CPU Scheduling:
Queues help manage the order in which processes are allocated CPU time. This ensures
that processes are executed efficiently and fairly.
2. Website Traffic Handling:
A queue can manage incoming HTTP requests, preventing the server from being
overwhelmed. This is crucial for website performance and stability.
3. Printer Spooling:
Queues are used to store print jobs before they are sent to the printer. This prevents the
user interface from being blocked while the printer is busy.
4. Network Packet Transmission:
In routers, queues are used to manage the order in which network packets are transmitted
or discarded. This is important for maintaining network stability and efficiency.
3) Define Abstract data type.
An Abstract Data Type (ADT) is a conceptual model that defines a set of
operations an An Abstract Data Type (ADT) is a conceptual model that
defines a set of operations and behaviors for a data structure, without
specifying how these operations are implemented or how data is organized
in memory. The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented. It does not
specify how data will be organized in memory and what algorithms will be
used for implementing the operations.
pe
5) Write any two operations performed on the stack.
• Push: Adds an element to the top of the stack.
• Pop: Removes and returns the top element from the stack.
• Peek (or Top): Returns the top element of the stack without removing it.
• IsEmpty: Checks if the stack is empty.
4) Write any four operations performed on data structure.
Four fundamental operations performed on data structures are searching,
inserting, deleting, and sorting. These operations allow for the manipulation
and organization of data within a structure, enabling efficient retrieval,
modification, and maintenance of information.
1. Searching: Searching involves finding a specific element within a data
structure based on a given key or criteria.
2. Inserting: Inserting adds new elements into the data structure, maintaining
its integrity and structure.
3. Deleting: Deleting removes elements from the data structure, potentially
altering its size and structure.
4. Sorting: Sorting arranges the elements within the data structure in a specific
order, such as ascending or descending, based on a defined criteria.
6) State any two differences between linear search and binary search.
Parameters Linear Search Binary Search
Linear Search sequentially checks each element
Binary Search continuously divides the sorted list,
Definition in the list until it finds a match or exhausts the
comparing the middle element with the target value.
list.
Time The time complexity is O(n), where n is the The time complexity is O(log n), making it faster for
Complexity number of elements in the list. larger datasets.
Efficiency Less efficient, especially for large datasets. More efficient, especially for large datasets.
Data
Does not require the list to be sorted. Requires the list to be sorted.
Requirement
Implementation Easier to implement. Requires a more complex implementation.
7) Describe directed and undirected graph
A directed graph (also called a digraph) has edges with a specific direction,
like arrows, indicating a one-way relationship between nodes. In an undirected
graph, edges have no direction, representing a two-way connection between
nodes.
Directed Graph:
• Edges have direction: An edge from node A to node B is different from an edge
from node B to A.
• Representation: Often visualized with arrows on the edges.
• Examples: Social networks where one user follows another, or a workflow
diagram showing the order of tasks.
• Directed graph edge (vi, vj) indicates a path from vertex vi to vertex vj .
• For a graph that is weakly connected, the edges can be represented as a
directed graph by removing all edges and replacing them with a directed edge
from the other node to the original node .
Undirected Graph:
• Edges have no direction: An edge between node A and node B is the same as
an edge between node B and node A.
• Representation: Edges are usually drawn as lines without arrows.
• Examples: A map showing roads where you can travel in either direction, or a
network of friends where everyone can call each other.
• Undirected graph edge {vi, vj} represents an edge between vertices vi and vj .
• For a graph that is strongly connected, the edges can be represented as an
undirected graph by representing them as a two-way edg
8) Define : (i) Binary tree (ii) Binary search tree
Binary Tree:
• A binary tree is a data structure where each node can have at most two children,
a left child and a right child.
• It's a hierarchical structure, meaning nodes are organized in a parent-child
relationship.
• This structure allows for efficient traversal and manipulation of data, making it
useful in various applications.
Binary Search Tree:
• A BST is a specialized binary tree where the values of nodes are arranged in a
specific order.
• The left subtree of a node contains only values less than the node's value, and
the right subtree contains only values greater than the node's value.
• This ordering makes it efficient for searching, insertion, and deletion
1) Write an algorithm to count number of nodes in singly linked list.
1. Initialize a variable count to 0.
2. Start at the head of the linked list.
3. Traverse the linked list using a loop.
4. For each node visited, increment the count by 1.
5. Continue this process until reaching the end of the list (i.e., when the next
pointer of the current node is NULL).
6. Return the value of count as the total number of nodes in the linked list.;
Here's a simple example of the algorithm in pseudocode:
function countNodes(head):
count = 0
current = head
while current is not NULL:
count = count + 1
current = current.next
return count
2) Write an algorithm to delete a node from the beginning of a circular
linked list.
• If head is NULL:
→ List is empty; return NULL.
• If head->next == head:
→ Only one node in the list.
→ Free/delete the head and return NULL.
• Otherwise:
a. Find the last node (node whose next points to head).
b. Point last node’s next to head->next.
c. Store head in a temporary pointer.
d. Move head to head->next.
e. Free/delete the old head.
f. Return the new head.
Code:
function deleteFromBeginning(head):
if head == NULL:
return NULL
if head.next == head:
free(head)
return NULL
temp ← head
last ← head
while last.next != head:
last ← last.next
last.next ← head.next
head ← head.next
free(temp)
return head
3) Write an algorithm to insert a new node at the beginning in linear
list.
Step 1: IF pointer = NULL Then Go to step 7 that is exit otherwise follow step2 [END OF IF]
Step 2: SET New_Node = pointer.
Step 3: SET pointer = pointer → next.
Step 4: SET New_Node → data= value.
Step 5: SET New_Node →next =head.
Step 6: SET head = New_Node.
Step 7: Exit.
Pseudocode:
function insertAtBeginning(head, data):
newNode ← allocate memory for node
newNode.data ← data
newNode.next ← head
head ← newNode
return head
4) Write algorithm for preorder traversal of binary tree.
Algorithm:
f root is NULL, return.
Visit (process/print) root->data.
Call preorder(root->left).
Call preorder(root->right).
🧾 Pseudocode:
function preorder(root):
if root is NULL:
return
visit(root)
preorder(root.left)
preorder(root.right)
5) Write an algorithm to delete a node at the beginning from a singly
Linked List
1. If head == NULL:
→ List is empty; return NULL.
2. Store head in a temporary pointer temp.
3. Move head to the next node (head = head->next).
4. Free/delete the memory of temp.
5. Return the updated head.
🧾 Pseudocode:
function deleteFromBeginning(head):
if head == NULL:
return NULL
temp ← head
head ← head.next
free(temp)
return head
6) Write an algorithm to delete an intermediate node in a singly
linked list.
Steps:
1. If head == NULL or pos < 2, return head (invalid case).
2. Initialize current to head.
3. Loop from i = 1 to pos - 2:
a. Move current to current->next.
b. If current == NULL or current->next == NULL, return head (position
invalid or it's the last node).
4. Store current->next (the node to delete) in temp.
5. Point current->next to temp->next.
6. Free/delete temp.
7. Return head.
7) Write algorithm for performing push and pop operations on stack.
Push operation can be performed in the below steps
Step 1 − Checks stack has some space or stack is full.
Step 2 − If the stack has no space then display “overflow” and exit.
Step 3 − If the stack has space then increase top by 1 to point next empty space.
Step 4 − Adds item to the newly stack location, where top is pointing.
Step 5 – PUSH operation performed successfully.
POP operation can be Performed in the below steps
Step 1 − Checks stack has some element or stack is empty.
Step 2 − If the stack has no element means it is empty then display “underflow”
Step 3 − If the stack has element some element, accesses the data element at which top is
pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − POP operation performed successfully.
8) Write an algorithm to insert an element at the beginning and at
end of linked list
Steps for Insertion at Beginning:
1. Allocate memory for a new node.
2. Set the new node's data field to the given data.
3. Set the new node's next pointer to point to the current head of the list.
4. Update the head to point to the new node.
Steps for Insertion at End:
1. Allocate memory for a new node.
2. Set the new node's data field to the given data.
3. Set the new node's next pointer to NULL.
4. If head is NULL, set head to the new node.
5. Otherwise, traverse the list until the last node (where next == NULL).
6. Set the last node's next pointer to point to the new node.
9) Write an algorithm to search an element in linked list.
Steps for Searching an Element:
1. Initialize a pointer current to the head.
2. While current is not NULL:
a. If current->data == target, return TRUE (element found).
b. Move current to current->next.
3. If current becomes NULL (end of list), return FALSE (element not found).
10) Write an algorithm to search a particular node in the given linked list.
Steps for Searching for a Particular Node:
1. Initialize a pointer current to the head.
2. While current is not NULL:
a. If current == targetNode, return TRUE (target node found).
b. Move current to current->next.
3. If current becomes NULL (end of list), return FALSE (target node not found).