UNIT-IV Linked List
UNIT-IV Linked List
Author:
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-IV : Linked List: Total Marks: 16
1. Describe any two terms from the following: Node, Null pointer, empty list with respect to
linear linked list with diagram.
Node:
A linked list is a linear data structure where each element is a separate object.
Each element is called as a node of a linked list.
Every node consists of two fields, first is data and second is address of next node.
Null pointer:
Empty list:
A linked list is said to be empty if head node contains NULL pointer. i.e. head=NULL.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
2. Describe any two terms from the following: information Field/ Data field, Address, Next
Pointer with respect to circular linked list with diagram.
Next Pointer:
Address:
In CLL, every node consists of two fields, first is data and second is address.
Address field contain the memory address of next node.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
3. List and define operations of linked list.
The basic operations that can be performed on a list are:
1. Creation: This operation is used to create a node in the linked list.
2. Insertion: This operation is used to insert a new node in the linked list.
● Inser ng node at the beginning of the list.
● Inser ng node at the middle of the list.
● Inser ng node at the end of the list.
3. Deletion: This operation is used to delete node from the linked list.
● Dele ng node from the beginning of the list.
● Dele ng node from the middle of the list.
● Dele ng node from the end of the list.
4. Traversing: It is a process of going through all the nodes of a linked list from one end to the
other end.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
4. Write algorithm for ‘search’ operation in an unsorted linked list.
● Algorithm:
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
5. WAP to search an element in a linked list.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
6. Explain insertion at the beginning and at end operations on linked list with example.
In a single linked list, the insertion operation can be performed in three ways.
They are as follows.
1. Inserting at Beginning of the list
2. Inserting at End of the list
3. Inserting at Middle of the list
Inserting At Beginning of the list:
In this operation, new node can be created and added at the beginning of a list.
New node points to existing first node and after that make new node as head node
In this operation, first we create new node q and assign value to data and next field.
Assign head to next field of q node.
At last, make q node as head node.
In this operation, new node can be created and added at the end of a list.
In this operation, first we create new node q and assign value to data and next field.
Then we assign head to pointer p and traverse pointer p to last node.
At last make the link between p and q.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
7. Write a ‘C’ program to insert new node at the end of linear linked list.
node* Insert_At_End(node *head)
{
if(head==NULL)
{
printf("\nLinked list is Empty hence unable to insert node at end");
}
else
{
node *q,*p;
int x;
printf("\nEnter Data for insertion:");
scanf("%d",&x);
q=(node*)malloc(sizeof(node));
q->data=x;
q->next=NULL;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
//insert node at end
p->next=q;
return(head);
}
}
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
8. Write algorithm to delete an intermediate node from a Singly Linked List.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
9. Write an algorithm to insert a new node as the last of a singly linked list. Give example.
In this operation, new node can be created and added at the end of a list.
Algorithm:
1. Start
2. Allocates memory for the new node q.
q=(node*)malloc(sizeof(node));
3. Assign data to the data field and NULL to the next field of the new node q.
q->data=x;
q->next=NULL;
4. Check whether linked list is Empty if (head is ‘NULL’) then
print “Linked list is empty” and goto step 7
5. Otherwise initialize p with head and keep moving the p to its next node until it
reaches to the last node in the list
p=head
while(p->next !=NULL)
p=p->next;
7. Stop
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
10. Write an algorithm to traverse a singly linked list.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
11. Write an algorithm to count no of nodes in the singly linked list.
12. 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.
13. Create a Singly Linked List using data fields 10, 20, 30, 40, 50. Search a node 40 from the SLL and
show procedure step-by-step with the help of diagram from start to end.
14. Describe advantage of circular linked list over linear linked list with example.
It is possible to traverse from the last node back to the first i.e. the head node.
The starting node does not matter as we can traverse each and every node
The previous node can be easily identified.
No requirement for a NULL assignment in the code. The circular list never points
to a NULL pointer unless fully deallocated.
Circular linked list also performs all regular functions of a singly linked list.
Circular linked lists are advantageous for end operations since beginning and end
coincide.
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
15. With example, describe how circular linked list works when a node is deleted from beginning
of list
In this operation, first node is deleted from the linked list.
● Algorithm:
1. Start
2. Create temporary node pointer variable q & p.
3. Check whether linked list is Empty. i.e if (head = = NULL) then
Display 'List is Empty,Deletion is not possible' and goto step 7.
4. Assign address of first node head to p i.e p=head.
5. Move the p node to next node until it reaches to the last node. i.e
while(p->next!=head)
{
p=p->next;
}
6. Set q=head, head=head->next, p->next=head and delete q node.
7. Stop
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
16. Difference between Array and Linked List:
DS IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14