数据结构顺序链表
来源:互联网 发布:yaf 数据库 yii 编辑:程序博客网 时间:2024/05/29 14:18
数据结构顺序链表的基本操作,创建、插入、查询等。
/*main.c*/#include <stdio.h>#include "linklist.h"int main(void){/* linklist.c */#include <stdio.h>#include <stdlib.h>#include "linklist.h"linklist CreateList(void) //创建空表{ linklist head = (linklist)malloc(sizeof(Lnode)); if(head==NULL) { printf("malloc failed\n"); return NULL; } head->data = -1; head->next = NULL; return head;}int GetListLenth(linklist L) //求链表长度{ int i=0; linklist h = L->next; while(h!=NULL) { i++; h=h->next; } return i;}void ShowList(linklist L) //输出链表{ linklist h = L->next; while(h!=NULL) { printf("%d,",h->data); h=h->next; } printf("\n");}int DelList(linklist L,int pos) // 删除{ linklist p = L; int len=GetListLenth(L); //1.判空 if(p->next==NULL) { printf("list is empty\n"); return ERROR; } //2.位置合法性 if(pos<0||pos>len-1) { printf("pos error\n"); return ERROR; } //3.找删除位置 while(pos--) { p=p->next; } //4.删除算法 linklist d = p->next; p->next = d->next; free(d); return OK;}int InsertList(linklist L,int pos,int val) //插入{ linklist p = L; int len= GetListLenth(L); //1.判读位置合法性 if(pos<0||pos>len) { printf("pos error\n"); return ERROR; } //2.移动到插入点 int i; for(i=0;i<pos;i++) { p=p->next; } /* while(pos--) { p=p->next; } */ //3.构建一个节点 linklist s = malloc(sizeof(Lnode)); s->data = val; s->next = p->next; p->next = s; return OK;}int ChangeList(linklist L,int pos,int val) //修改{ linklist p = L->next; int len = GetListLenth(L); //1.位置合法性 if(pos<0||pos>len-1) { printf("pos error\n"); return ERROR; } //2.移动到指定位置 int i; for(i=0;i<pos;i++) { p=p->next; } p->data = val; return OK; }int SearchList(linklist L,int pos) //查询指定位置的值{ linklist p = L->next; int len = GetListLenth(L); //1.位置合法性 if(pos<0||pos>len-1) { printf("pos error\n"); return ERROR; } //2.移动到指定位置 int i; for(i=0;i<pos;i++) { p=p->next; } printf("find %d\n",p->data); return p->data;}int ListIsEmpty(linklist L) // 判空{ int len = GetListLenth(L); if(len==0) { return TRUE; } else { return FALSE; }}int ClearList(linklist L) // 清空{ while(!ListIsEmpty(L)) { DelList(L,0); } return 0;}int ReverseList(linklist L) // 反转{ linklist p = L->next; //记录第一数据节点位置 L->next = NULL; //将头节点与数据节点断开 while(p!=NULL) { //遍历数据节点,并插入到L链表的头部 linklist d = p; p=p->next; d->next = L->next; L->next = d; }}void DestroyList(linklist L){ while(L != NULL) { L = L -> next; free(L); }}/* linklist.h */#ifndef _LINKLIST_H#define _LINKLIST_H#define TRUE 1#define FALSE 0#define OK 1#define ERROR -1typedef int data_t;struct node{ data_t data; struct node * next;};typedef struct node Lnode;typedef struct node * linklist;linklist CreateList(void); //创建空表data_t InsertList(linklist L,int pos,int val); //插入int DelList(linklist L,int pos); //删除data_t ChangeList(linklist L,int pos,int val); //修改int SearchList(linklist L,int pos); //查询指定位置的值int GetListLenth(linklist L); //求链表长度void ShowList(linklist L); //输出链表int ListIsEmpty(linklist L); //判空int ClearList(linklist L); //清空链表int ReverseList(linklist L); //链表的翻转void DestroyList(linklist L); //销毁#endif
linklist L = CreateList(); //创建一个空表 if(L==NULL){ printf("CreateList failed\n"); return ERROR; } InsertList(L,0,11); InsertList(L,1,22); InsertList(L,2,33); printf("len:%d\n",GetListLenth(L)); ReverseList(L); //DestroyList(L); // 销毁链表 ShowList(L);}
阅读全文
0 0
- 数据结构《顺序链表》
- 数据结构顺序链表
- 数据结构_1.顺序链表
- 数据结构之顺序链表
- 数据结构之顺序链表
- 数据结构之顺序表和链表
- 数据结构顺序表及链表实验
- 【数据结构】C语言实现顺序链表
- 20150130 【 ARM 】 数据结构+顺序链表+GDB
- 【数据结构】顺序队列(链表实现)
- 数据结构之 顺序表
- 数据结构--顺序线性表
- 数据结构---顺序表
- 数据结构之顺序表
- 数据结构顺序表
- 数据结构顺序表
- 顺序表(数据结构)
- 数据结构之顺序表
- 百度文库爬虫之ppt爬虫
- 洛谷P2299Mzc和体委的争夺战
- 驾驶证、行驶证、身份证识别技术
- OpenCV3.3.0 + CLion + CMake 配置(Mac巨细无敌版)
- OpenCV2编程手册笔记之 6.2使用低通滤波器
- 数据结构顺序链表
- neural networks and deep learning 学习笔记
- 【unity学习笔记】unity3d- 使用对象池高效管理内存
- 欢迎大学新生关注实无穷小微积分!
- 【待复习】PAT甲级1042. Shuffling Machine (20)
- codeforces 519C A and B and Team Training
- PHP源码加密方法实例
- Mybatis:Parameter 'list' not found. Available parameters are [termIds, eventIds, param1, param2]
- 每日一题--数组中的逆序对