-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl3_ii.cpp
57 lines (47 loc) · 1010 Bytes
/
l3_ii.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
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* head = nullptr; // Initialize the head of the linked list
void insert(int data) {
Node* temp = new Node();
temp->data = data;
temp->next = nullptr;
if (head == nullptr) {
head = temp;
} else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = temp;
}
}
void display() {
Node* t = head;
while (t != nullptr) {
cout << t->data << " -> ";
t = t->next;
}
cout << "null" << endl;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
insert(a);
}
display();
// Free memory by deleting nodes
Node* current = head;
while (current != nullptr) {
Node* temp = current;
current = current->next;
delete temp;
}
return 0;
}