#include<stdio.h> #include<ADTList.h> #include<stdlib.h> List ListStart; Position pin; int nodenum = 0; structNode { int element; int node; Position next; }; List MakeEmpty() { List L= (List)malloc(sizeof(struct Node)); L->element = 0; L->next = NULL; L->node = nodenum; nodenum++; return L; }
Position Find(int X) { Position P; P = ListStart->next; while ( P != NULL && P -> element!=X) { P = P -> next; } return P; }
Position FindPrevious(int X) { Position P,TmpCell; TmpCell = ListStart; P = ListStart->next; while ( P != NULL && P -> node!=X) { TmpCell = P; P = P -> next; } return TmpCell; }
voidInsert(int X, Position P) { Position TmpCell = (Position)(malloc(sizeof(struct Node))); if (TmpCell == NULL) printf("Out of space"); TmpCell->element = X; TmpCell->next = P->next; P->next = TmpCell; nodenum++; RefreshNode(); }
voidPrintList() { Position P = ListStart; do { printf("节点%d:\t%d\n", P->node, P->element); P = P->next; } while (P != NULL); printf("节点个数为:%d\n", nodenum); }
voidRefreshNode() { Position ptemp = ListStart; int j = 0; do { ptemp->node = j; ptemp = ptemp->next; j++; } while (ptemp != NULL); }
//.c文件中声明的函数 voidDeleteX() { int X; printf("请输入要删除的节点:"); scanf_s("%d", &X); if (X != 0) { Delete(X); } else { printf("不能删除头结点!"); } }
voidFindX() { int X; printf("请输入要查找的整数元素数值:"); scanf_s("%d", &X); Position P = Find(X); printf("%d位于链表的第%d个节点\n", X, P->node);
}
voidInsertX() { int num; int X; Position P=ListStart; printf("请输入要插入的位置:"); scanf_s("%d",&num); while ((P->next)->node!=num) { P = P->next; } printf("请输入要插入的整数元素:"); scanf_s("%d",&X); Insert(X,P); }