實現單鏈表及操作:
每行一條指令:
i 3 -1 → [3]
i 4 3 → [3,4]
i 7 -1 → [7,3,4]
i 5 8 → 8不存在,印 "Insertion(5) Failed: element 8 is not in the list"
d 3 → [7,4]
i 2 7 → [7,2,4]
d 9 → 9不存在,印 "Deletion failed: element 9 is not in the list"
f 3 → 3不存在,印 "Could not find 3 in the list"
f 7 → 前驅是header,印 "Key of previous node of 7 is header"
f 2 → 前驅是7,印 "Key of previous node of 2 is 7"
p → 印 "Key:7 Key:2 Key:4"
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node {
int Key;
PtrToNode Next;
};
List MakeEmpty(List L); // 初始化空表(帶頭節點)
Position Find(List L, int Key); // 查找節點
Position FindPrevious(List L, int Key); // 查前驅
void Insert(List L, int Key, Position P); // 在P後插入
void Delete(List L, int Key); // 刪除Key節點
void PrintList(List L); // 印表
void DeleteList(List L); // 釋放記憶體