#include<stdio.h>
#include<stdlib.h>
#define IS_FULL(ptr) (!(ptr))
typedef int T;
typedef struct node {
T Key;
T Data;
T count;
struct node* Link;
}Node;
Node* BuildList() {
Node* p;
Node* r = NULL;
Node* first = NULL;
int i;
for (i = 0; i < 10; i++) {
p = (Node*)malloc(sizeof(Node));
p->Key = 2 * i + 1;
p->Data = 2 * i + 2;
p->count = 0;
p->Link = NULL;
if (first != NULL) {
r->Link = p;
}
else {
first = p;
}
r = p;
}
return first;
}
Node* SearchList(Node* first, int k) {
Node* p;
Node* r;
int x, y,z;
int i = 1;
r = (Node*)malloc(sizeof(Node));
p = (Node*)malloc(sizeof(Node));
r->Link = first;
p = first;
if (first->Key == k) {
i = 0;
}
while (p != NULL) {
if (p->Key != k) {
r = r->Link;
p = p->Link;
}
else {
printf("%d\t%d\n", p->Key, p->Data);
p->count++;
if (i) {
if (p->count>r->count) {
x = p->Key;
y = p->Data;
z = p->count;
p->Key = r->Key;
p->Data = r->Data;
p->count = r->count;
r->Key = x;
r->Data = y;
r->count = z;
}
}
break;
}
}
if (p == NULL) {
printf("Can't find it!\n");
exit(1);
}
else {
return first;
}
}
void PrintList(Node* first) {
printf("\n the list contains: \n");
for (; first; first = first->Link) {
printf("%d\t%d\t%d\n", first->Key, first->Data,first->count);
}
printf("\n\n");
}
void Clear(Node** first) {
Node* p;
p = *first;
while (*first) {
p = (*first)->Link;
free(*first);
*first = p;
}
}
void main() {
Node* lst;
lst = BuildList();
PrintList(lst);
lst = SearchList(lst, 13);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 11);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
lst = SearchList(lst, 1);
PrintList(lst);
//Clear(&lst);
//PrintList(lst);
}
运行结果:
the list contains:
1 2 0
3 4 0
5 6 0
7 8 0
9 10 0
11 12 0
13 14 0
15 16 0
17 18 0
19 20 0
13 14
the list contains:
1 2 0
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
11 12 0
15 16 0
17 18 0
19 20 0
11 12
the list contains:
1 2 0
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
11 12 1
15 16 0
17 18 0
19 20 0
11 12
the list contains:
1 2 0
3 4 0
5 6 0
7 8 0
9 10 0
11 12 2
13 14 1
15 16 0
17 18 0
19 20 0
11 12
the list contains:
1 2 0
3 4 0
5 6 0
7 8 0
11 12 3
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
11 12
the list contains:
1 2 0
3 4 0
5 6 0
11 12 4
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
11 12
the list contains:
1 2 0
3 4 0
11 12 5
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
11 12
the list contains:
1 2 0
11 12 6
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
11 12
the list contains:
11 12 7
1 2 0
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
11 12
the list contains:
11 12 8
1 2 0
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
11 12
the list contains:
11 12 9
1 2 0
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
11 12
the list contains:
11 12 10
1 2 0
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 1
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 2
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 3
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 4
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 5
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 6
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 7
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 8
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 9
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
11 12 10
1 2 10
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
1 2
the list contains:
1 2 11
11 12 10
3 4 0
5 6 0
7 8 0
9 10 0
13 14 1
15 16 0
17 18 0
19 20 0
C:\C程序&C语言数据结构\C语言数据结构_从入门到入坑\7-15\计数方法-自组织线性表\Debug\计数方法-自组织线性表.exe (进程 13976)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .