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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
| #include<stdio.h> #include<malloc.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typedef int Status; typedef char ElemType;
typedef struct LNode { ElemType data; struct LNode *next; }LNode, *LinkList;
Status initList(LinkList* L) { *L = (LinkList)malloc(sizeof(LNode)); (*L)->data = 0; (*L)->next = NULL; return OK; }
char* isEmpty(LinkList* L) { return (*L)->next == NULL ? "空" : "不空"; }
int getLength(LinkList* L) { LinkList p = *L; int i = 0; while (p->next) { ++i; p = p->next; } return i; }
Status creatListHead(LinkList* L) { LinkList p ; char str[] = "myGithub:https://github.com/pengxiandyou"; (*L)->data=(int)_mbstrlen(str); for (int i = 0; i <(int)_mbstrlen(str); ++i) { p= (LinkList)malloc(sizeof(LNode)); p->data = str[i]; p->next = (*L)->next; (*L)->next = p; } return OK; }
Status creatListEnd(LinkList* L) { LinkList p,r=*L; char str[] = "myGithub:https://github.com/pengxiandyou"; (*L)->data = (int)_mbstrlen(str); for (int i = 0; i < (int)_mbstrlen(str); ++i) { p = (LinkList)malloc(sizeof(LNode)); p->data = str[i]; p->next = NULL; r ->next= p; r = p; } return OK; }
Status traversalList(LinkList L) { LinkList p = L->next; while (p) { printf("%c", p->data); p = p->next; } printf("\n"); return OK; }
Status insertList(LinkList* L, int i ,char c) { if (L == NULL || (i > (*L)->data || i < 1))return ERROR; LinkList p=*L; int j = 0; while (p&&j<i-1) { p = p->next; j++; } LinkList q = (LinkList)malloc(sizeof(LNode)); q->data = c; q->next = p->next; p->next = q; (*L)->data++; return OK; }
LNode* getListByIndex(LinkList L, int i) { LinkList p = L; int j = 0; while (p&&j<i - 1) { p = p->next; j++; }
return (p); }
LNode* getListByElem(LinkList L, char c ,int* i) { LinkList p = L; int j = 0; while (p&&p->next->data!=c) { p = p->next; j++; } *i = j + 1; return p; }
Status deleteListElem(LinkList* L, int i) { LinkList p = getListByIndex(*L, i); LinkList q = p->next; p->next = p->next->next; free(q); (*L)->data--; return OK; }
Status deleteListElem(LinkList* L, char c,int* i) { LinkList p = getListByElem(*L, c,i); LinkList q = p->next; p->next = p->next->next; free(q); (*L)->data--; return OK; }
Status Reverse(LinkList* L) { LinkList p = (*L)->next; LinkList q = *L; LinkList s; while (q->next) { q = q->next; } while (p!=q) { s = q->next; q->next = p; (*L)->next = p->next; p->next = s; p = (*L)->next; } return OK; }
Status changeElemByIndex(LinkList* L,char c, int i) { getListByIndex(*L, i)->next->data = c; return OK; }
LNode* findListMiddle(LinkList L) { LinkList fast, slow; int i; fast = slow = L;
for (i = 0; fast != NULL&&fast->next != NULL; fast = fast->next->next) { slow = slow->next; i++; } return slow; }
Status deleteALL(LinkList* L) { LinkList p = *L, q; while (p->next) { q = p->next; p->next = q->next; (*L)->data--; free(q); } free(p); return OK; } int main(void) { LinkList L; printf("开始初始化\n"); printf("初始化-------%s\n", initList(&L)==OK?"成功":"失败"); printf("单链表为-----%s\n", isEmpty(&L)); printf("单链表长度为-%d\n",getLength(&L)); printf("开始建表,使用默认头插法方式,尾插法未使用\n"); printf("建表---------%s\n", creatListHead(&L)==OK?"成功":"失败"); printf("单链表长度为-%d\n", getLength(&L)); printf("开始遍历\n"); printf("遍历---------%s\n", traversalList(L) == OK ? "成功" : "失败"); printf("开始逆转\n"); printf("逆转-------------------%s\n", Reverse(&L) == OK ? "成功" : "失败"); printf("遍历-------------------%s\n", traversalList(L) == OK ? "成功" : "失败"); printf("逆转-------------------%s\n", Reverse(&L) == OK ? "成功" : "失败"); printf("遍历-------------------%s\n", traversalList(L) == OK ? "成功" : "失败"); printf("寻找中间结点\n"); LNode* s = findListMiddle(L); printf("地址为 %d\t值为 %c\n",s ->next, s->data); printf("开始查找\n"); int i = 0; LNode* t= getListByElem(L, 'h', &i); printf("按值(h)查找,值为 %c\t索引为 %d\n", t->next->data, i); t = getListByIndex(L, 20); printf("按为(20)查找,值为 %c\n", t->next->data); printf("开始插入\n"); printf("在索引1处插入!---------%s\n", insertList(&L,1,'!') == OK ? "成功" : "失败"); printf("单链表长度为-%d\n", getLength(&L)); printf("遍历---------%s\n", traversalList(L) == OK ? "成功" : "失败"); printf("开始改值\n"); printf("更改索引1处的值为%%-----%s\n", changeElemByIndex(&L, '%', 1) == OK ? "成功" : "失败"); printf("遍历--------------------%s\n", traversalList(L) == OK ? "成功" : "失败"); printf("开始删除\n"); printf("通过索引1\n"); deleteListElem(&L, 1); printf("单链表长度为-----------%d\n", getLength(&L)); printf("遍历-------------------%s\n", traversalList(L) == OK ? "成功" : "失败"); printf("通过值#\n"); printf("在索引1处插入#---------%s\n", insertList(&L, 1, '#') == OK ? "成功" : "失败"); printf("单链表长度为-----------%d\n", getLength(&L)); printf("遍历-------------------%s\n", traversalList(L) == OK ? "成功" : "失败"); int j = 0; deleteListElem(&L,'#', &j); printf("单链表长度为-----------%d\n", getLength(&L)); printf("遍历-------------------%s\n", traversalList(L) == OK ? "成功" : "失败"); printf("开始删除表\n"); printf("删除-------------------%s\n", deleteALL(&L) == OK ? "成功" : "失败"); system("pause"); return 0; }
|