数据结构---单链表
来源:互联网 发布: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
- 数据结构---单链表
- 数据结构--单链表
- 数据结构单链表
- 数据结构单链表
- 【数据结构】单链表
- 数据结构单链表
- [数据结构]单链表
- 数据结构--单链表
- 数据结构--单链表
- 数据结构-单链表
- 数据结构 -- 单链表
- 数据结构-单链表
- 数据结构 --- 单链表
- 数据结构---单链表
- 数据结构--单链表
- 数据结构 单链表
- 数据结构-单链表
- 数据结构---单链表
- c++ GUI Qt4 PDF
- 浅谈MyBatis中的缓存
- ScrollView 的一个小方法
- 关于Android Studio无法查看详细的源代码问题解决
- 第十章 会话管理——《跟我学Shiro》
- 数据结构---单链表
- cenos6.5 xxx is not in the sudoers file. This incident will be reported.
- POJ 1401 Factorial 阶乘0的数量 .
- android之官方下拉刷新组件SwipeRefreshLayout
- 基于spring整合activeMQ以及点对点队列的封装
- 环信iOS使用步骤及使用总结
- View的scrollTo和scrollBy
- 对root安卓设备修改时间的方法
- js刷新页面方法大全