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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/* Đề Bài : Xử lý tất cả các yêu cầu sau đối với tất cả các Node,đối với các Node dương,đối với các Node âm :
Yêu Cầu :
1 - Nhập các Node .
2 - Xuất ra các Node .
3 - Đếm số lượng các Node .
4 - Tính tổng các Node .
5 - Tính tích các Node .
6 - Tìm Node lớn nhất .
7 - Tìm Node nhỏ nhất .
8 - Thêm vào cuối danh sách một Node và xuất ra danh sách sau khi đã thêm .
9 - Sắp xếp danh sách liên kết tăng dần .
10 - Sắp xếp danh sách liên kết giảm dần .635.cpp */
/*
#include<stdio.h>
#include<conio.h>
#include<limits.h>
#include<Windows.h>
// Khai báo struct Node .
struct node
{
int Data;
struct node*pNext;
};
typedef struct node NODE;
// Khai báo struct List .
struct list
{
NODE*pHead;
NODE*pTail;
};
typedef struct list LIST;
// Khởi tạo List .
void INit(LIST &l)
{
l.pHead=NULL;
l.pTail=NULL;
}
// Duyệt List .
NODE*getnode(int x)
{
NODE*p;
p=new NODE;
if(p==NULL)
return NULL;
p->Data=x;
p->pNext=NULL;
return p;
}
// Thêm vào đầu List .
void AddHead(LIST &l,NODE *new_ele)
{
if(l.pHead==NULL)
{
l.pHead=new_ele;
l.pTail=l.pHead;
}
else
{
new_ele->pNext=l.pHead;
l.pHead=new_ele;
}
}
// Thêm vào cuối List .
void AddTail(LIST &l,NODE *new_ele)
{
if(l.pHead==NULL)
{
l.pHead=new_ele;
l.pTail=l.pHead;
}
else
{
l.pTail->pNext=new_ele;
l.pTail=new_ele;
}
}
// Hàm nhập dữ liệu .
void InPut(LIST &l)
{
int n;
quaylai:printf("\nNhap vao so luong cac node:");
scanf("%d",&n);
if(n<0)
{
printf("\nSo ban nhap vao khong hop le!Xin vui long nhap lai!");
goto quaylai;
}
INit(l);
for(int i=1;i<=n;i++)
{
int x;
printf("\nNhap vao node thu %d:",i);
scanf("%d",&x);
NODE*p=getnode(x);
AddTail(l,p);
}
}
// Hàm xuất dữ liệu .
void OutPut(LIST l)
{
for(NODE*p=l.pHead;p!=NULL;p=p->pNext)
{
printf("%4d",p->Data);
}
}
// Hàm xử lý dữ liệu trên tất cả các Node .
void LietKeDemTinhTongTinhTichTimMaxTimMinCacNode(LIST l)
{
int dem=0,tong=0,tich=1;
int max=INT_MIN;
int min=INT_MAX;
for(NODE*p=l.pHead;p!=NULL;p=p->pNext)
{
printf("%4d",p->Data);
dem++;
tong+=p->Data;
tich*=p->Data;
if(p->Data>max)
{
max=p->Data;
}
if(p->Data<min)
{
min=p->Data;
}
}
printf("\n");
printf("\nSo Luong Cac Node La:%d",dem);
printf("\n");
printf("\nTong Cac Node La:%d",tong);
printf("\n");
printf("\nTich Cac Node La:%d",tich);
printf("\n");
printf("\nNode Lon Nhat La:%d",max);
printf("\n");
printf("\nNode Nho Nhat La:%d",min);
}
// Hàm xử lý dữ liệu trên các Node dương .
void LietKeDemTinhTongTinhTichTimMaxTimMinCacNodeDuong(LIST l)
{
int dem=0,tong=0,tich=1;
int max=INT_MIN;
int min=INT_MAX;
for(NODE*p=l.pHead;p!=NULL;p=p->pNext)
{
if(p->Data>=0)
{
printf("%4d",p->Data);
dem++;
tong+=p->Data;
tich*=p->Data;
if(p->Data>max)
{
max=p->Data;
}
if(p->Data<min)
{
min=p->Data;
}
}
}
printf("\n");
printf("\nSo Luong Cac Node Duong La:%d",dem);
printf("\n");
printf("\nTong Cac Node Duong La:%d",tong);
printf("\n");
printf("\nTich Cac Node Duong La:%d",tich);
printf("\n");
printf("\nNode Duong Lon Nhat La:%d",max);
printf("\n");
printf("\nNode Duong Nho Nhat La:%d",min);
}
// Hàm xử lý dữ liệu trên các Node âm .
void LietKeDemTinhTongTinhTichTimMaxTimMinCacNodeAm(LIST l)
{
int dem=0,tong=0,tich=1;
int max=INT_MIN;
int min=INT_MAX;
for(NODE*p=l.pHead;p!=NULL;p=p->pNext)
{
if(p->Data<0)
{
printf("%4d",p->Data);
dem++;
tong+=p->Data;
tich*=p->Data;
if(p->Data>max)
{
max=p->Data;
}
if(p->Data<min)
{
min=p->Data;
}
}
}
printf("\n");
printf("\nSo Luong Cac Node Am La:%d",dem);
printf("\n");
printf("\nTong Cac Node Am La:%d",tong);
printf("\n");
printf("\nTich Cac Node Am La:%d",tich);
printf("\n");
printf("\nNode Am Lon Nhat La:%d",max);
printf("\n");
printf("\nNode Am Nho Nhat La:%d",min);
}
// Hàm thêm vào cuối danh sách liên kết đơn một giá trị nguyên .
void ThemVaoCuoi(LIST l)
{
int x;
printf("\nNhap Vao Gia Tri Can Them Vao Cuoi Danh Sach Lien Ket Don:");
scanf("%d",&x);
NODE*p=getnode(x);
AddTail(l,p);
}
// Hàm hoán vị 2 Node .
void HoanVi(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
}
// Hàm sắp danh sách tăng dần .
void SapTangDan(LIST l)
{
for(NODE*p=l.pHead;p!=NULL;p=p->pNext)
{
for(NODE*q=p->pNext;q!=NULL;q=q->pNext)
{
if(p->Data>q->Data)
{
HoanVi(p->Data,q->Data);
}
}
}
}
// Hàm sắp danh sách giảm dần .
void SapGiamDan(LIST l)
{
for(NODE*p=l.pHead;p!=NULL;p=p->pNext)
{
for(NODE*q=p->pNext;q!=NULL;q=q->pNext)
{
if(p->Data<q->Data)
{
HoanVi(p->Data,q->Data);
}
}
}
}
// Hàm tăng kích cỡ chiều rộng và chiều cao của khung CMD .
void resizeConsole(int width, int height)
{
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, width, height, TRUE);
}
// Hàm MeNu .
void MeNu()
{
resizeConsole(2000,1000);
int tieptuc;
LIST lst;
quaylai:printf("\n>>>>>>>>>>>>>>>>Nhap List:<<<<<<<<<<<<<<<<<\n");
InPut(lst);
printf("\n>>>>>>>>>>>>>>>>>>>>Xuat List:<<<<<<<<<<<<<<<<<<<<\n");
OutPut(lst);
printf("\n");
printf("\n>>>>>>>>>>>>>>>>Xu Ly Cac Node:<<<<<<<<<<<<<<<<<<<<<\n");
LietKeDemTinhTongTinhTichTimMaxTimMinCacNode(lst);
printf("\n");
printf("\n>>>>>>>>>>>>>>>>>>>>Xu Ly Cac Node Duong:<<<<<<<<<<<<<<<<<<<<<<<\n");
LietKeDemTinhTongTinhTichTimMaxTimMinCacNodeDuong(lst);
printf("\n");
printf("\n>>>>>>>>>>>>>>>>>>>>Xu Ly Cac Node Am:<<<<<<<<<<<<<<<<<<<<<<\n");
LietKeDemTinhTongTinhTichTimMaxTimMinCacNodeAm(lst);
printf("\n");
ThemVaoCuoi(lst);
printf("\n>>>>>>>>>>>>>>Danh Sach Sau Khi Them Vao Cuoi La:<<<<<<<<<<<<<<<<<<<<\n");
OutPut(lst);
printf("\n");
printf("\n>>>>>>>>>>>>>>>Sap Danh Sach Tang Dan:<<<<<<<<<<<<<<<<<<<<<<\n");
SapTangDan(lst);
OutPut(lst);
printf("\n");
printf("\n>>>>>>>>>>>>>>>Sap Danh Sach Giam Dan:<<<<<<<<<<<<<<<<<<<<<<\n");
SapGiamDan(lst);
OutPut(lst);
printf("\n");
printf("\nBan co muon tiep tuc thuc hien chuong trinh hay khong ? Neu co bam phim C,nguoc lai bam bat ky 1 phim nao khac de ket thuc!");
tieptuc=getch();
if(tieptuc=='c'||tieptuc=='C')
{
system("cls");
goto quaylai;
}
}
// Hàm chính .
void main()
{
MeNu();
}
|
Nhận xét
Đăng nhận xét