单项不循环链表
单向不循环链表
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
typedef int DataType_t;
// 构造链表的结点,链表中所有结点的数据类型是相同的
typedef struct LinkedList
{
DataType_t data; // 节点的数据域
struct LinkedList *next; // 节点的指针域
} LiList_t;
// 创建一个空链表,空链表应该有一个头节点,对链表进行初始化
LiList_t *LiList_Creat(void)
{
// 1. 创建一个头结点,为头节点申请内存
LiList_t *Head = (LiList_t *)calloc(1, sizeof(LiList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is failed");
return NULL;
}
// 2. 对头节点进行初始化,头节点是不存储有效内容的
Head->next = NULL;
// 3. 把头结点的地址返回即可
return Head;
}
// 创建链表的新的节点并对链表进行初始化
LiList_t *LiList_NewNode(DataType_t data)
{
// 1. 创建一个新结点,为新节点申请内存
LiList_t *New = (LiList_t *)calloc(1, sizeof(LiList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is failed");
return NULL;
}
// 2. 对新节点进行初始化
New->data = data;
New->next = NULL;
return New;
}
// 将新的节点插入到链表首节点//头插
bool LiList_HeadInsert(LiList_t *Head, DataType_t data)
{
// 1. 创建新节点并对新节点进行初始化
LiList_t *New = LiList_NewNode(data);
if (NULL == New)
{
perror("can not insert new node\n");
return false;
}
// 2. 将新节点插入到链表头部
New->next = Head->next;
Head->next = New;
return true;
}
// 将新的节点插入到链表尾节点//尾插
bool LiList_tailInsert(LiList_t *Head, DataType_t data)
{
// 1. 创建新节点并对新节点进行初始化
LiList_t *New = LiList_NewNode(data);
if (NULL == New)
{
perror("can not insert new node\n");
return false;
}
// 2. 找到链表的尾节点
LiList_t *Phead = Head;
while (Phead->next)
{
Phead = Phead->next;
}
// 3. 将新节点插入到链表尾部
Phead->next = New;
return true;
}
// 将新的节点插入到链表指定节点//指定插
bool LiList_DestInsert(LiList_t *Head, DataType_t dest, DataType_t data)
{
// 1. 创建新节点并对新节点进行初始化
LiList_t *New = LiList_NewNode(data);
if (NULL == New)
{
perror("can not insert new node\n");
return false;
}
// 2. 找到指定节点的前一个节点
LiList_t *Phead = Head;
while (Phead->next != NULL && Phead->next->data != dest)
{
Phead = Phead->next;
}
if (Phead->next == NULL)
{
printf("data is not found\n");
return false;
}
// 3. 将新节点插入到指定节点之后
New->next = Phead->next;
Phead->next = New;
return true;
}
// 遍历
void LiList_Print(LiList_t *Head)
{
// 对链表的头节点的地址进行备份
LiList_t *Phead = Head;
while (Phead->next)
{
// 把头节点的直接后继作为新的节点
Phead = Phead->next;
// 输出当前节点的直接后继的数据域
printf("data = %d\n", Phead->data);
}
}
// 删除首节点
bool LiList_HeadDel(LiList_t *Head)
{
if (Head == NULL || Head->next == NULL)
{
perror("The LiList is empty");
return false;
}
LiList_t *DelNode = Head->next;
Head->next = DelNode->next;
free(DelNode);
return true;
}
// 删除尾节点
bool LiList_tailDel(LiList_t *Head)
{
if (Head == NULL || Head->next == NULL)
{
perror("The LiList is empty");
return false;
}
if (Head->next->next == NULL)
{
free(Head->next);
Head->next = NULL;
return true;
}
LiList_t *Phead = Head;
while (Phead->next->next != NULL)
{
Phead = Phead->next;
}
free(Phead->next);
Phead->next = NULL;
return true;
}
// 删除指定节点
bool LiList_DestDel(LiList_t *Head, DataType_t dest)
{
if (Head == NULL || Head->next == NULL)
{
perror("The LiList is empty");
return false;
}
LiList_t *Phead = Head;
while (Phead->next != NULL && Phead->next->data != dest)
{
Phead = Phead->next;
}
if (Phead->next == NULL)
{
printf("data is not found\n");
return false;
}
LiList_t *DelNode = Phead->next;
Phead->next = DelNode->next;
free(DelNode);
return true;
}
int main(int argc, char const *argv[])
{
LiList_t *Head = LiList_Creat();
LiList_tailInsert(Head, 1);
LiList_tailInsert(Head, 2);
LiList_tailInsert(Head, 3);
LiList_tailInsert(Head, 4);
// LiList_HeadInsert(Head, 5);
// LiList_TailInsert(Head, 5);
// LiList_DestInsert(Head, 4, 5);
// LiList_HeadDel(Head);
// LiList_TailDel(Head);
// LiList_DestDel(Head, 3);
LiList_Print(Head);
return 0;
}
运行结果


浙公网安备 33010602011771号