线性表的链式存储结构基本操作代码实现
线性表的链式存储结构基本操作代码实现
一、创建单链表:从表尾到表头逆向创建
bool CreateList_L(LinkList L,int n) {printf("------创建链表开始------n");LinkList p;for (int i=n;i>0;i--){printf("输入第%d个结点数据n",i);p=(LinkList)malloc(sizeof(LNode));scanf_s("%d",&p->data);//将p插入到表头p->next=L->next;L->next=p;}printf("------创建链表完成------n");return true; }
12345678910111213141516二、打印链表数据
bool PrintList(LinkList L) {printf("------打印链表数据开始------n");LNode *p=L->next;while (p){printf("%d ",p->data);p=p->next;}printf("n------打印完成!------n");return true; } 123456789101112
三、查找单链表结点
bool GetElem_L(LinkList L,int pos,ElemType *e) {LinkList p=L->next;//L为头指针,p指向头结点int j=1;//计数器while (p&&j<pos)//寻找p指向第pos个结点或者为空{p=p->next;j++;}if (!p||j>pos)//pos大于表长或者pos小于1{return false;}*e=p->data;return true; }
1234567891011121314151617四、插入结点
bool ListInsert(LinkList L,int pos,ElemType e) {printf("------插入链表结点开始------n");LinkList p=L;//p指向头节点int j=0;//计数器while (p&&j<pos-1)//寻找p指向第pos-1个结点{p=p->next;j++;}if (!p||j>pos-1){return false;}LinkList s=(LinkList)malloc(sizeof(LNode));s->data=e;s->next=p->next;p->next=s;printf("------插入链表结点数据%d完成------n",e);return true; }
123456789101112131415161718192021五、删除结点
bool ListDelete(LinkList L,int pos,ElemType *e) {printf("------删除链表结点开始------n");LinkList p=L;int j=0;while (p->next&&j<pos-1)//寻找p指向第pos-1个结点{p=p->next;j++;}if (!p->next||j>pos-1){return false;}LinkList q=p->next;p->next=q->next;*e=q->data;free(q);printf("------删除链表结点数据%d完成------n",*e);return true; }
123456789101112131415161718192021六、获取表长
int ListLength(LinkList L) {LinkList p=L->next;int len=0;while (p){len++;p=p->next;}return len; } 1234567891011
七、判断链表中是否有某数据
bool IsListData(LinkList L,ElemType dt) {LinkList p=L->next;for (int i = 0; i < ListLength(L); i++){if (dt==p->data)return true;p=p->next;}return false; } 1234567891011
六、程序运行
#include "stdafx.h" #include "malloc.h" #include "stdio.h" #include "stdlib.h" int _tmain(int argc, _TCHAR* argv[]) {printf("输入创建链接结点个数n");int n;scanf_s("%d",&n);LinkList L;L=(LinkList)malloc(sizeof(LNode));L->next=NULL;//建立带头结点的单链表CreateList_L(L,n);PrintList(L);ListInsert(L,4,4);PrintList(L);ElemType *e=(ElemType*)malloc(sizeof(ElemType));ListDelete(L,4,e);PrintList(L);GetElem_L(L,1,e);printf("链表第1个结点数据为%dn",*e);system("pause");return 0; }
123456789101112131415161718192021222324252627282930相关知识
python二级选择题与分析(8)
python画栀子花代码
给对象的小惊喜c#玫瑰花
字符串通常采用的两种存储方式是( )。
从0实现 BT 下载 :1 种子的解析
用vsC语言代码写:首先将顺序栈存储结构定义放在一个头文件:如取名为SqStackDef.h。2.将顺序栈的基本操作算法也集中放在一个文件之中,如取名为SqStackAlgo.h。3.将函数的测试和主函数组合成一个文件,如取名为SqStackUse.cpp。
卷积神经网络实现鸢尾花数据分类python代码实现
写一个C语言代码测量土壤湿度
python程序员实现表白代码的案例
Android数据结构与算法之一 基础简介
网址: 线性表的链式存储结构基本操作代码实现 https://www.huajiangbk.com/newsview1095230.html
上一篇: c# string类型存储原理 |
下一篇: mybatis存储过程及mode |
推荐分享

- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039