#include#include #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("%dt%dn", 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("%dt%dt%dn", first->Key, first->Data,first->count); } printf("nn"); } 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。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)