c语言链式存储实现
来源:互联网 发布:mac隐藏windows分区 编辑:程序博客网 时间:2024/05/19 03:21
之前写了一个关于线性表的顺序存储,大家都知道,在链表操作中,为了方便的删除,增加元素而无须大量地移动元素,通常来说,链表都是链式存储,今天我就写一个这个关于链表的链式存储的一些相关操作。包括创建链表,头插和尾插法创建链表,求链表的长度,插入元素和删除元素,还有就是下查找固定位置的链表并返回其元素的值。
#include<stdio.h>#include<malloc.h>#define ERROR 0#define OK 1typedef int ElemType;typedef int Status;/*******结点定义*/typedef struct Node{ ElemType data; struct Node *next;} Node,*pNode;/********创建带头结点的空链表*/pNode CreateList(){ pNode L; L=(pNode)malloc(sizeof(Node)); L->next=NULL; return L;}/************头插法*/void headInsertList(pNode L,int e){ pNode p=(pNode)malloc(sizeof(Node)); p->data=e; p->next=L->next; L->next=p;}/**********链表的输出*/void printList(pNode L){ pNode p=L->next; while(p) { printf("%d ",p->data); p=p->next; } printf("\n");}/************链表元素的个数*/int lengthList(pNode L){ int length=0; pNode p=L->next; while(p) { ++length; p=p->next; } return length;}/********链表的尾插法*/void tailInsertList(pNode L,ElemType e){ pNode p=L->next; pNode q=(pNode)malloc(sizeof(Node)); q->data=e; while(p->next) { p=p->next; } q->next=NULL; p->next=q;}/*******在链表的第i个位置插入元素e*/Status InsertList(pNode L,int pos,ElemType e){ pNode p=L; pNode s;//待插入结点 int j=1; while(p && j<pos) { p=p->next; ++j; } if(!p || j>pos) return ERROR; s=(pNode)malloc(sizeof(Node)); s->data=e; s->next=p->next; p->next=s; return OK;}/**********删除链表第i个位置的元素并返回该元素*/Status deleteList(pNode L,int pos,ElemType *e){ pNode p=L; pNode q; int j=1; while(p->next && j<pos) { p=p->next; ++j; } if(!(p->next) || j>pos) return ERROR; q=p->next; *e=q->data; p->next=q->next; free(q); return OK;}/**********获得第 i 个位置的元素*/Status GetElem(pNode L,int i,ElemType *e){ pNode p; int j=1; p=L->next;//p指向第一个元素 while(p && j<i) { p=p->next; ++j; } if(!p || j>i) { return ERROR; } *e=p->data; return OK;}int main(){ /* pNode L; int i=1; int m;//获得某个位置元素的值 int length;//保存链表长度 int n;//保存删除后结点的值 L=CreateList(); headInsertList(L,i); tailInsertList(L,2); headInsertList(L,3); // length=lengthList(L); printList(L); InsertList(L,4,4); printList(L); deleteList(L,4,&n); printf("%d\n",n); printList(L); GetElem(L,2,&m); printf("%d\n",m); // printf("%d\n",length);*/ int i; pNode L; printf("创建链表请输入1:\n增加链表元素请输入2:\n删除元素请输入3:\n查找元素请输入4\n输出链表请按5\n输出链表长度请输入6:\n结束请输入0:\n"); while(1) { scanf("%d",&i); switch(i) { case 0: return 0; break; case 1: { int n,i; ElemType e; printf("请输入创建链表元素的个数:\n"); scanf("%d",&n); if(n<=0) { return -1; } L=CreateList(); for(i=0; i<n; i++) { printf("请输入第%d个要插入的数字:",(i+1)); scanf("%d",&e); headInsertList(L,e); } } break; case 2: { int pos; ElemType e; printf("请输入要插入的位置:\n"); scanf("%d",&pos); printf("请输入要插入的元素:\n"); scanf("%d",&e); InsertList(L,pos,e); } break; case 3: { int pos; ElemType e; printf("请输入要删除元素的位置:\n"); scanf("%d",&pos); deleteList(L,pos,&e); } break; case 4: { int pos; int r;//接收状态返回值 ElemType e; printf("请输入要查找元素的位置:\n"); scanf("%d",&pos); r=GetElem(L,pos,&e); printf("%d\n",e); } break; case 5: printList(L); break; case 6: printf("%d\n",lengthList(L)); break; default: printf("输入数据有误,请重新输入!\n"); break; } } return 0;<p>}</p><p><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">上面的代码已亲自测试过,如有哪不妥的地方,请大神指出。共同学习,一起进步。</span></p>
0 0
- c语言链式存储实现
- 栈的链式存储实现c语言
- 队列的链式存储实现c语言
- 线性表的链式存储(单链表)C语言实现
- 循环队列-链式存储结构-c语言实现
- 用C语言实现栈的链式存储
- 线性表-链式存储结构(C语言实现)
- C语言实现线性表的链式存储结构
- C语言实现一般线性表的链式存储
- 线性表的链式存储结构(C语言实现)
- 线性表的链式存储实现c语言
- C语言实现链式队列
- C语言实现链式栈
- C语言实现链式队列
- c语言实现链式队列
- C语言栈的链式存储结构
- 栈-链式存储结构-c语言
- 二叉树链式存储的C实现
- 通过windows自带的命令查看笔记本电脑的电池状态
- ZigBee实现基础组网成功返回LED 字符 P1_1绿闪烁
- 优化算法(模拟退火算法 与 遗传算法)
- AfxGetApp的仔细讲解
- F - 24 Game-构造水题
- c语言链式存储实现
- Leetcode----205Isomorphic Strings
- [80]Remove Duplicates from Sorted Array II
- 第一个包含类的C++程序
- Android 微信授权登录、获取Wx用户信息,解决无法回调问题
- Swift中推送通知设置:Binary operator '|' cannot be applied to two UIUserNotificationType operands 错误解决
- android开发工具类之获得WIFI IP地址或者手机网络IP
- LeetCode Palindrome Number
- HDU 2844 Coins 多重背包