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