大话数据结构笔记, 线性表链式存储,元素的插入,获取,表的初始化,删除,销毁
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 #define ERROR 0 #define OK 1 typedef int ElemType;typedef struct Node { ElemType data; Node *next; } Node; typedef struct Node *LinkList;typedef int Status;LinkList InitLinkList (int size) { LinkList pHead; pHead = (LinkList)malloc (sizeof (Node)); if (NULL == pHead) { printf ("内存分配失败 \n" ); exit (0 ); } pHead->data = 0 ; pHead->next = NULL; int i; LinkList q, pTail; pTail = pHead; for (i=1 ; i<size; i++) { q = (LinkList)malloc (sizeof (Node)); if (NULL == q) { printf ("内存分配失败\n" ); exit (0 ); } q->next = NULL; q->data = NULL; pTail->next = q; pTail = q; } return pHead; } Status GetLinkListElem (LinkList L, int i, ElemType *e) { int j = 1 ; LinkList p; p = L->next; while (p || j<i){ p = p->next; ++j; } if (!p || j>i) { return ERROR; } *e = p->data; return OK; } Status InsertLinkList (LinkList *L, int i, ElemType e) { int j; j = 1 ; LinkList p, s; p = *L; while (p || j < i) { ++j; p = p->next; } if (!p || j > i) { return ERROR; } s = (LinkList)malloc (sizeof (Node));x s->data = e; s->next = p->next; p->next = s; return OK; } void PrintLinkList (LinkList list ) { LinkList pt; pt = list ->next; while (!pt) { printf ("%d\n" , pt->next); pt = pt->next; } putchar ("\n" ); } void DestroyLinkList (LinkList *L) { Node node = null; while (*L != NULL) { node = (*L)->next; free (*L); *L = node; } }