#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ElemType int
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
// 帶頭節點的初始化
bool InitList(LinkList &headList)
{
headList = (LNode*) malloc(sizeof(LNode));
if(headList == NULL){
return false;
}
headList->next = NULL;
return true;
}
// 在指定結點p後插入元素e
bool InsertNextNode(LNode *p, ElemType e)
{
if(p == NULL){
return false;
}
LNode *node = (LNode *)malloc(sizeof(LNode));
if(node == NULL){ // 內存分配失敗
return false;
}
node->data = e;
node->next = p->next; // 連接node的後繼結點
p->next = node; // 連接node的前驅結點
// printf("%d\n", p->next->data);
return true;
}
// 在第idx的位置上插入e
bool ListInsert(LinkList &headList, int idx, ElemType e)
{
if(idx < 1){
return false;
}
LNode *p = headList; // headList指向頭結點 頭結點不存數據
int j = 0; // 當前p指向第幾個結點
while(p != NULL && j < idx - 1){ // 循環找到第idx-1個節點
p = p->next;
j++;
}
if(p == NULL){ // idx值不合法
return false;
}
return InsertNextNode(p, e); // 調用在指定結點後插入元素的函數
}
// 在指定結點p之前插入元素e O(n)方法
bool InsertPriorNode(LinkList &headList, LNode *p, ElemType e)
{
LNode *tmp = headList;
while(tmp != NULL){ // 循環找到p結點的前一個結點
if(tmp->next == p){
break;
}
tmp = tmp->next;
}
if(tmp == NULL){
return false;
}
LNode *node = (LNode *)malloc(sizeof(LNode));
if(node == NULL){
return false;
}
node->data = e;
node->next = p;
tmp->next = node;
return true;
}
// 在指定結點p之前插入元素e O(1)方法
bool InsertPriorNode(LNode *p, ElemType e)
{
if(p == NULL){
return false;
}
LNode *node = (LNode *)malloc(sizeof(LNode));
if(node == NULL){
return false;
}
// 移動數據 實現插入結點
node->data = p->data; // 新申請的結點替換掉當前的p結點
p->data = e; // 然後把p結點的數據更換,這樣p就成了先前p的前驅結點
node->next = p->next; // 連接
p->next = node;
return true;
}
// 刪除操作 刪除第idx個位置的元素
bool ListDelete(LinkList &headList, int idx)
{
if(idx < 0){
return false;
}
LNode *p = headList;
for(int i = 0; i < idx-1 && p != NULL; i++){ // 循環找到第i-1個結點
p = p->next;
}
if(p == NULL || p->next == NULL){ // 如果idx無效
return false;
}
LNode *tmp = p->next; // tmp爲當前要刪除的結點
p->next = tmp->next;
free(tmp);
return true;
}
// 刪除指定結點 O(n)方法
bool DeleteNode(LinkList &headList, LNode *p)
{
if(p == NULL){
return false;
}
LNode *tmp = headList;
while(tmp->next != p){ // 循環找到被刪除結點的前驅結點
tmp = tmp->next;
}
tmp->next = p->next;
free(p);
return true;
}
// 刪除指定結點 O(1)方法
// 缺陷是無法刪除最後一個結點
bool DeleteNode(LNode *p)
{
if(p == NULL || p->next == NULL){ // 如果p不存在或p是最後一個結點
return false;
}
LNode *q = p->next; // q是被刪除結點p的後繼結點
p->data = q->data; // 把p變成q結點
p->next = q->next; // p在與q的後繼結點相連,等價於刪除了p結點
free(q);
return true;
}
// 帶頭節點的判空方法
bool IsEmpty(LinkList &headList)
{
return (headList->next == NULL);
}
int main()
{
LinkList headList;
bool flag = false;
flag = InitList(headList);
printf("%d\n", flag);
for(int i = 0; i < 10; i++){
ListInsert(headList, i+1, i+1);
}
// 調用鏈表操作函數
ListInsert(headList, 6, 5);
InsertPriorNode(headList, headList->next->next, 10);
InsertPriorNode(headList->next->next, 11);
ListDelete(headList, 8);
DeleteNode(headList, headList->next->next);
DeleteNode(headList->next->next);
int i = 1;
for(LNode *p = headList->next; p != NULL; p = p->next){
printf("第%d個結點的值是:%d\n", i++, p->data);
}
return 0;
}