Unit II Linked List 2
Unit II Linked List 2
Each structure of the list is called a node and consists of two fields, one containing the
data and the other containing the address of the next item in the list.
A linked list is therefore a collection of structures not ordered by their physical
placements in the memory (like an array) but by logical links that are stored as a part of
the data in the structure itself.
2. Doubly Linked List: Each node has two pointers - one to the next node and
another to the previous node. It allows for traversal in both directions (forward
and backward). It enables efficient insertion/deletion at any position as you can
navigate from either side.
head null
prev data next prev data next prev data next
null tail
4. Circular Doubly Linked List: It combines the features of doubly linked list and
circular linked list. Each node has two pointers: one to the next and one to the
previous node, forming a closed loop. It offers efficient bi-directional traversal and
insertion/deletion at any point within the circular structure.
head prev data next prev data next prev data next
What is the type of linked list called if the last node points to the first node?
If the last node of a linked list points back to the first node, it is called a "circular
linked list".
In what condition the list converted to circular link list. Give example.
A list can be converted to a circular linked list by making the last node of the list point to
the first node, forming a loop. To convert a regular linked list into a circular linked list,
the next pointer of the last node is modified to point back to the first node.
Consider the following regular linked list:
1 -> 2 -> 3 -> 4 -> null
To convert it into a circular linked list, we make the last node (4) point back to the first
node (1):
1 -> 2 -> 3 -> 4 -> (points back to 1)
Now, the last node (4) has its next pointer pointing to the first node (1), creating a loop
or circular structure. With this modification, we have successfully converted the regular
linked list into a circular linked list.
What are the different parts of a node in a doubly linked list? Draw a schematic
diagram of a doubly linked list with 5 nodes.
A doubly linked list is a type of linked list where each node contains three separate
components:
Data: This part holds the actual data value associated with the node.
Previous Pointer (prev): It points to the previous node in the list. prev data next
Next Pointer (next): It points to the next node in the list.
Node
Schematic representation of a doubly linked list with five nodes:
head null
1 2 3 4 5
null tail
What do you mean by a circular doubly linked list? Draw a diagram of a circular
doubly linked list with five nodes.
A circular doubly linked list combines the features of doubly linked list and circular linked
list. Each node has two pointers: one to the next and one to the previous node, forming
a closed loop.
head 1 2 3 4 5
Mention one advantage of doubly linked list over singly linked list.
One advantage of a doubly linked list over a singly linked list is its ability to support
bidirectional traversal. In a doubly linked list, each node has both a next and a prev
pointer. This allows for efficient navigation in both forward and backward directions.
Unlike singly linked lists, which only allow forward traversal, doubly linked lists provide
greater flexibility for certain operations.
Mention one disadvantage of doubly linked list over singly linked list.
One disadvantage of a doubly linked list compared to a singly linked list is that it
requires more memory due to the additional storage needed for the previous
pointers (prev). In a singly linked list, each node only contains a next pointer, which
makes it more memory-efficient. However, in a doubly linked list, each node has both a
next and a prev pointer, resulting in increased memory overhead.
Describe the advantages and disadvantages of doubly linked list over single
linked list.
Advantages of Doubly Linked List:
Bidirectional Traversal: In a doubly linked list, each node has both a next and a
prev pointer. This allows for forward and backward traversal, making it easier to
navigate in both directions.
Insertions and Deletions at Both Ends: Doubly linked lists allow efficient
insertions and deletions at both the beginning and the end of the list. For example:
Tail Pointer: A doubly linked list maintains a tail pointer, which points to the last
node. This makes appending elements to the end of the list more efficient.
Disadvantages of Doubly Linked List:
Increased Memory Overhead: Each node in a doubly linked list requires additional
memory for the prev pointer. In contrast, a singly linked list only needs a next
pointer. Therefore, doubly linked lists consume more memory.
Complexity: Managing two pointers (next and prev) for each node increases the
complexity of operations. Insertions and deletions require updating both pointers,
which can lead to more error-prone code.
Slower Traversal: Although doubly linked lists allow bidirectional traversal,
accessing nodes in the middle of the list is slower than in a singly linked list. This is
because we need to traverse through both next and prev pointers.
What is the condition for a doubly linked linear list, specified by head and tail
as the pointers respectively to the first and last nodes, to be empty?
A doubly linked linear list is considered empty when both the head and tail pointers are
set to null or when the head pointer is null and the tail pointer is not pointing to any
valid node.
Advantages of linked list:
1. Linked list can grow or shrink in size during the execution of a program
2. A linked list can be made just as long as required
3. Linked list does not waste memory space; it uses the memory which is needed for
the list at any point of time.
4. Linked list can provide flexibility in allowing the items to be rearranged efficiently.
5. It is easier to insert or delete items at any place, only by rearranging the links.
1. It can grow or shrink in size during the 1. It cannot grow or shrink in size once it
execution of a program. is declared.
2. It does not waste memory space. 2. It wastes memory space if the length
is not known at time of declaration.
5. For fixed length list, it takes more 5. For fixed length list, it takes less
memory than an array. memory than a linked list.
“Linked list is not always better than array” – do you agree with the
statement? Justify your answer.
I agree with the statement "Linked list is not always better than array." Both arrays and
linked lists are fundamental data structures, but they have different strengths and
weaknesses that make them suitable for different use cases.
The best data structure choice depends on our specific needs.
Use arrays when:
Random access is frequent.
Data size is known beforehand and unlikely to change significantly.
Memory efficiency for primitive data types is a priority.
Simplicity and ease of implementation are crucial.
Use linked lists when:
The data size is dynamic or can vary significantly.
Frequent insertions/deletions are required, especially in the middle of the list.
Memory efficiency for complex data structures is important.
What is self-referential structure?
A structure which contains a member field that points to the same structure type is
called self-referential structure. Structure of a node in a linked list is an example of self-
referential structure.
How can you insert nodes in a linked list? Explain with example.
Inserting a node in a linked list
There can be four cases while inserting a node in a linked list.
Insertion in an empty list.
Insertion at the beginning.
Insertion at the end.
Insertion in between the list nodes.
To insert a node, initially we will dynamically allocate space for that node using malloc().
Suppose newnode is a pointer that points to this dynamically allocated node. If memory
is not available the newnode will have NULL, otherwise in the data part of the node we
will put the value to insert.
newnode = (node*)malloc(sizeof(node));
if (newnode == NULL) {
printf("Unable to allocate memory for the new node.\n");
return;
}
newnode->data=x;
The „next‟ part of the newnode contains garbage value. We will assign address to it
separately in the different cases.
How can you delete nodes from a list? Explain with example.
Deleting a node from a linked list
There can be three cases while deleting a node from a linked list.
Deletion at the beginning.
Deletion at the end.
Deletion in between the list nodes.
Deletion by value:
(i) If head is NULL, the list is “Empty”, otherwise follow the remaining steps.
(ii) If the value present at head node, then delete the first node from the linked list.
if ((*addr_head)->data==key)
{
delnode = *addr_head;
*addr_head = (*addr_head)->next;
free(delnode);
}
(iii) Otherwise, find the address of the node after which we want to delete. Suppose
the node is „temp‟.
temp=*addr_head;
while(temp->next!=NULL && temp->next->data != key) /* key is item
which we want to delete */
temp = temp->next;
(iv) If „temp->next‟ is NULL, the key is “Not present”. Otherwise, delete the node after
„temp‟.
if (temp->next==NULL)
printf(“Item not present”);
else {
delnode=temp->next;
temp->next = temp->next->next;
free(delnode);
}
Write a function to delete the first node from a singly linked list.
void delete_from_beginning(node **addr_head)
{
node *delnode;
if (*addr_head==NULL)
printf("List is empty\n");
else
{
delnode = *addr_head;
*addr_head = (*addr_head)->next;
free(delnode);
}
}
Write a function to delete the last node from a singly linked list.
void delete_from_end(node **addr_head)
{
node *delnode,*temp;
if (*addr_head==NULL)
printf("List is empty\n");
else if ((*addr_head)->next==NULL)
{
delnode = *addr_head;
*addr_head = (*addr_head)->next;
free(delnode);
}
else
{
temp=*addr_head;
while(temp->next->next != NULL)
temp = temp->next;
delnode = temp->next;
temp->next = NULL;
free(delnode);
}
}
head->next=newhead;
newhead=head;
head=temp;
}
return newhead;
}
void display(node *head)
{
node *temp;
printf("Content of linked list:\t");
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("%d\t",temp->data);
}
printf("\n");
}
void main()
{
node *head;
head=create();
display(head);
head=reverse(head);
display(head);
}
void main() {
node *head=NULL,*tail=NULL;
int x;
create(&head,&tail);
display(head);
display_reverse(tail);
printf("Enter data to be deleted: ");
scanf("%d",&x);
delete_by_value(&head,&tail,x);
display(head);
display_reverse(tail);
}
Write the code for insertion in double linked list at sorted position.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct mode *prev;
int data;
struct node *next;
}node;
void insert_proper(node **addr_head, node **addr_tail, int x)
{
node *newnode,*temp;
newnode = (node*)malloc(sizeof(node));
newnode->data = x;
if (*addr_head==NULL)
{
newnode->next = newnode->prev = NULL;
*addr_head = *addr_tail = newnode;
}
else if ((*addr_head)->data > x)
{
newnode->next = *addr_head;
newnode->prev=NULL;
(*addr_head)->prev=newnode;
*addr_head =newnode;
}
else if ((*addr_tail)->data < x)
{
newnode->prev = *addr_tail;
newnode->next = NULL;
(*addr_tail)->next = newnode;
*addr_tail = newnode;
}
else
{
temp=*addr_head;
while(temp->next!=*addr_tail && temp->next->data < x)
temp = temp->next;
newnode->next = temp->next;
newnode->prev = temp;
temp->next->prev = newnode;
temp->next = newnode;
}
}
void create_sorted(node **addr_head,node **addr_tail)
{
int x;
do
{
printf("Enter any number (-999 to stop): ");
scanf("%d",&x);
if (x==-999)
break;
insert_proper(addr_head,addr_tail,x);
}while(x!=-999);
}