Table of contents | |
Introduction | |
What is a Linked List? | |
Applications of Linked Lists | |
Advantages of Linked Lists | |
Disadvantages of Linked Lists | |
Sample Problems | |
Conclusion |
Linked lists are an essential data structure in computer science and are widely used in various applications. In this article, we will explore the applications, advantages, and disadvantages of linked lists in the context of data structures and algorithms using C++. We will also provide simple code examples with explanations to help beginners understand the concepts better.
A linked list is a linear data structure that consists of a sequence of nodes, where each node contains data and a reference (or link) to the next node in the sequence. The last node in the list points to NULL, indicating the end of the list.
Code Example 1: Creating a Linked List in C++
#include <iostream>
struct Node {
int data;
Node* next;
};
int main() {
// Creating nodes
Node* head = new Node();
Node* second = new Node();
Node* third = new Node();
// Assigning data and linking nodes
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
// Printing the linked list
Node* current = head;
while (current != NULL) {
std::cout << current->data << " ";
current = current->next;
}
return 0;
}
Output:
1 2 3
Explanation: In the above code, we create a simple linked list with three nodes. Each node contains an integer data value and a pointer to the next node. We then traverse the linked list and print the data values of each node.
Problem 1: Write a C++ program to count the number of nodes in a linked list.
#include <iostream>
struct Node {
int data;
Node* next;
};
int countNodes(Node* head) {
int count = 0;
Node* current = head;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
int main() {
Node* head = new Node();
Node* second = new Node();
Node* third = new Node();
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
int nodeCount = countNodes(head);
std::cout << "Number of nodes: " << nodeCount << std::endl;
return 0;
}
Output:
Number of nodes: 3
Problem 2: Write a C++ program to reverse a linked list.
#include <iostream>
struct Node {
int data;
Node* next;
};
Node* reverseList(Node* head) {
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
int main() {
Node* head = new Node();
Node* second = new Node();
Node* third = new Node();
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
std::cout << "Original list: ";
printList(head);
Node* reversed = reverseList(head);
std::cout << "Reversed list: ";
printList(reversed);
return 0;
}
Output:
Original list: 1 2 3
Reversed list: 3 2 1
Linked lists are versatile data structures that offer dynamic size, efficient insertion and deletion, and memory flexibility. While they have disadvantages such as slower random access and extra memory overhead, they are widely used in various applications like implementing dynamic data structures, managing memory efficiently, and representing graphs and trees. Understanding linked lists and their pros and cons is crucial for every programmer, as they form the building blocks of more complex data structures and algorithms.
153 videos|115 docs|24 tests
|
|
Explore Courses for Software Development exam
|