-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno2.2.cpp
More file actions
89 lines (77 loc) · 1.89 KB
/
Copy pathno2.2.cpp
File metadata and controls
89 lines (77 loc) · 1.89 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;
struct Node {
int num;
Node* next;
};
class LinkedList {
private:
Node* head;
Node* tail;
public:
LinkedList() {
head = NULL;
tail = NULL;
}
void insert_Node(int n) {
struct Node* new_node = new Node;
new_node->num = n;
new_node->next = head;
head = new_node;
}
void delete_Node() {
if (head == NULL) {
cout << "\nLinked List is empty.";
return;
}
Node* temp = head;
head = head->next;
delete temp;
cout << "\nNode deleted.";
}
void display_all_nodes() {
if (head == NULL) {
cout << "\nLinked List is empty.";
return;
}
cout << "\nThe list contains the data entered:\n";
Node* temp = head;
while (temp != NULL) {
cout << temp->num << " ";
temp = temp->next;
}
}
};
int main() {
char choice;
int item;
LinkedList l;
do {
cout << "\nMENU";
cout << "\n1. Add Node";
cout << "\n2. Delete Node";
cout << "\n3. Display Nodes";
cout << "\n4. Exit";
cout << "\nEnter your choice (1-4): ";
cin >> choice;
switch (choice) {
case '1':
cout << "\nEnter item to add: ";
cin >> item;
l.insert_Node(item);
break;
case '2':
l.delete_Node();
break;
case '3':
l.display_all_nodes();
break;
case '4':
cout << "\nExiting...";
break;
default:
cout << "\nInvalid choice. Try again.";
}
} while (choice != '4');
return 0;
}