数据结构之链式线性表
来源:互联网 发布:python 3.5.1 编辑:程序博客网 时间:2024/05/16 04:06
带头结点的
/*本链表含有头结点,结点从1开始。2017.9.27 by 横济沧海*/#include<stdio.h>#include<string.h>#include<string>#include<cmath>#include<algorithm>#include<iostream>using namespace std;#define OK 1#define ERROR 0typedef int status;typedef struct LNODE{ int data; struct LNODE *next;} LNode,*Linklist;status initLinset(Linklist &head)//初始化{ head=(Linklist)malloc(sizeof(LNODE)); if(!head)return ERROR; head->next=NULL; return OK;}status clearList(Linklist &head)//将整表进行清空{ Linklist p,q; p=head->next; while(p) { q=p->next; free(p); p=q; } head->next=NULL; return OK;}status DestroyList(Linklist &head)//将整表删除掉{ Linklist p,q; p=head->next; while(p) { q=p->next; free(p); p=q; } free(head); head=NULL;//free指针记得赋为NULL return OK;}status insertList(Linklist &L,int i,int e)//在i前插入e{ int j=0,k; Linklist p,q; p=L; if(j>i-1)return ERROR; while(p&&j<i-1) { //这里要好好理解,i=0时,p指向头结点,当i=1时,p指向首结点 // // j++; p=p->next; } if(p!=NULL)//如果p为空那么就没有找到i-1这个节点,就说明i的值不合法 { q=(Linklist)malloc(sizeof(LNODE)); if(q!=NULL) { q->data=e; q->next=p->next; p->next=q; return OK; } else return ERROR; } else return ERROR;}status deleteList(Linklist &L,int i)//删除I节点{ int j=0,k; Linklist p,q; p=L; if(j>i-1)return ERROR; while(p&&j<i-1) { j++; p=p->next; } if(p!=NULL) { q=p->next; p->next=q->next; free(q); return OK; } else return ERROR;}status updataList(Linklist &L,int i,int e)//将i处的值更新为e{ int j=0,k; Linklist p,q; p=L; if(j>i-1)return ERROR; while(p&&j<i) { j++; p=p->next; } if(p!=NULL) { p->data=e; return OK; } else return ERROR;}int getElem(Linklist L,int i)//得到i处的值{ int j=0,k; Linklist p,q; p=L; if(j>i-1)return ERROR; while(p&&j<i) { j++; p=p->next; } if(!p)return -1; else return p->data;}int query(Linklist L,int e)//查询是否有e这个元素{ int j=0,k; Linklist p,q; p=L->next; while(p) { j++; if(p->data==e)return j; else p=p->next; } return -1;}void traverse(Linklist L)//链表的遍历{ Linklist p,q; p=L->next; while(p) { printf("%d ",p->data); p=p->next; } printf("\n");}int getlength(Linklist L)//q取得长度{ Linklist p,q; int j=0; p=L->next; while(p) { j++; p=p->next; } return j;}int main(){ int i,j,k; Linklist head; initLinset(head); for(i=1; i<=10; i++) insertList(head,i,i); traverse(head); insertList(head,2,100); traverse(head); insertList(head,1,99); traverse(head); insertList(head,5,98); traverse(head); deleteList(head,1); traverse(head); deleteList(head,9); traverse(head); updataList(head,4,55); updataList(head,1,22); traverse(head); printf("%d %d %d\n",getElem(head,3),getlength(head),query(head,33)); clearList(head); printf("%d\n",getlength(head)); DestroyList(head); if(head==NULL)printf("This List has been destroyed!"); return 0;}
阅读全文
0 0
- 数据结构之链式线性表
- 常用数据结构之linklist链式线性表
- 数据结构之线性表的链式存储
- 数据结构:线性表之链式存储结构
- 【数据结构】线性表之链式存储结构
- java 数据结构之 链式线性表
- 数据结构:线性表之链式存储结构
- 数据结构(4)之线性表之单链式存储
- 数据结构:线性表之链式表实战演练
- 进击的数据结构二之线性表的链式结构
- 数据结构笔记之线性表的链式存储结构
- C语言数据结构-2.线性表之链式存储结构
- Python 数据结构 之 线性表 的链式存储结构
- 数据结构学习笔记(二)------------线性表之链式存储
- 数据结构(4)线性表之链式表示
- 数据结构之线性表的链式表示及其实现
- 数据结构之线性表的链式表示和实现
- 数据结构之线性表(C++) -- 链式描述
- HDU 5573 Binary Tree 构造(二进制)
- 在ArcGIS中如何设置图层是否可选择?
- 一致性hash算法释义
- XMLHttpRequest的用法
- Ubuntu系统启动报错:The system is running in low-graphics mode
- 数据结构之链式线性表
- 第三天
- HTML-后台登录模版02
- 分布式事务方案:TCC方案
- 设置<a>超链接不可用
- docker配置proxy
- 计算机网络: IP地址,子网掩码,网段表示法,默认网关,DNS服务器详解 楔子: 以Windows系统中IP地址设置界面为参考(如图1), IP地址, 子网掩码, 默认网关 和 DNS服务器,
- LeetCode-56-Merge Intervals Python自定义sort,贪心
- S7-300哪些24V数字量输入模块需要电源,与电源如何连接