Linked List
Linked List
* Insertion
void insertStart (struct Node **head, int data)
{
newNode - >
data = data;
newNode - >
next = *head;
* Deletion
void deleteStart(struct Node **head)
{
struct Node *temp = *head;
free (temp);
}
* Traversal
void display(struct Node* node)
{
printf("Linked List: ");
printf("\n");
}
* Insertion
Insertion at Beginning
void insertStart (struct Node **head, int data)
{
Insertion at End:
void insertLast (struct Node **head, int data)
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;
newNode->next = NULL;
* Deletion
Deletion at beginning:
if (*head == del_node)
*head = del_node->next;
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
free(del);
Deletion at end:
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
* Traversal
void display (struct Node *node)
{
struct Node *end;
printf ("\nIn Forward Direction\n");
while (node != NULL)
{
printf (" %d ", node->data);
end = node;
node = node->next;
}
printf ("\nIn Backward direction \n");
while (end != NULL)
{
printf (" %d ", end->data);
end = end->prev;
}
}
Disadvantages:
Complexity: Circular lists are complex as compared to singly linked lists.
Infinite Loop: If not handled carefully, then the code may go in an
infinite loop.
No Direct Access: Elements are accessed sequentially as they are stored
randomly in memory.
Slower Traversal: Traversing a DLL can be slower than traversing a singly
linked list.
Harder to find the end of the list and loop control.
**Applications of linked list**
* What are the different applications of linked lists in computer science?
Stacks and Queues: Linked lists can be used to make other data
structures like stacks and queues.
Graphs: Linked lists can represent graphs.
Memory Management: Linked lists can help manage memory in
programming.
Directory of Names: Linked lists can keep a list of names.
Sparse Matrices: Linked lists can represent sparse matrices.
Music Player: Songs in the music player are linked to the previous and
next songs.
* What are the advantages and disadvantages of using linked lists over other
data structures, such as arrays?
Advantages:
Dynamic Size: Linked lists can grow or shrink during runtime.
Efficient Operations: Insertions and deletions are more efficient if the
pointer to the node is given.
No Memory Reallocation: Unlike arrays, you don’t need to reallocate
memory when adding or removing elements.
Disadvantages:
More Memory: Each node needs extra space for a pointer to the next
node.
No Random Access: Elements are accessed sequentially, not randomly
like in arrays.
Slow Traversal: It can be slower to go through all elements compared to
arrays.