-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList2.cpp
More file actions
58 lines (46 loc) · 1.62 KB
/
LinkedList2.cpp
File metadata and controls
58 lines (46 loc) · 1.62 KB
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
#include <iostream>
using namespace std;
// Node class represents a node in the linked list
class Node {
public:
int data; // data stored in the node
Node* next; // pointer to the next node in the list
// Constructor to initialize a node with given data and a null next pointer
Node(int value) : data(value), next(nullptr) {}
};
// LinkedList class represents the linked list
class LinkedList {
private:
Node* head; // pointer to the first node in the list
public:
// Constructor to initialize an empty linked list
LinkedList() : head(nullptr) {}
// Function to insert a new node at the beginning of the list
void insertAtBeginning(int value) {
Node* newNode = new Node(value); // create a new node
newNode->next = head; // set the new node's next pointer to the current head
head = newNode; // update the head to the new node
}
// Function to print the elements of the linked list
void display() {
Node* current = head; // start from the head of the list
// Traverse the list and print each element
while (current != nullptr) {
cout << current->data << " ";
current = current->next; // move to the next node
}
cout << endl;
}
};
int main() {
// Create a linked list
LinkedList myList;
// Insert elements at the beginning of the list
myList.insertAtBeginning(3);
myList.insertAtBeginning(2);
myList.insertAtBeginning(1);
// Display the elements of the list
cout << "Linked List: ";
myList.display();
return 0;
}