本篇用C语言描述单链表。
欢迎各位大佬指正代码中错误或者不合适的地方
以下是头文件:
#ifndef __LIST_H_
#define __LIST_H_
#include <stdbool.h>
#include <stdint.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
#include <memory.h>
typedef struct
{
char x;
uint64_t y;
} ElementType;
typedef struct node
{
ElementType Element;
struct node *Next;
} Node;
typedef Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List InitList(void);
bool IsEmpty(List L);
bool IsLast(List L, Position P);
Position Find(List L, ElementType X);
void Delete(List L, ElementType X);
Position FindPrevious(List L, ElementType X);
void AddNodeAtHead(List L, ElementType X);
void AddNodeAtEnd(List L, ElementType X);
void PrintAllNode(List L);
void DeleteList(List L);
#endif
以下是定义函数的.c文件:
/*****************************************************************************
Copyright: Null
File name: list.c
Description: 包含对单链表的初始化,添加节点,删除节点,寻找节点等常见操作
Author: 罗腾伟
Version: 1.0
Date: 2016.12.31
*****************************************************************************/
#include "list.h"
/*************************************************
Function: InitList
Description: 初始化一个链表的头结点
Calls:
Called By:
Input: void
Output:
Return: @List : 链表头结点的地址
Others: 需要事先定义一个指向节点类型的变量
Example:
List test;
test = InitList();
*************************************************/
List InitList(void)
{
List L;
L = (List)malloc(sizeof(Node));
L->Next = NULL;
memset(&L->Element, 0, sizeof(ElementType));
return L;
}
/*************************************************
Function: IsEmpty
Description: 判断一个链表是否为空
Calls:
Called By:
Input: @List L:链表头结点
Output:
Return: @true :空表
@false :非空
Others:
Example:
*************************************************/
bool IsEmpty(List L)
{
return L->Next == NULL;
}
/*************************************************
Function: IsLast
Description: 判断链表中某个节点是否是最后一个节点
Calls:
Called By:
Input: @List L :链表头结点
@Position P :链表中的结点
Output:
Return: @true : 最后一个节点
@false : 不是最后一个节点
Others:
Example:
*************************************************/
bool IsLast(List L, Position P)
{
return P->Next == NULL;
}
/*************************************************
Function: StructCompare
Description: 通过比较内存中的内容比较两个结构体中的内容是否相等
Calls: memcmp
Called By: Find
Input: @void *struct1 : 结构体1的地址
@void *struct2 : 结构体2的地址
@uint16_t length : 结构体长度
Output:
Return: @true : 内容相同
@false:内容不同
Others:需要注意填充字节
Example:
*************************************************/
static bool StructCompare(void *struct1, void *struct2, uint16_t length)
{
return (0 == memcmp(struct1, struct2, length));
}
Position Find(List L, ElementType X)
{
Position P;
P = L->Next;
while (P != NULL && !StructCompare(&X, &P->Element, sizeof(ElementType)))
{
P = P->Next;
}
return P;
}
void Delete(List L, ElementType X)
{
Position P, TmpCell;
P = FindPrevious(L, X);
if (!IsLast(L, P))
{
TmpCell = P->Next;
P->Next = TmpCell->Next;
free(TmpCell);
}
}
Position FindPrevious(List L, ElementType X)
{
Position P;
P = L;
while (P->Next != NULL && !StructCompare(&X, &P->Next->Element, sizeof(ElementType)))
P = P->Next;
return P;
}
void AddNodeAtHead(List L, ElementType X)
{
Position TmpCell;
TmpCell = (Position)malloc(sizeof(Node));
TmpCell->Element = X;
TmpCell->Next = L->Next;
L->Next = TmpCell;
}
void AddNodeAtEnd(List L, ElementType X)
{
Position TmpCell;
TmpCell = (Position)malloc(sizeof(Node));
TmpCell->Element = X;
TmpCell->Next = NULL;
Position TmpL = L;
while (TmpL->Next != NULL)
{
TmpL = TmpL->Next;
}
TmpL->Next = TmpCell;
}
void PrintAllNode(List L)
{
Position P;
P = L->Next;
printf("******************************\r\n");
while (P != NULL)
{
printf("x = %c, y = %d\r\n", P->Element.x, P->Element.y);
P = P->Next;
}
printf("******************************\r\n\n\n");
}
void DeleteList(List L)
{
Position temp, P;
P = L->Next;
L->Next = NULL;
while (P != NULL)
{
temp = P->Next;
free(P);
P = temp;
}
}
欢迎各位大佬指正代码中错误或者不合适的地方