数据结构 C语言实现 线性表的链式实现
2016-06-20 17:07
267 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/study_sky/article/details/51722050
本节定义的两个类型LNodeType和LinkListType.
LNodeType 为结点,LinkListType为指向LNodeType的指针。
难点在于L_CreatList的参数Lhead为返回数据,类型为指向LNodeType指针的指针(双重指针)。
#include <stdio.h> #include <stdlib.h> #include "define.h" typedef int ElemType; typedef struct LNode LNodeType; typedef struct LNode *LinkListType; struct LNode { ElemType data; struct LNode *next; }; Status L_GetElem(LinkListType Lhead, int i, ElemType *e) { LNodeType *p; int j = 1; p = Lhead -> next; while ( p && j < i ){ p = p->next; ++j; } if( !p || j > i ) return ERROR; *e = p->data; return OK; } Status L_ListInsert(LinkListType Lhead, int i, ElemType e) { LinkListType p; int j = 0; p = Lhead; while ( p && j < i - 1 ) { p = p->next; ++j; } if ( !p || j > i ) return ERROR; LinkListType s; s = (LinkListType ) malloc ( sizeof(LNodeType)); s->data = e; s->next = p->next; p->next = s; return OK; } Status L_ListDelete(LinkListType L, int i, ElemType *e) { LinkListType p, q; int j; p = L; j = 0; while(p->next && j < i-1 ) { p = p->next; ++j; } if ( !(p->next) || j > i-1) return ERROR; q = p -> next; p->next = q->next; *e = q->data; free(q); return OK; } /* * 逆序创建n个数据的链表 */ void L_CreateList( LinkListType *Lhead, int n) { LinkListType p, L; int i; L = (LinkListType)malloc(sizeof(LNodeType)); L->next = NULL; for ( i = n; i > 0; --i){ p = (LinkListType) malloc(sizeof(LNodeType)); scanf("%d",&p->data);//输入数据的类型限制为整形了。 p->next = L->next; L->next = p; } *Lhead = L; } void L_MergeList(LinkListType La, LinkListType Lb, LinkListType *Lc) { LinkListType pa,pb,pc; pa = La->next; pb = Lb->next; *Lc = pc = La; while (pa && pb) { if (pa->data <= pb->data){ pc->next = pa; pc = pa; pa = pa->next; }else{ pc->next = pb; pc = pb; pb = pb->next; } } pc->next = pa ? pa : pb; free(Lb); } /* * 以下为随手测试一下 */ void printList( LinkListType L ){ if (L->next == NULL){ printf("data is empty"); } L = L->next; while (L !=NULL){ printf("%d\n", L->data); L = L->next; } } int main(void) { LinkListType L; L_CreateList(&L, 2); printf("\n-printList-----\n"); printList(L); printf("\n--GetElem 1 ----\n"); ElemType e; L_GetElem(L, 1, &e); printf("%d\n",e); L_ListInsert(L, 2, 99); printf("\n-ListInsert- 2----\n"); printList(L); printf("\n---ListDelete 1 ---\n"); L_ListDelete(L,1,&e); printf("e = %d\n", e); printList(L); return 0; }
相关文章推荐
- 线性表——链式结构(c语言实现)
- C语言数据结构之用线性顺序存储结构实现栈
- 数据结构--线性表(顺序实现,链式实现,多项式计算)
- C语言单链表(线性表的链式存储)的实现
- 严蔚敏版《数据结构》第二章线性表的算法C语言实现
- 数据结构:栈的链式实现(C语言描述)
- C语言数据结构-2.线性表之链式存储结构
- 数据结构------线性表的链式表示与实现
- 数据结构学习----线性表的链式表示之升序排序的单链表(Java实现)
- (C语言)队列的链式实现(数据结构十一)
- 数据结构实现链式队列(C语言)
- 数据结构学习----线性表的链式表示之循环单链表(Java实现)
- 线性表的链式表示和实现(数据结构)
- 数据结构学习笔记-栈的链式存储(C语言实现)
- 数据结构复习——线性表的链式存储实现(双向链表)
- (C语言)队列的链式实现(数据结构十一)
- C语言 双向循环链表的实现和结构 链表 线性表的链式实现
- 数据结构学习---线性表的链式存储实现(双向链表)
- JAVA数据结构 线性表的链式存储及其实现
- 数据结构-C语言 单链线性表实现归并 C=A∪B