单链表的函数声明和定义
来源:互联网 发布:gx48b软件下载 编辑:程序博客网 时间:2024/06/08 13:13
#include<stdio.h>#include<stdlib.h>typedef int ElemType;typedef struct node {ElemType data;struct node *next;}Node;typedef Node* PtrToNode;typedef PtrToNode List;typedef PtrToNode Position;//判断是否是空链表int IsEmpty(List L) {return L->next == NULL;}//判断是否是末尾int IsLast(Position P, List L) {return P->next == NULL;}//寻找Position Find(ElemType X, List L) {Position P;P = L->next;while (P != NULL && P->data != X) {P = P->next;}return P;}Position FindPrevious(ElemType X, List L) {Position P;P = L;while (P->next != NULL && P->next->data != X) {P = P->next;}return P;}void Delete(ElemType X, List L) {Position P, Tmp;P = FindPrevious(X, L);if (!IsLast(P,L)) {Tmp = P->next;P->next = Tmp;free(Tmp);}}//根据位置来插入元素 void InSert(ElemType X, List L, Position P) {Position TmpCell;TmpCell = (List)malloc(sizeof(Node));if (TmpCell == NULL) {printf("Out of space!!!");}TmpCell->data = X;TmpCell->next = P->next;P->next = TmpCell;}//输出链表 void PrintList(List L) {Position P;P = L;while (P != NULL) {printf("%d ", P->data);P = P->next;}printf("\n");}//创建链表 List CreatedList() {Position head, p1, p2;int n;p2 = p1 = (Node*)malloc(sizeof(Node));if (p1 == NULL) {printf("Can not create p1!!!");return NULL;}else {head = NULL;n = 0;printf("请输入数据:");scanf_s("%d", &(p1->data));while (p1->data != 0) {n++;if (n == 1) {head = p1;p2->next = NULL;}else {p2->next = p1;}p2 = p1;p1 = (struct node*)malloc(sizeof(struct node));printf("请输入数据:");scanf_s("%d", &(p1->data));}p2->next = NULL;free(p1);p1 = NULL;return head;}}//删除链表 void DeLeteList(List L) {Position P, Tmp;P = L->next;L->next = NULL;while (P != NULL) {Tmp = P->next;free(P);P = Tmp;}}//生成空链表 List MakeEmpty(List L) {L = (struct node*)malloc(sizeof(struct node));L->data = 0;L->next = NULL;return L;}//返回头结点 Position Header(List L) {return L;}//返回第一个数据节点的位置 Position First(List L) {if (L->next != NULL)return L->next;}//获得位置P后继节点位置 Position Advance(Position P) {if (P != NULL)return P->next;}//返回P位置的数据 ElemType Retrieve(Position P) {if (P != NULL)return P->data;}
阅读全文
0 0
- 单链表的函数声明和定义
- 函数的声明和定义
- 函数的声明和定义
- 函数的声明和定义
- 函数的声明和定义
- 函数的声明和定义
- 函数的声明和定义
- 变量和函数的定义和声明
- 函数声明和定义
- 全局函数的声明和定义
- 内联函数的声明和定义
- 内联函数的声明和定义位置
- 变量,函数的定义和声明
- c_函数的声明和定义
- 内联函数的声明和定义
- 函数的定义、声明、初始化和赋值
- 函数的声明、定义和调用
- 【转】函数的声明和定义
- Solidity学习(3)
- 九周第四次课 2017.12.14 LAMP架构介绍、MySQL、MariaDB介绍、MySQL安装
- MySQL简单语法(图片)
- 获取bash文件所在的目录绝对路径以及获取函数方法名称
- 分析微信数据及可视化
- 单链表的函数声明和定义
- shiro与spring的整合使用
- 第8周 【项目5
- Java益智小代码—递归和循环分析
- 2. Add Two Numbers
- global \ nonlocal 和 闭包
- 学习日志2017.12.14
- div盒子基础结构
- Hexo博客主题pure修改记录