#pragma once
#include <stdbool.h>
//带头节点的单链表
//单链表尾节点的next尾NULL
//List为一条链表,Node *一个节点的地址
typedef struct Node
{
int data; //数据
struct Node *next; //下一个节点的地址
}Node,*List; //List = Node *
//初始化
void InitList(List plist);
//头插法
bool Insert_head(List plist, int val);
//尾插法
bool Insert_tail(List plist, int val);
//在pos下标插入数据val
bool Insert_pos(List plist, int pos, int val);
//查找,找到返回节点地址,没有找到返回NULL
Node *Search(List plist, int key);
//删除第一个key对应的节点
bool Delete(List plist, int key);
//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(List plist, int *rtval);
//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(List plist, int *rtval);
//获取长度,统计数据节点的个数
int GetLength(List plist);
//判空
bool IsEmpty(List plist);
//清除所有数据
void Clear(List plist);
//销毁所有节点
void Destory(List plist);
//打印
void Show(List plist);
.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "list.h"
//初始化,头节点已经存在
void InitList(List plist)
{
assert(plist != NULL);
plist->next = NULL;
}
//头插法
bool Insert_head(List plist, int val)
{
assert(plist != NULL);
Node *p = (Node *)malloc(sizeof(Node));
p->data = val;
p->next = plist->next; //先剪后链接,插入顺序是逆序
plist->next = p;
return true;
}
//尾插法
bool Insert_tail(List plist, int val)
{
assert(plist != NULL);
//第一步,创建新节点
Node *p = (Node *)malloc(sizeof(Node));
if(p == NULL)
{
return false;
}
p->data = val;
//第二步,找尾节点
Node *q;
for(q = plist; q->next != NULL; q = q->next)
;
//将p插入在q的后面
p->next = q->next;
q->next = p;
return true;
}
//在pos下标插入数据val
bool Insert_pos(List plist, int pos, int val)
{
if(pos < 0 )
{
return false;
}
int i;
Node *q;
for(i = 0,q=plist; q->next != NULL&& i < pos; i++,q = q->next)
{
;
}
if(i < pos)
{
return false;
}
Node *p = (Node *)malloc(sizeof(Node));
p->data = val;
p->next = q->next;
q->next = p;
return true;
}
//查找,找到返回节点地址,没有找到返回NULL
Node *Search(List plist, int key)
{
//遍历所有的数据节点
for(Node *p = plist->next; p != NULL; p = p->next)
{
if(p->data == key)
{
return p;
}
}
return NULL;
}
//查找key的前趋节点
static Node *SearchPri(List plist, int key)
{
assert(plist != NULL);
for(Node *p = plist; p->next != NULL; p = p->next)
{
if(p->next->data == key)
{
return p;
}
}
return NULL;
}
//删除第一个key对应的节点
bool Delete(List plist, int key)
{
Node *p = SearchPri(plist, key);
if(p == NULL)
{
return false;
}
Node *q = p->next; //q指向将要删除的节点
p->next = q->next; //将q从链表中剔除
free(p); //释放内存
return true;
}
//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(List plist, int *rtval)
{
if(plist->next == NULL)
{
return false;
}
Node *p = plist->next;
if(rtval != NULL)
{
*rtval = p->data;
}
plist->next = p->next;
free(p);
return true;
}
//删除最后一个数据节点,并通过rtval获得删除的值
//倒数第二个点p->next->next != NULL
bool Delete_tail(List plist, int *rtval)
{
if(plist->next == NULL)
{
return false;
}
Node *p;//指向倒数第二个点
for(p = plist; p->next->next != NULL; p = p->next)
;
Node *q = p->next;
if(rtval != NULL)
{
*rtval = q->data;
}
free(q);
p->next = NULL;
return true;
}
//获取长度,统计数据节点的个数
int GetLength(List plist)
{
int count = 0;
for(Node *p = plist->next; p != NULL; p = p->next)
{
count++;
}
return count;
}
//判空
bool IsEmpty(List plist)
{
return plist->next == NULL;
}
//清除所有数据
void Clear(List plist)
{
Destory(plist);
}
//销毁所有节点
void Destory(List plist)
{
Node *p;
while(plist->next != NULL)
{
p = plist->next;
plist->next = p->next;
free(p);
}
}
//打印
void Show(List plist)
{
for(Node *p = plist->next; p != NULL; p = p->next)
{
printf("%d ",p->data);
}
printf("\n");
}
main.c
#include <stdio.h>
#include "list.h"
int main()
{
Node head;
InitList(&head);
// List p; // Node *p;
// InitList(p);
for(int i = 0; i < 10; i++)
{
//正序
Insert_tail(&head, i);
//逆序
// Insert_head(&head, i);
}
Show(&head);
Insert_pos(&head,1,30);
Insert_pos(&head,5,80);
Insert_pos(&head,12,300);
Show(&head);
Delete(&head,30);
Delete(&head,80);
Delete(&head,300);
Show(&head);
//删除头节点
Delete_head(&head,0);
Show(&head);
return 0;
}