-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLL_1.cpp
60 lines (48 loc) · 1.17 KB
/
LL_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
// Constructor for Node class
Node(int value) {
data = value;
next = nullptr;
}
};
class LinkedList {
private:
Node* head; // Pointer to the first node (head) of the list
public:
// Constructor for LinkedList class (initially empty)
LinkedList() {
head = nullptr;
}
// Set the head of the list
void setHead(Node* node) {
head = node;
}
// Display the linked list (for testing)
void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "null" << endl;
}
};
int main() {
// Create a linked list
LinkedList list;
// Create some nodes
Node* node1 = new Node(10); // Node with value 10
Node* node2 = new Node(20); // Node with value 20
// Link the nodes together
node1->next = node2;
// Set the head of the list to point to the first node
list.setHead(node1);
// Display the linked list
list.display(); // Output: 10 -> 20 -> null
return 0;
}