• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
狂奔的蜜蜂
博客园    首页    新随笔    联系   管理    订阅  订阅

List & LinkList

  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #include <stdlib.h>
  4 
  5 enum { OK = 1, OVERFLOW = -1, ERROR = -2, LIST_INIT_SIZE = 100, LISTINCREMENT = 10};
  6 
  7 typedef int ElemType; typedef  int Status;
  8 #define SqlPtr(x) ((SqList*)*x) 
  9 #define Sql(x) ((SqList*)x)
 10 #define SllPtr(x) ((LinkList*)*x)
 11 #define Sll(x) ((LinkList*)x)
 12 
 13 typedef  struct{
 14     ElemType  *elem;
 15     int  length, listsize;
 16 }SqList;
 17 
 18 typedef struct Node {
 19     ElemType data;
 20     struct Node *next;
 21 }Node;
 22 
 23 typedef struct {
 24     Node* head;
 25     int len;
 26 }LinkList;
 27 
 28 typedef struct {
 29     Status (*initList)(void **L);
 30     Status (*listInsert)(void *L, int i, ElemType e);
 31     Status (*listDelete)(void *L, int i, int *e);
 32     Status (*listGetElem)(void *L, int i, int *elem);
 33     Status (*listPrint)(void *L);
 34     int (*listGetLength)(void *L);
 35 }ListADT;
 36 
 37 Status InitList_link(void **L) {
 38     (*L) = malloc(sizeof(LinkList));
 39     SllPtr(L)->head = (Node*)malloc(sizeof(Node));
 40     SllPtr(L)->head->next = NULL;
 41     SllPtr(L)->len = 0;
 42 
 43     return OK; 
 44 }
 45 
 46 Status ListInsert_link(void *L, int i, ElemType e) {
 47     Node *p = Sll(L)->head, *tmp;
 48     int j = 0;
 49 
 50     while (p&&j<i-1) {
 51         p = p->next;
 52         ++j;
 53     }
 54 
 55     if (!p||j>i-1)
 56         return ERROR;
 57 
 58     tmp = (Node*)malloc(sizeof(Node));
 59     tmp->data = e;
 60     tmp->next = p->next;
 61     p->next = tmp;
 62 
 63     Sll(L)->len++;
 64 
 65     return OK;
 66 }
 67 
 68 Status ListDelete_link(void *L, int i, int *e) {
 69     Node* p = Sll(L)->head;
 70     Node* tmp;
 71     int j = 0;
 72 
 73     while (p->next&&j<i-1) {
 74         p = p->next;
 75         ++j;
 76     }
 77 
 78     if (!(p->next) || j>i-1)
 79         return ERROR;
 80     
 81     tmp = p->next;
 82     p->next= tmp->next;
 83     *e = tmp->data;
 84     free(tmp);
 85 
 86     Sll(L)->len--;
 87 
 88     return OK;
 89 }
 90 
 91 ElemType ListGetElem_link(void *L, int i, ElemType *elem) {
 92     Node* p = Sll(L)->head;
 93     int j = 0;
 94 
 95     while (p&&j<i) {
 96         p = p->next;
 97         ++j;
 98     }
 99 
100     if (!p || j>i)
101         return ERROR;
102     
103     if( elem != NULL )
104         *elem = p->data;
105 
106     return p->data;
107 }
108 
109 Status ListPrint_link(void *L) {
110     int n = 0;
111     for( n = 1; n < Sll(L)->len + 1; n++ ) {
112         printf("%d, ", ListGetElem_link(L, n, NULL));
113     }
114     printf("\n");
115 
116     return OK;
117 }
118 
119 int ListGetLength_link(void *L) {
120     return Sll(L)->len;
121 }
122 
123 Status InitList_sq(void **L) {
124     *L = malloc(sizeof(SqList));
125     SqlPtr(L)->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
126     if(!(SqlPtr(L)->elem)) exit(OVERFLOW);
127     SqlPtr(L)->length = 0;
128     SqlPtr(L)->listsize = LIST_INIT_SIZE;
129     return OK;
130 }
131 
132 Status ListInsert_sq(void *L, int i, ElemType e) {
133     ElemType *newbase, *p, *q;
134     if (i < 1 || i > Sql(L)->length + 1) return ERROR;
135 
136     if (Sql(L)->length >= Sql(L)->listsize) {
137         newbase = (ElemType*)realloc(Sql(L)->elem, (Sql(L)->listsize + LISTINCREMENT) * sizeof(ElemType));
138         if (newbase == NULL) exit(OVERFLOW);
139         Sql(L)->elem = newbase;
140         Sql(L)->listsize += LISTINCREMENT;
141     }
142 
143     q = &(Sql(L)->elem[i-1]);
144     p = NULL;
145 
146     for( p = &(Sql(L)->elem[Sql(L)->length - 1]); p >= q; --p ) {
147         *(p+1) = *p;
148     }
149     *q = e;
150     Sql(L)->length++;
151     return OK;
152 }
153 
154 Status ListDelete_sq(void *L, int i, int *e) {
155     ElemType *p, *end;
156 
157     if (i < 1 || i > Sql(L)->length + 1)
158         exit(ERROR);
159 
160     end = &(Sql(L)->elem[Sql(L)->length - 1]);
161 
162     if (e != NULL)
163         *e = Sql(L)->elem[i-1];
164 
165     for( p = &(Sql(L)->elem[i-1]); p <= end; p++ ) {
166         *p = *(p+1);
167     }
168     Sql(L)->length--;
169 
170     return OK;
171 }
172 
173 ElemType ListGetElem_sq(void *L, int i, int *elem) {
174     if (i < 1 || i > Sql(L)->length + 1) {
175         exit(ERROR);
176     }
177 
178     if (elem != NULL)
179         *elem = Sql(L)->elem[i - 1];
180     return Sql(L)->elem[i - 1];
181 }
182 
183 Status ListPrint_sq(void *L) {
184     int n = 0;
185     for (n = 1; n < Sql(L)->length + 1; n++) {
186         printf("%d, ", ListGetElem_sq(L, n, NULL));
187     }
188     printf("\n");
189     return OK;
190 }
191 
192 int ListGetLength_sq(void *L) {
193     return Sql(L)->length;
194 }
195 
196 void MergeList(void *La, void *Lb, void* ret, ListADT adt) {
197     int nIndexA = 1, nIndexB = 1, nIndexC = 1;
198     int nElemA = 0, nElemB = 0;
199 
200     while (nIndexA <= adt.listGetLength(La) && nIndexB <= adt.listGetLength(Lb)) {
201         adt.listGetElem(La, nIndexA, &nElemA);
202         adt.listGetElem(Lb, nIndexB, &nElemB);
203 
204         if (nElemA <= nElemB) {
205             adt.listInsert(ret, nIndexC, nElemA); nIndexA++;
206         }else{
207             adt.listInsert(ret, nIndexC, nElemB); nIndexB++;
208         }
209         nIndexC++;
210     }
211 
212     while (nIndexA <= adt.listGetLength(La)) {
213         adt.listGetElem(La, nIndexA++, &nElemA);
214         adt.listInsert(ret, nIndexC++, nElemA);
215     }
216 
217     while (nIndexB <= adt.listGetLength(Lb)) {
218         adt.listGetElem(Lb, nIndexB++, &nElemB);
219         adt.listInsert(ret, nIndexC++, nElemB);
220     }
221 
222     return;
223 }
224 
225 void Test1_Simple(ListADT adt) {
226     int n = 0, nInput = 0, e;
227     void *L;
228     printf("Initlist\n");
229     adt.initList(&L);
230 
231     for (n = 0; n < 10; n++)
232         adt.listInsert(L, n, n);
233     printf("CurrentList:\n"); adt.listPrint(L);
234 
235     adt.listInsert(L, 5, 6);
236     printf("After insert:\n"); adt.listPrint(L);
237 
238     adt.listDelete(L, 2, &e);
239     printf("After delete:\n"); adt.listPrint(L);
240 
241     getchar();
242 }
243 
244 void Test2_Simple(ListADT adt) {
245     int n = 0, i = 0, k = 0;
246     void* L[3];
247     for (n = 0; n < 3; n++)
248         adt.initList(&L[n]);
249 
250     for (n = 0; n < 10; n++) {
251         for (i=0; i < 2; i++) {
252             adt.listInsert(L[i], n + 1, k++);
253         }
254     }
255 
256     MergeList(L[0], L[1], L[2], adt);
257     for (n = 0; n < 3; n++) {
258         adt.listPrint(L[n]);
259     }
260 
261     getchar();
262 }
263 
264 int main() {
265     ListADT adt;
266     printf("Sequence list: \n");
267     adt.initList = InitList_sq;
268     adt.listDelete = ListDelete_sq;
269     adt.listGetElem = ListGetElem_sq;
270     adt.listGetLength = ListGetLength_sq;
271     adt.listInsert = ListInsert_sq;
272     adt.listPrint = ListPrint_sq;
273     Test1_Simple(adt);
274     Test2_Simple(adt);
275 
276     printf("Link list: \n");
277     adt.initList = InitList_link;
278     adt.listDelete = ListDelete_link;
279     adt.listGetElem = ListGetElem_link;
280     adt.listGetLength = ListGetLength_link;
281     adt.listInsert = ListInsert_link;
282     adt.listPrint = ListPrint_link;
283     Test1_Simple(adt);
284     Test2_Simple(adt);
285 
286     return 0;
287 }

 

posted @ 2013-10-23 08:21  狂奔的蜜蜂  阅读(238)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3