链表是一种基本的数据结构,属于线性表的一种,本文将结合代码简单介绍链表的相关基础知识点。
认识链表
上一篇实现了线性表的顺序存储,本篇将实现线性表的链式存储,即链表(Linklist)的实现。链式存储的特点是用一组任意的存储单元存储线性表的数据元素。因此,为了表示每个数据元素与其直接后继元素之间的逻辑关系,对该数据元素来说,除存储本身的信息之外,还需要存储一个指示其直接后继的信息(即直接后继的存储位置)。
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
| #include <stdio.h> #include <stdlib.h>
#define OK 1 #define ERROR 0
typedef int Status; typedef int ElemType;
typedef struct LNode { ElemType data; struct LNode *next; }LNode,*LinkList;
Status InitList(LinkList *L) { *L = (LNode *)malloc(sizeof(LNode)); if(!(*L)) return ERROR; (*L)->next = NULL; return OK; }
Status HeadInsertCreate(LinkList L,int n) { ElemType e; while(n--) { scanf("%d",&e); LNode *p; p = (LNode *)malloc(sizeof(LNode)); p->data = e; p->next = L->next; L->next = p; } return OK; }
Status TailInsertCreate(LinkList L,int n) { LinkList tail; tail = L; ElemType e; while(n--) { scanf("%d",&e); LNode *p; p = (LNode *)malloc(sizeof(LNode)); p->data = e; p->next = NULL; tail->next = p; tail = p; } return OK; }
Status GetElem(LinkList L,int pos,ElemType* e) { LNode *p; p = L->next; int i = 1; while(p && i < pos) { p = p->next; i++; } if(!p || i > pos) return ERROR; (*e) = p->data; return OK; }
Status GetPos(LinkList L,ElemType e,int* pos) { LNode *p; int i = 1; p = L->next; while(p) { if(p->data == e) { (*pos) = i; return OK; } p = p->next; i++; } return ERROR; }
Status Insert(LinkList L,int pos,ElemType e) { LNode *pre; pre = L; int i = 0; while(pre && i < pos-1) { pre = pre->next; i++; } if(!pre || i > pos-1) return ERROR; LNode *p; p = (LNode *)malloc(sizeof(LNode)); p->data = e; p->next = pre->next; pre->next = p; return OK; }
Status DeleteAccordToPos(LinkList L,int pos,ElemType* e) { LNode *pre; pre = L; int i = 0; while(pre->next && i < pos-1) { pre = pre->next; i++; } if(!pre->next || i > pos-1) return ERROR; LNode *p; p = pre->next; pre->next = p->next; (*e) = p->data; free(p); return OK; }
Status DeleteAccordToElem(LinkList L,ElemType e,int* pos) { LNode *pre,*p; int i = 1; pre = L; p = L->next; while(p) { if(p->data == e) { pre->next = p->next; (*pos) = i; free(p); return OK; } pre = p; p = p->next; i++; } return ERROR; }
int Length(LinkList L) { LNode *p; int length = 0; p = L; while(p->next) { length++; p = p->next; } if(!p) length = 0; return length; }
Status Destroy(LinkList L) { LNode *p; p = L; while(p) { LNode *tmp; tmp = p; p = p->next; free(tmp); } return OK; }
Status Inverse(LinkList L) { LNode *p,*q,*tmp; p = L->next; q = p->next; tmp = NULL; while(q) { tmp = q->next; q->next = p; p = q; q = tmp; } L->next->next = NULL; L->next = p; return OK; }
Status Traverse(LinkList L) { LNode *p; p = L->next; while(p) { printf("%d ",p->data); p = p->next; } printf("\n"); return OK; }
|
完整代码可参见:GitHub
DONE!
所示代码如有错误请多加指正,谢谢