Data_Struct(LinkList)
最近在学数据结构,学到链表这节作业有链表,毕竟菜鸟代码基本照看书上算法写的,再加上自己的小修改,这里先记录下来,万一哪天想看了,来看看。
里面有用到二级指针,还是不太理解,还有就是注释不多,后续有了更好的理解,再来添加
1 #define TRUE 1 2 #define FALSE 0 3 #define OK 1 4 #define ERROR 0 5 #define OVERFLOW -2 6 #define INFEASIBLE -1 7 #define Status int 8 #define ElemType int 9 10 typedef struct LNode 11 { 12 ElemType data; 13 struct LNode *next; 14 }LNode; 15 16 typedef LNode LinkList; 17 Status ListInsert_L(LinkList L, int i, ElemType e);//插入函数 18 Status ListDelete_L(LinkList L, int i, ElemType *e);//删除某个元素 19 void CreateList_L(LinkList L, int n);//创建一个带头结点的空链表 20 void MergeList_L(LinkList *La, LinkList *Lb, LinkList **Lc); //合并两个有序链表
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <malloc.h> 4 #include "LinkList.h" 5 6 7 //新建带头结点的空链表 8 void CreateList_L(LinkList **L, int n) 9 { 10 int i; LinkList *p = NULL; 11 (*L) = (LinkList*) malloc (sizeof (LNode)); 12 (*L) ->next = NULL; 13 printf("请输入链表元素 :\n"); 14 for(i = n; i > 0; --i) 15 { 16 p = (LinkList*) malloc (sizeof (LNode));//生成新结点 17 scanf("%d", &p -> data);//输入元素值 18 p -> next = (*L) -> next; 19 (*L) -> next = p;//插入到表头 20 } 21 } 22 23 24 //插入元素 25 Status ListInsert_L(LinkList *L, int i, ElemType e) 26 { 27 LinkList *p = L; 28 LinkList *s = NULL; 29 int j = 0; 30 while (p->next && j < i - 1) 31 { 32 p = p -> next; 33 ++ j; 34 } 35 if(!p || j != i - 1) 36 return ERROR; 37 s = (LinkList*) malloc (sizeof(LNode)); 38 s -> data = e; 39 s -> next = p ->next; 40 p -> next = s; 41 return OK; 42 } 43 44 45 //删除元素 46 Status ListDelete_L(LinkList *L, int i, ElemType *e) 47 { 48 LinkList *p = L; 49 LinkList *q; 50 int j = 0; 51 while(p -> next && j < i - 1) 52 { 53 p = p -> next; 54 ++ j; 55 } 56 if(!(p -> next) || j > i - 1) 57 return ERROR; 58 q = p -> next; 59 p -> next = q -> next; 60 *e= q -> data; 61 free(q); 62 return OK; 63 } 64 65 66 67 68 void MergeList_L(LinkList *La, LinkList *Lb, LinkList **Lc) 69 { 70 LinkList *pa = La -> next; 71 LinkList *pb = Lb -> next; 72 LinkList *pc = NULL; 73 (*Lc) = pc = La; 74 while(pa && pb) 75 { 76 if(pa -> data <= pb -> data) 77 { 78 pc -> next = pa; 79 pc = pa; 80 pa = pa ->next; 81 } 82 else 83 { 84 pc -> next = pb; 85 pc = pb; 86 pb = pb -> next; 87 } 88 } 89 pc -> next = pa ? pa : pb;//插入剩余段 90 free(Lb);//释放Lb头结点 91 Lb = NULL; 92 93 } 94 95 96 void Free(LinkList *L) 97 { 98 if(L && L -> next) 99 Free( L -> next); 100 free(L); 101 return; 102 } 103 104 105 int main() 106 { 107 int selectn,length0,length1,location,e,counti; 108 LNode *L1 = NULL,*L2 = NULL, *L3 = NULL, *la = NULL, *lc = NULL; 109 printf("请输入新链表长度 :\n"); 110 scanf("%d", &length0); 111 CreateList_L(&L1, length0); 112 printf("请选择 : \n"); 113 printf("1 : 插入元素\n"); 114 printf("2 : 删除元素\n"); 115 printf("3 : 合并链表\n"); 116 scanf("%d", &selectn); 117 switch (selectn) 118 { 119 case 1 : 120 printf("请输入插入位置 :\n"); 121 scanf("%d", &location); 122 printf("请输入要插入元素 :\n"); 123 scanf("%d", &e); 124 if(ListInsert_L(L1, location, e) == OK ) 125 { //逆序输出 126 counti = 1; 127 la = L1; 128 printf("新链表的顺序为 :\n"); 129 while(la -> next) 130 { 131 la = la->next; 132 if(counti++ % 10) 133 printf("%-5d",la -> data); 134 else 135 printf("%-5d\n",la -> data); 136 } 137 } 138 else 139 printf("插入异常\n"); 140 printf("\n"); 141 break; 142 case 2 : 143 printf("请输入要删除的位置 :\n"); 144 scanf("%d",&location); 145 if(ListDelete_L(L1, location, &e) == OK) 146 { 147 printf("删除成功\n被删除元素为 : %-5d\n", e); 148 } 149 else 150 printf("删除异常\n"); 151 break; 152 case 3 : 153 printf("请输入链表2 :\n"); 154 printf("请输入链表长度 :\n"); 155 scanf("%d", &length1); 156 CreateList_L(&L2, length1); 157 MergeList_L(L1, L2, &L3); 158 L2 = NULL; 159 lc = L3 -> next; 160 printf("新链表顺序为 :\n"); 161 counti = 1; 162 while(lc) 163 { if(counti++ % 10) 164 printf("%-5d", lc -> data); 165 else 166 printf("%-5d", lc -> data); 167 lc = lc -> next; 168 } 169 printf("\n"); 170 break; 171 default : 172 printf("ERROR\n"); 173 break; 174 } 175 Free(L1); 176 L1 = NULL; 177 L3 = NULL; 178 system("pause"); 179 return 0; 180 }