-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq08.cpp
114 lines (104 loc) · 1.88 KB
/
q08.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
/*
/* Design, develop, and execute a program in C++ to create a class
/* called LIST (linked list) with member functions to insert an element
/* at the front of the list as well as to delete an element from the front
/* of the list. Demonstrate all the functions after creating a list object.
/*
*/
#include <iostream>
using namespace std;
class Node {
public:
int val;
Node *next;
Node() {
next = NULL;
}
};
class List {
Node *start;
public:
List() {
start = NULL;
}
void insert(int item) {
Node *n = new Node();
n->val = item;
if(start == NULL)
start = n;
else {
n->next = start;
start = n;
}
}
void insertBefore(int item, int before) {
Node *n = new Node(), *s = start, *prev = NULL;
n->val = item;
while(s != NULL) {
if(s->val == before) {
if(s == start)
start = n;
n->next = s;
if(prev != NULL)
prev->next = n;
break;
}
prev = s;
s = s->next;
}
}
int remove() {
if(start == NULL)
return -999;
int out = start->val;
Node *temp = start;
start = temp->next;
delete temp;
return out;
}
void display() {
Node *s = start;
while(s != NULL) {
cout << " " << s->val;
s = s->next;
}
cout << endl;
}
};
int main() {
List l;
cout << " \n Enter \n";
cout << " i to Insert\n";
cout << " b to Insert Before\n";
cout << " d to Delete\n";
cout << " s to Show\n";
do {
int item, before;
char choice;
cout << ">> ";
cin >> choice;
switch(choice) {
case 'i':
cout << " Enter value to insert: ";
cin >> item;
l.insert(item);
break;
case 'b':
cout << " Enter value to insert: ";
cin >> item;
cout << " Enter to insert before: ";
cin >> before;
l.insertBefore(item, before);
break;
case 'd':
cout << " Deleted: " << l.remove() << endl;
break;
case 's':
l.display();
break;
default:
return 0;
}
} while(1);
}