

typedef int ELEM_TYPE;
//带头结点的单链表
//有效节点的结构体设计
typedef struct Node
{
ELEM_TYPE data; //数据域 //保存节点有效值
struct Node *next; //指针域 //保存下一个有效节点的地址
}Node, *PNode;
//typedef struct Node Node;
//typedef struct Node* PNode;
单链表的基本操作有 初始化,头插,尾插,按位置插,头删,尾删,按位置删,查找,按值删,获取有效值个数,判空,清空,销毁,打印
函数
//初始化
void Init_list(PNode plist);
//头插
bool Insert_Head(struct Node *plist, ELEM_TYPE val);
//尾插
bool Insert_Tail(struct Node *plist, ELEM_TYPE val);
//按位置插
bool Insert_pos(struct Node *plist, int pos, ELEM_TYPE val);
//头删
bool Del_head(struct Node* plist);
//尾删
bool Del_tail(struct Node *plist);
//按位置删
bool Del_pos(struct Node *plist, int pos);
//按值删
bool Del_val(struct Node *plist, ELEM_TYPE val);
//查找节点
struct Node* Search(struct Node *plist, ELEM_TYPE val);
//获取有效值个数
int Get_Length(struct Node *plist);
//判空
bool IsEmpty(struct Node *plist);
//清空
void Clear(struct Node *plist);
//20:10
//销毁
void Destroy(struct Node *plist);
//打印
void Show(struct Node *plist);
void Init_list(PNode plist)
{
//assert plist
plist->next = NULL;
//plist->data; 头结点的数据域不使用
}

//头插
bool Insert_Head(struct Node *plist, ELEM_TYPE val)
{
//1.assert plist
assert(plist != NULL);
//2.购买新节点(购买好节点之后,记得将val值赋值进去)
struct Node *pnewnode = (struct Node *)malloc(1 * sizeof(struct Node));
assert(pnewnode != NULL);
pnewnode->data = val;
//3.找到合适的插入位置
//因为是头插函数 所以不需要特意的去合适的位置 直接向plist后面插即可
//4.插入
pnewnode->next = plist->next;
plist->next = pnewnode;
return true;
}

//尾插
bool Insert_Tail(struct Node *plist, ELEM_TYPE val)
{
//1.assert plist
assert(plist != NULL);
//2.购买新节点(购买好节点之后,记得将val值赋值进去)
struct Node *pnewnode = (struct Node *)malloc(1 * sizeof(struct Node));
assert(pnewnode != NULL);
pnewnode->data = val;
//3.找到合适的插入位置
struct Node *p = plist;
for(p; p->next!=NULL; p=p->next);
//此时 p就指向尾结点
//4.插入
pnewnode->next = p->next;
p->next = pnewnode;
return true;
}


//按位置插
bool Insert_pos(struct Node *plist, int pos, ELEM_TYPE val)
{
//assert
assert(plist != NULL);
assert(pos>=0 && pos<=Get_Length(plist));
//1.购买新节点(购买好节点之后,记得将val值赋值进去)
struct Node *pnewnode = (struct Node *)malloc(sizeof(struct Node));
assert(pnewnode != NULL);
pnewnode->data = val;
//2.找到合适的插入位置(让指针p指向合适的节点)
struct Node *p = plist;
for(int i=0; i<pos; i++)
{
p=p->next;
}
//3.插入
pnewnode->next = p->next;
p->next = pnewnode;
return true;
}

//头删
bool Del_head(struct Node* plist)
{
//1.对plist 断言
assert(plist != NULL);
//2.删除需要判空
if(IsEmpty(plist))
{
return false;
}
//3.先申请一个临时指针p,指向第一个有效节点的地址
struct Node *p = plist->next;
//4.头结点的next域跨越指向(不再指向第一个有效节点,而是指向第二个)
plist->next = p->next;
//plist->next = plist->next->next;
free(p);
return true;
}

