0% found this document useful (0 votes)
2 views6 pages

DSU Unit 3 Q and A

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Unit-3 Linked Lists

1. Write an algorithm to insert a new node at the beginning of a Singly


linked list. Give example.
Algorithm to Insert a New Node at the Beginning of a Singly Linked List
Step 1: Create a new node with the given data.
Step 2: Set the `next` pointer of the new node to the current head of the list.
Step 3: Update the head of the list to point to the new node.
Step 4: The new node is now the first node in the list.
Example: -
Let's consider a linked list with nodes containing the values `10 -> 20 -> 30`.
Now, we want to insert a new node with the value `5` at the beginning of the
list.
Before Insertion:
Head -> 10 -> 20 -> 30 -> NULL
Step-by-Step Insertion:
1. Create a new node with data `5`:
- New Node: `5 -> NULL`
2. Set the `next` pointer of the new node to the current head (`10`):
- New Node: `5 -> 10 -> 20 -> 30 -> NULL`
3. Update the head to point to the new node:
- Head now points to `5`
The final linked list after inserting `5` at the beginning is:
5 -> 10 -> 20 -> 30 -> NULL

2. Describe circular linked list with suitable diagram. Also state


advantage of circular linked list over linear linked list.
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 circular chain. Unlike a linear linked list, which
has a `NULL` pointer at the end, a circular linked list's `next` pointer of the
last node points to the head of the list.
Structure of a Circular Linked List
- Nodes: Each node contains two parts:
- Data: Stores the actual data.
- Next: A pointer that points to the next node in the sequence.
- Head: The first node in the list, where the list starts.
Diagram
Consider a circular linked list with four nodes containing values `10`, `20`,
`30`, and `40`.
Head
|
[10] -> [20] -> [30] -> [40]
^ |
|_________________________|
Advantages of Circular Linked List Over Linear Linked List
1. Efficient Traversal:
- In a circular linked list, you can easily traverse the entire list starting from
any node and eventually come back to the starting node, making it ideal for
applications that require a circular traversal, like round-robin scheduling.
2. No Special Case for End Node:
- Unlike a linear linked list, where you need to handle the end node
separately (as its `next` is `NULL`), in a circular linked list, all nodes are
treated uniformly, simplifying certain operations.
3. Efficient Queue Implementation:
- A circular linked list can be used to efficiently implement a circular queue.
The front and rear pointers can easily be managed without the need for
shifting elements, as is required in a linear array implementation.
4. Memory Utilization:
- Circular linked lists can sometimes be more memory efficient because they
eliminate the need for extra pointers (like `tail` or `previous`) in certain
implementations, especially when circular traversal is needed.

3. Write an algorithm to search an element in linked list.


Algorithm Search in LinkedList (head, target):
1. Initialize a pointer, current, to head.
2. Initialize a variable, position, to 1 to keep track of the node's position.
3. While current is not NULL:
a. If current. Data equal’s target:
i. Return position (target is found).
b. Move current to the next node.
c. Increment position by 1.
4. If current becomes NULL and the target is not found:
a. Return -1 (target is not present in the list).

4. Create a Singly linked list using data fields 70, 50, 30, 40, 90. Search a
node 40 from the singly linked list & show procedure step-by-step
with the help of diagram from start to end.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
int searchNode(struct Node* head, int target) {
struct Node* current = head;
int position = 1;
while (current != NULL) {
if (current->data == target) {
return position;
}
current = current->next;
position++;
}
return -1; // Element not found
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 70);
insertAtEnd(&head, 50);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
insertAtEnd(&head, 90);
printf("Created Linked List: ");
printList(head);
int target = 40;
int position = searchNode(head, target);
if (position != -1) {
printf("Element %d found at position %d in the linked list.\n", target,
position);
} else {
printf("Element %d not found in the linked list.\n", target);
}
return 0;
}
Step-by-Step Explanation and Diagram
1. Creating the Linked List:
- We begin by creating the linked list with the nodes containing values `70`,
`50`, `30`, `40`, and `90` in that order.
- Initial List:
Head -> 70 -> 50 -> 30 -> 40 -> 90 -> NULL
2. Search Procedure:
- We search for the node containing the value `40`.
- Step-by-Step Search:
- Start at the head (`70`).
- Move to the next node (`50`).
- Move to the next node (`30`).
- Move to the next node (`40`), which is the target value.
- Traversal Visualization:
Head -> 70 -> 50 -> 30 -> [40] -> 90 -> NULL
^
|
Target found
3. Output:
- The program will output that the element `40` is found at position `4` in the
linked list.
Program Output
Created Linked List: 70 -> 50 -> 30 -> 40 -> 90 -> NULL
Element 40 found at position 4 in the linked list.

5. Define term pointer and null pointer.


A pointer is a variable that stores the memory address of another variable,
allowing direct access and manipulation of memory. A null pointer is a special
type of pointer initialized to `NULL`, meaning it doesn't point to any valid
memory address, often used to indicate that the pointer is not currently in use
or points to nothing.
6. Create singly linked list using data fields 10, 20, 30, 40, 50 and show
step-by step procedure with the help of diagram from start to end.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode; // If list is empty, new node becomes head
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next; // Traverse to the last node
}
temp->next = newNode; // Insert new node at the end
}
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
insertAtEnd(&head, 50);
printf("Created Linked List: ");
printList(head);
return 0;
}
7. Write an algorithm to count no. of nodes in singly linked list.
Algorithm Count Nodes in Linked List(head):
1. Initialize a variable, count, to 0.
2. Initialize a pointer, current, to head.
3. While current is not NULL:
a. Increment count by 1.
b. Move current to the next node.
4. Return count as the total number of nodes.

8. Explain operation on singly linked list.

9. Write an algorithm to delete a node at the beginning from a singly Linked


List.
10. Create a singly Linked List using data fields 10, 20, 30, 40, 50 and show
procedure step-by-step with the help of diagram from start to end.
11. Create a singly linked list using data fields 10, 20, 30, 40, 50. Search a
node 40 from the singly linked list and show procedure step-by-step with
the help of the diagram from start to end.
12. Construct a singly linked list using data fields 21, 25, 96, 58, 74 and
show procedure step-by-step with the help of diagram start to end.
13. Compare Linked List and Array. (any 4 points)
14. Write an algorithm to search a particular node in the given linked list.
15. Show with suitable diagrams how to delete a node from singly linked list
at the beginning, in between and at the end of the list.
16. Explain node structure for single linked list. Also write advantages of
singly list over array. (any Two)
17. Compare linear list with circular list.
18. Write an algorithm to insert a new node at the beginning in linear list.
19. Write the ‘C’ function for: (i) searching a node in single linked list. (ii)
counting number of nodes in single linked list.
20. Create a singly linked list using data fields 15, 20, 22, 58, 60. Search a
node 22 from the SLL and show procedure step-by-step with the help of
diagram from start to end.
21. Write an algorithm to delete a node from the beginning of a circular
linked list.
22. Create a singly linked list using data fields 90, 25, 46, 39, 56. Search a
node 40 from the SLL and show procedure step-by-step with the help of
diagram from start to end.
23.

You might also like