001.
#include <iostream>
002.
003.
004.
typedef
struct
list {
005.
int
info;
006.
struct
list* link;
007.
} node;
008.
node *head, *
null
, *current, *temp, *pred;
009.
010.
int
ShowMenu();
011.
void
INSERT_FN();
012.
int
_FindMax();
013.
int
Affter(
int
y);
014.
015.
void
DisplaySingly();
016.
void
CreateSingly(
int
Number);
017.
void
InsertFronSingly(
int
x);
018.
void
insertRearSingly(
int
y);
019.
void
insertMiddleSingly(
int
_info,
int
_Affter);
020.
void
DeleteSingly(
int
D);
021.
022.
void
average();
023.
void
_Sum();
024.
025.
void
max();
026.
void
min();
027.
028.
int
main()
029.
{
030.
int
Menu;
031.
032.
do
{
033.
int
A;
034.
int
D;
035.
036.
switch
(Menu = ShowMenu())
037.
{
038.
case
1: INSERT_FN();
break
;
039.
040.
case
2: std::cout <<
"Delete:"
; std::cin >> A; DeleteSingly(A);
break
;
041.
042.
case
3: DisplaySingly();
break
;
043.
044.
case
4: _Sum();
break
;
045.
046.
case
5: average();
break
;
047.
048.
case
6: max();
break
;
049.
050.
case
7: min();
break
;
051.
}
052.
}
while
(Menu != 8);
053.
054.
std::cout <<
"\nExit\n"
;
055.
056.
return
0;
057.
}
058.
059.
int
ShowMenu()
060.
{
061.
int
_Menu;
062.
063.
std::cout <<
"\nPlease select menu\n"
;
064.
std::cout <<
" Insert --> 1\n"
;
065.
std::cout <<
" Delete --> 2\n"
;
066.
std::cout <<
" Display --> 3\n"
;
067.
std::cout <<
" Sum --> 4\n"
;
068.
std::cout <<
" Average --> 5\n"
;
069.
std::cout <<
" Maximum --> 6\n"
;
070.
std::cout <<
" Minimum --> 7\n"
;
071.
std::cout <<
" Exit --> 8\n"
;
072.
073.
std::cout <<
"Select : "
;
074.
std::cin >> _Menu;
075.
076.
return
_Menu;
077.
}
078.
079.
void
DisplaySingly(
void
)
080.
{
081.
if
(head == NULL)
082.
std::cout <<
"Empty List\n"
;
083.
084.
else
{
085.
current = head;
086.
while
(current != NULL) {
087.
std::cout << current->info << std::endl;
088.
current = current->link;
089.
}
090.
}
091.
}
092.
093.
094.
095.
void
INSERT_FN()
096.
{
097.
int
Y;
098.
099.
std::cout <<
"Insert : "
;
100.
std::cin >> Y;
101.
102.
if
(head ==
null
) {
103.
CreateSingly(Y);
104.
}
else
if
(Y <= head->info) {
105.
InsertFronSingly(Y);
106.
}
else
if
(Y >= _FindMax()) {
107.
insertRearSingly(Y);
108.
}
else
{
109.
insertMiddleSingly(Y, Affter(Y));
110.
}
111.
}
112.
113.
void
CreateSingly(
int
Number)
114.
{
115.
if
(head == NULL) {
116.
head =
new
node;
117.
head->info = Number;
118.
head->link = NULL;
119.
}
120.
}
121.
122.
void
InsertFronSingly(
int
x)
123.
{
124.
if
(head == NULL)
125.
std::cout <<
"Empty List"
;
126.
else
{
127.
temp =
new
list;
128.
temp->info = x;
129.
temp->link = head;
130.
head = temp;
131.
}
132.
}
133.
134.
int
_FindMax(
void
)
135.
{
136.
current = head;
137.
while
(current->link !=
null
) {
138.
current = current->link;
139.
}
140.
141.
return
current->info;
142.
}
143.
144.
void
insertRearSingly(
int
y)
145.
{
146.
if
(head == NULL)
147.
std::cout <<
"EMPTY LIST"
;
148.
else
{
149.
temp =
new
node;
150.
temp->info = y;
151.
current = head;
152.
while
(current->link != NULL)
153.
current = current->link;
154.
temp->link = NULL;
155.
current->link = temp;
156.
}
157.
}
158.
159.
void
insertMiddleSingly(
int
_info,
int
_Affter)
160.
{
161.
if
(head == NULL)
162.
std::cout <<
"Empty List"
;
163.
else
{
164.
temp =
new
node;
165.
temp->info = _info;
166.
current = head;
167.
while
(current->info != _Affter && current->link != NULL)
168.
current = current->link;
169.
temp->link = current->link;
170.
current->link = temp;
171.
}
172.
}
173.
174.
int
Affter(
int
y)
175.
{
176.
current = head;
177.
178.
while
((y < current->info) or (y > current->link->info)) {
179.
current = current->link;
180.
}
181.
182.
return
current->info;
183.
}
184.
185.
void
DeleteSingly(
int
D)
186.
{
187.
int
DELETE;
188.
189.
if
(head->info == D) {
190.
current = head;
191.
head = head->link;
192.
DELETE;
193.
current;
194.
}
else
{
195.
pred = head;
196.
current = head->link;
197.
while
(current->link != NULL && current->info != D) {
198.
pred = current;
199.
current = current->link;
200.
}
201.
if
(current->info = D) {
202.
pred->link = current->link;
203.
DELETE;
204.
current;
205.
}
else
206.
std::cout <<
"NOT FOUND"
;
207.
}
208.
}
209.
210.
void
_Sum()
211.
{
212.
int
sum = 0;
213.
current = head;
214.
while
(current != NULL) {
215.
sum = sum + current->info;
216.
current = current->link;
217.
}
218.
std::cout <<
"Sum="
<< sum << std::endl;
219.
}
220.
221.
void
average()
222.
{
223.
float
sum = 0;
224.
int
counter = 0;
225.
current = head;
226.
while
(current != NULL) {
227.
sum = sum + current->info;
228.
counter++;
229.
current = current->link;
230.
}
231.
std::cout <<
"average="
<< sum / counter << std::endl;
232.
}
233.
234.
void
max()
235.
{
236.
237.
int
max = 0;
238.
current = head;
239.
for
(current = head->link, max = head->info; current; current = current->link)
240.
if
(current->info > max)
241.
max = current->info;
242.
243.
std::cout <<
"Maximum="
<< max << std::endl;
244.
}
245.
246.
void
min()
247.
{
248.
int
min;
249.
for
(current = head->link, min = head->info; current; current = current->link)
250.
if
(current->info < min)
251.
min = current->info;
252.
253.
std::cout <<
"Minimum="
<< min << std::endl;
254.
}