Skip to content

Commit 301639f

Browse files
authored
GH-230: Linked List in C++ (#231)
1 parent fad4c54 commit 301639f

File tree

4 files changed

+351
-2
lines changed

4 files changed

+351
-2
lines changed

concepts/cpp/linked-list/README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Linked List in C++
2+
3+
From wikipedia
4+
5+
> In computer science, a `linked list` is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.
6+
7+
## Linked List implementation in C++
8+
9+
## Basic Operations
10+
11+
12+
```c++
13+
#include <iostream>
14+
15+
using namespace std;
16+
```
17+
18+
**Initialization**: Create a linked list with given values
19+
20+
21+
```c++
22+
struct Node {
23+
int val;
24+
Node* next;
25+
Node(): val(0), next(NULL) {}
26+
Node(int val): val(val), next(NULL){}
27+
Node(int val, Node* next): val(val), next(next){}
28+
};
29+
30+
Node* node1 = new Node(1);
31+
Node* node2 = new Node(2, node1);
32+
```
33+
34+
**Insertion**: Insert a new element at the beginning, end or in the middle of the list
35+
36+
**Deletion**: Delete an element from the beginning, end or in the middle of the list
37+
38+
39+
40+
```c++
41+
struct Node {
42+
int val;
43+
Node* next;
44+
45+
Node(int val) : val(val), next(nullptr) {}
46+
};
47+
48+
class LinkedList {
49+
public:
50+
Node* head;
51+
52+
LinkedList() : head(nullptr) {}
53+
54+
void insert(int val) {
55+
Node* newNode = new Node(val);
56+
if (head == nullptr) {
57+
head = newNode;
58+
} else {
59+
Node* current = head;
60+
while (current->next != nullptr) {
61+
current = current->next;
62+
}
63+
current->next = newNode;
64+
}
65+
}
66+
67+
void deleteNode(int val) {
68+
Node* current = head;
69+
Node* prev = nullptr;
70+
71+
while (current != nullptr && current->val != val) {
72+
prev = current;
73+
current = current->next;
74+
}
75+
76+
if (current == nullptr) {
77+
std::cout << "Value not found in list" << std::endl;
78+
} else if (prev == nullptr) {
79+
head = current->next;
80+
delete current;
81+
} else {
82+
prev->next = current->next;
83+
delete current;
84+
}
85+
}
86+
87+
void printList() {
88+
Node* current = head;
89+
while (current != nullptr) {
90+
std::cout << current->val << " ";
91+
current = current->next;
92+
}
93+
std::cout << std::endl;
94+
}
95+
};
96+
```
97+
98+
99+
```c++
100+
LinkedList list;
101+
102+
// Insert some values
103+
list.insert(1);
104+
list.insert(2);
105+
list.insert(3);
106+
list.insert(4);
107+
list.insert(5);
108+
109+
std::cout << "List before deletion: ";
110+
list.printList();
111+
112+
// Delete a node
113+
list.deleteNode(3);
114+
115+
std::cout << "List after deletion: ";
116+
list.printList();
117+
```
118+
119+
List before deletion: 1 2 3 4 5
120+
List after deletion: 1 2 4 5
121+
122+
123+
## 🔗 Further Reading
124+
125+
* [Linked List Data Structure](https://www.geeksforgeeks.org/data-structures/linked-list/), geeksforgeeks
126+
* ▶️ [Linked Lists Introduction](https://www.youtube.com/watch?v=-Yn5DU0_-lw&t=7s&ab_channel=WilliamFiset), WilliamFiset, 2017
127+
* ▶️ [CS50 2018 - Lecture 4 - Linked Lists](https://www.youtube.com/watch?v=wh4TS7RJDTA), CS50, 2018
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Linked List in C++\n",
8+
"\n",
9+
"From wikipedia\n",
10+
"\n",
11+
"> In computer science, a `linked list` is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence."
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"## Linked List implementation in C++"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"## Basic Operations"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 2,
31+
"metadata": {
32+
"vscode": {
33+
"languageId": "cpp"
34+
}
35+
},
36+
"outputs": [],
37+
"source": [
38+
"#include <iostream>\n",
39+
"\n",
40+
"using namespace std;"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"**Initialization**: Create a linked list with given values"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": 7,
53+
"metadata": {
54+
"vscode": {
55+
"languageId": "cpp"
56+
}
57+
},
58+
"outputs": [],
59+
"source": [
60+
"struct Node {\n",
61+
" int val;\n",
62+
" Node* next;\n",
63+
" Node(): val(0), next(NULL) {}\n",
64+
" Node(int val): val(val), next(NULL){}\n",
65+
" Node(int val, Node* next): val(val), next(next){}\n",
66+
"};\n",
67+
"\n",
68+
"Node* node1 = new Node(1);\n",
69+
"Node* node2 = new Node(2, node1);"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"metadata": {},
75+
"source": [
76+
"**Insertion**: Insert a new element at the beginning, end or in the middle of the list\n",
77+
"\n",
78+
"**Deletion**: Delete an element from the beginning, end or in the middle of the list\n"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 10,
84+
"metadata": {
85+
"vscode": {
86+
"languageId": "cpp"
87+
}
88+
},
89+
"outputs": [],
90+
"source": [
91+
"struct Node {\n",
92+
" int val;\n",
93+
" Node* next;\n",
94+
" \n",
95+
" Node(int val) : val(val), next(nullptr) {}\n",
96+
"};\n",
97+
"\n",
98+
"class LinkedList {\n",
99+
"public:\n",
100+
" Node* head;\n",
101+
" \n",
102+
" LinkedList() : head(nullptr) {}\n",
103+
" \n",
104+
" void insert(int val) {\n",
105+
" Node* newNode = new Node(val);\n",
106+
" if (head == nullptr) {\n",
107+
" head = newNode;\n",
108+
" } else {\n",
109+
" Node* current = head;\n",
110+
" while (current->next != nullptr) {\n",
111+
" current = current->next;\n",
112+
" }\n",
113+
" current->next = newNode;\n",
114+
" }\n",
115+
" }\n",
116+
" \n",
117+
" void deleteNode(int val) {\n",
118+
" Node* current = head;\n",
119+
" Node* prev = nullptr;\n",
120+
" \n",
121+
" while (current != nullptr && current->val != val) {\n",
122+
" prev = current;\n",
123+
" current = current->next;\n",
124+
" }\n",
125+
" \n",
126+
" if (current == nullptr) {\n",
127+
" std::cout << \"Value not found in list\" << std::endl;\n",
128+
" } else if (prev == nullptr) {\n",
129+
" head = current->next;\n",
130+
" delete current;\n",
131+
" } else {\n",
132+
" prev->next = current->next;\n",
133+
" delete current;\n",
134+
" }\n",
135+
" }\n",
136+
" \n",
137+
" void printList() {\n",
138+
" Node* current = head;\n",
139+
" while (current != nullptr) {\n",
140+
" std::cout << current->val << \" \";\n",
141+
" current = current->next;\n",
142+
" }\n",
143+
" std::cout << std::endl;\n",
144+
" }\n",
145+
"};"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": 11,
151+
"metadata": {
152+
"vscode": {
153+
"languageId": "cpp"
154+
}
155+
},
156+
"outputs": [
157+
{
158+
"name": "stdout",
159+
"output_type": "stream",
160+
"text": [
161+
"List before deletion: 1 2 3 4 5 \n",
162+
"List after deletion: 1 2 4 5 \n"
163+
]
164+
}
165+
],
166+
"source": [
167+
"LinkedList list;\n",
168+
"\n",
169+
"// Insert some values\n",
170+
"list.insert(1);\n",
171+
"list.insert(2);\n",
172+
"list.insert(3);\n",
173+
"list.insert(4);\n",
174+
"list.insert(5);\n",
175+
"\n",
176+
"std::cout << \"List before deletion: \";\n",
177+
"list.printList();\n",
178+
"\n",
179+
"// Delete a node\n",
180+
"list.deleteNode(3);\n",
181+
"\n",
182+
"std::cout << \"List after deletion: \";\n",
183+
"list.printList();"
184+
]
185+
},
186+
{
187+
"cell_type": "markdown",
188+
"metadata": {},
189+
"source": [
190+
"## 🔗 Further Reading\n",
191+
"\n",
192+
"* [Linked List Data Structure](https://www.geeksforgeeks.org/data-structures/linked-list/), geeksforgeeks\n",
193+
"* ▶️ [Linked Lists Introduction](https://www.youtube.com/watch?v=-Yn5DU0_-lw&t=7s&ab_channel=WilliamFiset), WilliamFiset, 2017\n",
194+
"* ▶️ [CS50 2018 - Lecture 4 - Linked Lists](https://www.youtube.com/watch?v=wh4TS7RJDTA), CS50, 2018"
195+
]
196+
}
197+
],
198+
"metadata": {
199+
"kernelspec": {
200+
"display_name": "C++17",
201+
"language": "C++17",
202+
"name": "xcpp17"
203+
},
204+
"language_info": {
205+
"codemirror_mode": "text/x-c++src",
206+
"file_extension": ".cpp",
207+
"mimetype": "text/x-c++src",
208+
"name": "c++",
209+
"version": "17"
210+
},
211+
"orig_nbformat": 4
212+
},
213+
"nbformat": 4,
214+
"nbformat_minor": 2
215+
}

concepts/general/linked-list/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Linked List
22

33
*See implementation in*
4-
C++,
4+
[C++](/concepts/cpp/linked-list/README.md),
55
Java,
66
[Python](/concepts/python/linked_list.md),
77
[Typescript](/concepts/typescript/linked-list.md)
@@ -35,6 +35,12 @@ SEARCHING
3535
|---------------------|---------------------|
3636
| $O(n)$ | $O(n)$ |
3737

38+
## Basic Operations
39+
40+
* Initialization: Create a linked list with given values
41+
* Insertion: Insert a new element at the beginning, end or in the middle of the list
42+
* Deletion: Delete an element from the beginning, end or in the middle of the list
43+
3844
## 🔗 Further Reading
3945

4046
* [Linked Lists in Python](https://realpython.com/linked-lists-python/), realpython.com

readme/cpp/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ A data structure is a data organization, management, and storage format that is
2626
<a href="/concepts/cpp/string/README.md">String</a>
2727
</li>
2828
<li>
29-
<code>B</code> Linked List
29+
<code>B</code>
30+
<a href="/concepts/cpp/linked-list/README.md">Linked List</a>
3031
</li>
3132
<li>
3233
<code>B</code> Stack

0 commit comments

Comments
 (0)