Notes 2

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 4

Linked Lists in Data Structures

1. Introduction to Linked Lists


A linked list is a linear data structure in which elements, known as nodes, are linked using pointers.
Unlike arrays, the elements in a linked list are not stored in contiguous memory locations.
Each node in a linked list contains:
1. Data: The actual data value.
2. Pointer (or Reference): Points to the next node in the sequence.
Linked lists provide dynamic memory allocation and are useful when the size of data is unknown or
varies frequently.

2. Types of Linked Lists


Linked lists come in various forms depending on their structure and features:
2.1 Singly Linked List:
• Each node has two parts: data and a pointer to the next node.
• Traversal is only in one direction.
• Example:
r
Copy code
[data|next] -> [data|next] -> NULL

2.2 Doubly Linked List:


• Each node contains three parts: data, a pointer to the next node, and a pointer to the previous
node.
• Enables traversal in both directions.
• Example:
r
Copy code
NULL <- [prev|data|next] <-> [prev|data|next] -> NULL

2.3 Circular Linked List:


• In this type, the last node points back to the first node, creating a circular structure.
• Can be singly or doubly linked.

3. Operations on Linked Lists


Several operations can be performed on linked lists:
3.1 Traversal:
• Visiting each node of the linked list sequentially.
• Example Code (C-like pseudocode):
c
Copy code
Node* temp = head;
while (temp != NULL) {
print(temp->data);
temp = temp->next;
}

3.2 Insertion:
• At the Beginning: Insert a new node before the head and update the head pointer.
• At the End: Traverse to the last node and update its next pointer to the new node.
• At a Specific Position: Traverse to the required position and adjust pointers accordingly.
3.3 Deletion:
• From the Beginning: Update the head pointer to the next node.
• From the End: Traverse to the second-last node and set its next pointer to NULL.
• From a Specific Position: Adjust pointers to bypass the node to be deleted.
3.4 Searching:
• Traverse the list to find a specific value or node.
3.5 Reversal:
• Reverse the direction of pointers in the list.

4. Advantages of Linked Lists


• Dynamic Size: Linked lists can grow or shrink dynamically without the need for
reallocation or reorganization of memory.
• Efficient Insertions/Deletions: Adding or removing elements is efficient because there is no
need to shift elements as in arrays.

5. Disadvantages of Linked Lists


• Increased Memory Usage: Additional memory is required for storing pointers.
• Sequential Access: Linked lists do not support random access; traversal is necessary to
access elements.
• Complexity: Implementation is more complex compared to arrays.

6. Applications of Linked Lists


Linked lists are widely used in various scenarios:
1. Dynamic Memory Allocation: Helps in managing memory efficiently.
2. Implementing Stacks and Queues: These data structures are often implemented using
linked lists.
3. Representation of Sparse Matrices: Compact storage of sparse matrices where most
elements are zero.
4. Graph Representation: Adjacency lists in graph algorithms are implemented using linked
lists.

7. Singly Linked List Implementation


Example Code (C):
c
Copy code
#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to print the linked list


void printList(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);

printList(head);
return 0;
}

8. Comparison with Arrays


Feature Arrays Linked Lists
Memory Allocation Contiguous Non-contiguous
Size Fixed Dynamic
Access Time O(1) (random access) O(n) (sequential)
Insertion/Deletion Expensive Efficient
9. Doubly Linked List Implementation
Example Code (C):
c
Copy code
#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}

// Function to print the linked list forward


void printListForward(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = createNode(1);
struct Node* second = createNode(2);
struct Node* third = createNode(3);

head->next = second;
second->prev = head;
second->next = third;
third->prev = second;

printListForward(head);
return 0;
}

10. Circular Linked List


In a circular linked list, the last node's next pointer points back to the first node. This structure is
useful in applications like round-robin scheduling.

You might also like