//尾删
bool Del_tail(struct Node *plist)
{
//1.对plist 断言
assert(plist != NULL);
//2.删除需要判空(判空链表)
if(IsEmpty(plist))
{
return false;
}
//3.让临时指针p指向待删除节点(尾结点)
struct Node *p = plist;
for(; p->next!=NULL; p=p->next);
//4.再让临时指针q指向倒数的第二个节点
struct Node *q = plist;
for(; q->next!=p; q=q->next);
//5.跨越指向,并释放待删除节点
q->next = p->next;
//q->next = NULL;
free(p);
return true;
}

//按位置删
bool Del_pos(struct Node *plist, int pos)
{
//1.对plist 断言 pos做合法性判断
assert(plist != NULL);
assert(pos >=0 && pos < Get_Length(plist));
//2.删除需要判空(判空链表)
if(IsEmpty(plist))
{
return false;
}
//3.pos="几",则让指针q从头结点开始向后跑"几"步(此时,q指向待删除节点的上一个节点)
struct Node *q = plist;
for(int i=0; i<pos; i++)//此时,for循环结束,指针q指向待删除节点的上一个节点
{
q=q->next;
}
//4.直接让q的next域给到指针p(此时,指针p指向待删除节点)
struct Node *p = q->next;//此时,指针p指向待删除节点
//5.跨越指向,释放待删除节点
q->next = p->next;
free(p);
return true;
}

//按值删
bool Del_val(struct Node *plist, ELEM_TYPE val)
{
//assert plist
//删除需要判空(判空链表)
if(IsEmpty(plist))
{
return false;
}
//用指针p去接收Search函数的返回值
struct Node *p = Search(plist, val);
if(p == NULL)//如果p==NULL 代表val不存在
{
return false;
}
//执行这一行时,代表val值节点存在,且此时用p指向
//让指着q停留在指针p的上一个节点位置
struct Node *q = plist;
for(; q->next!=p; q=q->next);
//此时p和q分别已经指向了待删除节点和待删除节点的上一个节点
//,则此时直接跨越指向,并且释放待删除节点
q->next = p->next;
free(p);
return true;
}

//查找节点
struct Node* Search(struct Node *plist, ELEM_TYPE val)
{
//assert plist
struct Node *p = plist->next;
for(; p!=NULL; p=p->next)
{
if(p->data == val)
{
return p;
}
}
return NULL;
}
int Get_Length(struct Node *plist)
{
//assert
int count = 0;
struct Node *p = plist->next;
for(; p!=NULL; p=p->next)
{
count++;
}
return count;
}
//判空
bool IsEmpty(struct Node *plist)
{
assert(plist != NULL);
//如果头结点的next域为NULL,则代表没有有效节点,为空链表
return plist->next == NULL;
}
有两种销毁方式
无限头删

双指针

//销毁1(无限头删)
void Destroy1(struct Node *plist)
{
/*while(!IsEmpty(plist))
{
Del_head(plist);
}*/
while(plist->next != NULL)
{
struct Node *p = plist->next;
plist->next = p->next;
free(p);
}
}
//销毁2(不借助头结点, 需要两个指针)
void Destroy(struct Node *plist)
{
//assert
struct Node *p = plist->next;
struct Node *q = NULL;
while(p != NULL)
{
q = p->next;
free(p);
p = q;
}
plist->next = NULL;
}
//打印
void Show(struct Node *plist)
{
struct Node *p = plist->next;
for(; p!=NULL; p=p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
//单链表测试用例
int main()
{
struct Node head;
Init_list(&head);
for(int i=0; i<10; i++)
{
Insert_pos(&head, i, i+1);
}
Show(&head);
Insert_Head(&head, 100);
Insert_Tail(&head, 200);
Show(&head);
Del_head(&head);
Del_tail(&head);
Show(&head);
Del_pos(&head, 3);//1 2 3 5 6 7 8 9 10
Del_val(&head, 7);//1 2 3 5 6 8 9 10
Show(&head);
printf("length = %d\n", Get_Length(&head));
Destroy(&head);
Show(&head);
return 0;
}
结果如下:

