/
single list.cpp
197 lines (189 loc) · 3.08 KB
/
single list.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include<iostream>
using namespace std;
class Node{
friend class List;
public:
Node()
{
next = NULL;
}
Node(int input)
{
data = input;
next = NULL;
}
private:
int data;
Node *next;
};
class List{
public:
List()
{
head = NULL;
}
void insert(Node *data);
void insert(Node *data, int pos);
void del(int data);
void print();
private:
Node *head;
};
void List::insert(Node *data)
{
if (head == NULL)
{
head = data;
return;
}
Node *ptr;
for (ptr = head; ptr->next != NULL; ptr = ptr->next);
ptr->next = data;
}
void List::insert(Node *data, int pos)
{
if (head == NULL)
{
head = data;
return;
}
else if (pos == 1)
{
data->next = head;
head = data;
return;
}
Node *ptr;
int idx;
for (ptr = head, idx = 1; (idx != pos - 1) && (ptr->next != NULL); ptr = ptr->next, idx++);
if (idx + 1 < pos)
{
cout << "overflow..." << endl;
getchar();
getchar();
}
else if (ptr->next == NULL)
ptr->next = data;
else
{
Node *new_next = ptr->next;
ptr->next = data;
data->next = new_next;
}
}
void List::del(int data)
{
if (head == NULL)
{
cout << "List is empty..." << endl;
return;
}
Node *ptr, *prev = head;
for (ptr = head; ptr != NULL; ptr = ptr->next)
{
if (ptr->data == data)
{
if (ptr == head)
{
head = ptr->next;
delete ptr;
return;
}
else if (ptr->next == NULL)
{
prev->next = NULL;
delete ptr;
return;
}
else
{
prev->next = ptr->next;
delete ptr;
return;
}
}
prev = ptr;
}
cout << "No data..." << endl;
getchar(); getchar();
}
void List::print()
{
if (head == NULL)
{
cout << "List is empty..." << endl;
return;
}
Node *ptr;
for (ptr = head; ptr != NULL; ptr = ptr->next)
cout << ptr->data << ' ';
cout << endl;
}
int main()
{
//List *lst=new List();
List lst;
while (1)
{
cout << "-------------------------------------------------" << endl;
cout << "- 1. insert 2. delete, 3. print, 4. exit -" << endl;
cout << "-------------------------------------------------" << endl;
cout << endl << "select menu -> ";
int menu; cin >> menu;
if (menu == 1)
{
int data, pos, type;
cout << "1. input data 2.input data and position " << endl;
cout << "select type -> ";
cin >> type;
if (type == 1)
{
cout << "input data -> ";
cin >> data;
Node *new_node = new Node(data);
//lst->insert(new_node);
lst.insert(new_node);
system("cls");
}
else if (type == 2)
{
cout << "input data and pos -> ";
cin >> data >> pos;
Node *new_node = new Node(data);
//lst->insert(new_node, pos);
lst.insert(new_node, pos);
system("cls");
}
else continue;
}
else if (menu == 2)
{
int data;
cout << "input data for delete -> ";
cin >> data;
lst.del(data);
system("cls");
}
else if (menu == 3)
{
//lst->print();
lst.print();
cout << "press enter key...";
getchar();
getchar();
system("cls");
}
else if (menu == 4)
{
cout << "Bye~ Bye~" << endl;
break;
}
else
{
cout << "What are you doing???" << endl;
getchar();
system("cls");
}
}
return 0;
}