Skip to content

GH-230: Linked List in C++ #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions concepts/cpp/linked-list/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Linked List in C++

From wikipedia

> 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.

## Linked List implementation in C++

## Basic Operations


```c++
#include <iostream>

using namespace std;
```

**Initialization**: Create a linked list with given values


```c++
struct Node {
int val;
Node* next;
Node(): val(0), next(NULL) {}
Node(int val): val(val), next(NULL){}
Node(int val, Node* next): val(val), next(next){}
};

Node* node1 = new Node(1);
Node* node2 = new Node(2, node1);
```

**Insertion**: Insert a new element at the beginning, end or in the middle of the list

**Deletion**: Delete an element from the beginning, end or in the middle of the list



```c++
struct Node {
int val;
Node* next;

Node(int val) : val(val), next(nullptr) {}
};

class LinkedList {
public:
Node* head;

LinkedList() : head(nullptr) {}

void insert(int val) {
Node* newNode = new Node(val);
if (head == nullptr) {
head = newNode;
} else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
}

void deleteNode(int val) {
Node* current = head;
Node* prev = nullptr;

while (current != nullptr && current->val != val) {
prev = current;
current = current->next;
}

if (current == nullptr) {
std::cout << "Value not found in list" << std::endl;
} else if (prev == nullptr) {
head = current->next;
delete current;
} else {
prev->next = current->next;
delete current;
}
}

void printList() {
Node* current = head;
while (current != nullptr) {
std::cout << current->val << " ";
current = current->next;
}
std::cout << std::endl;
}
};
```


```c++
LinkedList list;

// Insert some values
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);

std::cout << "List before deletion: ";
list.printList();

// Delete a node
list.deleteNode(3);

std::cout << "List after deletion: ";
list.printList();
```

List before deletion: 1 2 3 4 5
List after deletion: 1 2 4 5


## 🔗 Further Reading

* [Linked List Data Structure](https://www.geeksforgeeks.org/data-structures/linked-list/), geeksforgeeks
* ▶️ [Linked Lists Introduction](https://www.youtube.com/watch?v=-Yn5DU0_-lw&t=7s&ab_channel=WilliamFiset), WilliamFiset, 2017
* ▶️ [CS50 2018 - Lecture 4 - Linked Lists](https://www.youtube.com/watch?v=wh4TS7RJDTA), CS50, 2018
215 changes: 215 additions & 0 deletions concepts/cpp/linked-list/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Linked List in C++\n",
"\n",
"From wikipedia\n",
"\n",
"> 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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Linked List implementation in C++"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Operations"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
"using namespace std;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Initialization**: Create a linked list with given values"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [],
"source": [
"struct Node {\n",
" int val;\n",
" Node* next;\n",
" Node(): val(0), next(NULL) {}\n",
" Node(int val): val(val), next(NULL){}\n",
" Node(int val, Node* next): val(val), next(next){}\n",
"};\n",
"\n",
"Node* node1 = new Node(1);\n",
"Node* node2 = new Node(2, node1);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Insertion**: Insert a new element at the beginning, end or in the middle of the list\n",
"\n",
"**Deletion**: Delete an element from the beginning, end or in the middle of the list\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [],
"source": [
"struct Node {\n",
" int val;\n",
" Node* next;\n",
" \n",
" Node(int val) : val(val), next(nullptr) {}\n",
"};\n",
"\n",
"class LinkedList {\n",
"public:\n",
" Node* head;\n",
" \n",
" LinkedList() : head(nullptr) {}\n",
" \n",
" void insert(int val) {\n",
" Node* newNode = new Node(val);\n",
" if (head == nullptr) {\n",
" head = newNode;\n",
" } else {\n",
" Node* current = head;\n",
" while (current->next != nullptr) {\n",
" current = current->next;\n",
" }\n",
" current->next = newNode;\n",
" }\n",
" }\n",
" \n",
" void deleteNode(int val) {\n",
" Node* current = head;\n",
" Node* prev = nullptr;\n",
" \n",
" while (current != nullptr && current->val != val) {\n",
" prev = current;\n",
" current = current->next;\n",
" }\n",
" \n",
" if (current == nullptr) {\n",
" std::cout << \"Value not found in list\" << std::endl;\n",
" } else if (prev == nullptr) {\n",
" head = current->next;\n",
" delete current;\n",
" } else {\n",
" prev->next = current->next;\n",
" delete current;\n",
" }\n",
" }\n",
" \n",
" void printList() {\n",
" Node* current = head;\n",
" while (current != nullptr) {\n",
" std::cout << current->val << \" \";\n",
" current = current->next;\n",
" }\n",
" std::cout << std::endl;\n",
" }\n",
"};"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"List before deletion: 1 2 3 4 5 \n",
"List after deletion: 1 2 4 5 \n"
]
}
],
"source": [
"LinkedList list;\n",
"\n",
"// Insert some values\n",
"list.insert(1);\n",
"list.insert(2);\n",
"list.insert(3);\n",
"list.insert(4);\n",
"list.insert(5);\n",
"\n",
"std::cout << \"List before deletion: \";\n",
"list.printList();\n",
"\n",
"// Delete a node\n",
"list.deleteNode(3);\n",
"\n",
"std::cout << \"List after deletion: \";\n",
"list.printList();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🔗 Further Reading\n",
"\n",
"* [Linked List Data Structure](https://www.geeksforgeeks.org/data-structures/linked-list/), geeksforgeeks\n",
"* ▶️ [Linked Lists Introduction](https://www.youtube.com/watch?v=-Yn5DU0_-lw&t=7s&ab_channel=WilliamFiset), WilliamFiset, 2017\n",
"* ▶️ [CS50 2018 - Lecture 4 - Linked Lists](https://www.youtube.com/watch?v=wh4TS7RJDTA), CS50, 2018"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
8 changes: 7 additions & 1 deletion concepts/general/linked-list/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Linked List

*See implementation in*
C++,
[C++](/concepts/cpp/linked-list/README.md),
Java,
[Python](/concepts/python/linked_list.md),
[Typescript](/concepts/typescript/linked-list.md)
Expand Down Expand Up @@ -35,6 +35,12 @@ SEARCHING
|---------------------|---------------------|
| $O(n)$ | $O(n)$ |

## Basic Operations

* Initialization: Create a linked list with given values
* Insertion: Insert a new element at the beginning, end or in the middle of the list
* Deletion: Delete an element from the beginning, end or in the middle of the list

## 🔗 Further Reading

* [Linked Lists in Python](https://realpython.com/linked-lists-python/), realpython.com
Expand Down
3 changes: 2 additions & 1 deletion readme/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ A data structure is a data organization, management, and storage format that is
<a href="/concepts/cpp/string/README.md">String</a>
</li>
<li>
<code>B</code> Linked List
<code>B</code>
<a href="/concepts/cpp/linked-list/README.md">Linked List</a>
</li>
<li>
<code>B</code> Stack
Expand Down