Skip to content

Commit a3034d7

Browse files
authored
Add files via upload
added simple LinkedList.cpp
1 parent e53da69 commit a3034d7

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

data-structures/linkedList.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Node {
6+
public:
7+
int data;
8+
Node* next;
9+
10+
Node(int data) {
11+
this->data = data;
12+
this->next = NULL;
13+
}
14+
};
15+
16+
class LinkedList {
17+
public:
18+
Node* head;
19+
20+
LinkedList() {
21+
this->head = NULL;
22+
}
23+
24+
void addNode(int data) {
25+
Node* newNode = new Node(data);
26+
if (this->head == NULL) {
27+
this->head = newNode;
28+
return;
29+
}
30+
Node* currentNode = this->head;
31+
while (currentNode->next != NULL) {
32+
currentNode = currentNode->next;
33+
}
34+
currentNode->next = newNode;
35+
}
36+
37+
void printList() {
38+
Node* currentNode = this->head;
39+
while (currentNode != NULL) {
40+
cout << currentNode->data << " ";
41+
currentNode = currentNode->next;
42+
}
43+
cout << endl;
44+
}
45+
};
46+
47+
int main() {
48+
LinkedList list;
49+
list.addNode(1);
50+
list.addNode(2);
51+
list.addNode(3);
52+
list.addNode(4);
53+
list.addNode(5);
54+
list.printList();
55+
return 0;
56+
}

0 commit comments

Comments
 (0)