标签:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define OK 1 6 #define ERROR 0 7 typedef int Status; 8 typedef int ElemType; 9 10 typedef struct Node 11 { 12 ElemType data; 13 struct Node* next; 14 }Node; 15 16 typedef struct Node* LinkList; 17 18 Status InitList(LinkList* L) //初始化操作,建立一个空的线性表.想把头指针里面的值改掉,所以传个头指针的地址 19 { 20 *L = (LinkList)malloc(sizeof(Node)); 21 if (!(*L)) 22 return ERROR; 23 (*L)->next = *L; 24 return OK; 25 } 26 Status ListEmpty(LinkList L) //若线性表为空,返回true,否则返回false 27 { 28 if (L->next == L) 29 return OK; 30 return ERROR; 31 } 32 Status ClearList(LinkList* L) //清空线性表 33 { 34 LinkList p = (*L)->next; 35 LinkList q; 36 while (p != *L) 37 { 38 q = p; 39 p = p->next; 40 free(q); 41 } 42 (*L)->next = (*L); 43 return OK; 44 } 45 Status GetElem(LinkList L, int i, ElemType *e) //查找线性表中的第i个位置的元素值,并赋值给e 46 { 47 int j = 1; 48 LinkList p = L->next; 49 while ((p!=L) && j < i) 50 { 51 p = p->next; 52 j++; 53 } 54 if ((p==L) || j > i) 55 return ERROR; 56 *e = p->data; 57 return OK; 58 } 59 Status LocateElem(LinkList L, ElemType e) //查找线性表L中与给定值e相等的元素,如果查找成功,则返回第一个相同的元素在L中的下标;否则,返回0表示失败 60 { 61 int i = 1; 62 LinkList p = L->next; 63 while (p != L && p->data != e) 64 { 65 p = p->next; 66 i++; 67 } 68 if (!p) 69 return ERROR; 70 return i; 71 } 72 Status ListInsert(LinkList* L, int i, ElemType e) //在线性表L的第i个位置插入元素e 73 { 74 LinkList p, q; 75 p = (*L)->next; 76 int j = 1; 77 while ((p!=*L) && j < i) //尾部插入时,p指向最后一个值,而j=i-1 78 { 79 p = p->next; 80 j++; 81 } 82 if (!p || j > i) 83 return ERROR; 84 85 q = (LinkList)malloc(sizeof(Node)); 86 q->data = e; 87 q->next = p->next; 88 p->next = q; 89 90 return ERROR; 91 } 92 Status ListDelete(LinkList*L, int i, ElemType* e) //删除线性表L中第i个位置元素,并用e返回其值 93 { 94 LinkList p, q; 95 p = *L; 96 int j = 1; 97 //while (p && j<i) 98 while ((p->next!=*L) && j < i) 99 { 100 p = p->next; 101 j++; 102 } 103 //if (!p || j > i) 104 if (!(p->next) || j > i) 105 return ERROR; 106 q = p->next; 107 *e = q->data; 108 p->next = q->next; 109 free(q); 110 return OK; 111 } 112 Status CreateListHead(LinkList *L, int n) 113 { 114 LinkList p; 115 int i; 116 (*L) = (LinkList)malloc(sizeof(Node)); 117 (*L)->next = *L; 118 for (i = 0; i < n; i++) 119 { 120 p = (LinkList)malloc(sizeof(Node)); 121 p->data = i; 122 p->next = (*L)->next; 123 (*L)->next = p; 124 } 125 return OK; 126 } 127 Status CreateListTail(LinkList *L, int n) 128 { 129 LinkList p; 130 int i; 131 (*L) = (LinkList)malloc(sizeof(Node)); 132 //(*L)->next = NULL; 133 LinkList q = (*L); 134 for (i = 0; i < n; i++) 135 { 136 p = (LinkList)malloc(sizeof(Node)); 137 p->data = i + 10; 138 q->next = p; 139 q = p; 140 } 141 q->next = *L; 142 return OK; 143 } 144 Status visit(ElemType n) 145 { 146 printf("-> %d ", n); 147 return OK; 148 } 149 Status ListTraverse(LinkList L) 150 { 151 LinkList p = L->next; 152 while (p != L) 153 { 154 visit(p->data); 155 p = p->next; 156 } 157 return OK; 158 } 159 160 int ListLength(LinkList L) //返回线性表L的长度 161 { 162 int i = 0; 163 LinkList p = L->next; 164 while (p != L) 165 { 166 i++; 167 p = p->next; 168 } 169 return i; 170 } 171 172 int main() 173 { 174 LinkList L; 175 Status i, j; 176 char opp = ‘-1‘; 177 ElemType e; 178 int pos = 1; 179 int k = 0; 180 181 i = InitList(&L); 182 printf("链表L初始化完毕,ListLength(L)=%d\n\n", ListLength(L)); 183 184 printf("\n1.遍历操作 \n2.插入操作 \n3.删除操作 \n4.获取结点数据\n5.查找某个数是否在链表中 \n6.头插法创建链表 \n7.尾插法创建链表 \n8.置空链表 \n0.退出 \n请选择你的操作:\n"); 185 186 while (opp != ‘0‘) { 187 scanf_s("%c", &opp); 188 switch (opp) { 189 case ‘1‘: 190 ListTraverse(L); 191 printf("\n"); 192 break; 193 194 case ‘2‘: 195 srand((unsigned)time(NULL)); 196 for (j = 1; j <= 10; j++) 197 { 198 i = ListInsert(&L, 1, rand() % 100); 199 } 200 printf("在L的表头依次插入10个随机数后:"); 201 ListTraverse(L); 202 printf("\n"); 203 printf("链表L创建完毕,ListLength(L)=%d\n\n", ListLength(L)); 204 break; 205 case ‘3‘: 206 printf("要删除第几个元素?"); 207 scanf_s("%d", &pos); 208 ListDelete(&L, pos, &e); 209 printf("删除第%d个元素成功,现在链表为:\n", pos); 210 ListTraverse(L); 211 printf("\n"); 212 break; 213 214 case ‘4‘: 215 printf("你需要获取第几个元素?"); 216 scanf_s("%d", &pos); 217 GetElem(L, pos, &e); 218 printf("第%d个元素的值为:%d\n", pos, e); 219 printf("\n"); 220 break; 221 case ‘5‘: 222 printf("输入你需要查找的数:"); 223 scanf_s("%d", &pos); 224 k = LocateElem(L, pos); 225 if (k) 226 printf("第%d个元素的值为%d\n", k, pos); 227 else 228 printf("没有值为%d的元素\n", pos); 229 printf("\n"); 230 break; 231 case ‘6‘: 232 CreateListHead(&L, 10); 233 printf("整体创建L的元素(头插法):\n"); 234 ListTraverse(L); 235 printf("\n"); 236 break; 237 238 case ‘7‘: 239 CreateListTail(&L, 10); 240 printf("整体创建L的元素(尾插法):\n"); 241 ListTraverse(L); 242 printf("\n"); 243 break; 244 245 case ‘8‘: 246 i = ClearList(&L); 247 printf("\n清空L后:ListLength(L)=%d\n", ListLength(L)); 248 ListTraverse(L); 249 printf("\n"); 250 break; 251 252 case ‘0‘: 253 exit(0); 254 } 255 } 256 }
标签:
原文地址:http://www.cnblogs.com/miracle2015126/p/5870013.html