File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments