数据结构---单链表

来源:互联网 发布:mac cp 拷贝文件夹 编辑:程序博客网 时间:2024/06/14 02:51
本篇用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: NullFile name: list.cDescription: 包含对单链表的初始化,添加节点,删除节点,寻找节点等常见操作Author: 罗腾伟Version: 1.0Date: 2016.12.31*****************************************************************************/#include "list.h"/*************************************************Function: InitListDescription: 初始化一个链表的头结点Calls: Called By: Input: voidOutput: 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: IsEmptyDescription: 判断一个链表是否为空Calls:Called By:Input:@List L:链表头结点Output:Return:@true  :空表   @false :非空Others: Example:*************************************************/bool IsEmpty(List L){return L->Next == NULL;}/*************************************************Function: IsLastDescription: 判断链表中某个节点是否是最后一个节点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: StructCompareDescription: 通过比较内存中的内容比较两个结构体中的内容是否相等Calls:memcmpCalled By:FindInput:@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;}}


欢迎各位大佬指正代码中错误或者不合适的地方大笑

1 0
原创粉丝点击