1 #include <stdio.h> 2 #include <stdlib.h> 3 #define OVERFLOW -2 4 #define OK 1 5 #define ERROR 0 6 typedef int ElemType; 7 8 typedef struct LNode { 9 ElemType data; 10 struct LNode *next; 11 }LNode,*LinkList; 12 13 LinkList CreateList_L(LinkList L,int n); 14 void TraverseList_L(LinkList L); 15 int GetElem_L(LinkList L,int i,ElemType *e); 16 LinkList ListInsert_L(LinkList L,int i,ElemType e); 17 LinkList ListDelete(LinkList L,int i,ElemType *e); 18 LinkList MergeList_L(LinkList La,LinkList Lb,LinkList Lc); 19 LocateList_L(LinkList L,ElemType e); 20 LinkList InverseList_L(LinkList L); 21 LinkList InitList_L(LinkList L); 22 23 //初始化单链表 24 LinkList InitList_L(LinkList L) { 25 L = (LinkList)malloc(sizeof(LNode)); 26 L->next = NULL; 27 return L; 28 } 29 //创建一个单向链表 30 LinkList CreateList_L(LinkList L,int n) { 31 int i; 32 LinkList p; 33 34 printf("请输入元素的值:"); 35 for(i=n; i>0; --i) { 36 p = (LinkList)malloc(sizeof(LNode)); 37 scanf("%d",&p->data); 38 p->next = L->next; 39 L->next = p; 40 } 41 return L; 42 } 43 44 //遍历链表 45 void TraverseList_L(LinkList L) { 46 LinkList p; 47 p = L->next; 48 while(p) { 49 printf("%d ",p->data); 50 p = p->next; 51 } 52 printf(" "); 53 } 54 55 //取得链表上第i个元素 56 int GetElem_L(LinkList L,int i,ElemType *e) { 57 LinkList p; 58 int j; 59 p = L->next; 60 j = 1; 61 while(p && j<i) { 62 p = p->next; 63 ++j; 64 } 65 if(!p||j>i) return ERROR; 66 *e = p->data; 67 return OK; 68 } 69 70 //向链表中插入一个元素 71 LinkList ListInsert_L(LinkList L,int i,ElemType e) { 72 LinkList p,s; 73 int j; 74 p = L; 75 j = 0; 76 while(p && j<i-1) { 77 p = p->next; 78 ++j; 79 } 80 if(!p || j>i-1) return ERROR; 81 s = (LinkList)malloc(sizeof(LNode)); 82 s->data = e; 83 s->next = p->next; 84 p->next = s; 85 return L; 86 } 87 88 //从链表删除一个元素 89 LinkList ListDelete(LinkList L,int i,ElemType *e) { 90 LinkList p,q; 91 int j; 92 p = L; 93 j = 0; 94 while(p->next && j<i-1) { 95 ++j; 96 p = p->next; 97 } 98 if(!(p->next) || j>i-1) return ERROR; 99 q = p->next; 100 p->next = q->next; 101 *e = q->data; 102 free(q); 103 return L; 104 } 105 106 //将两个链表进行归并排序合并 107 LinkList MergeList_L(LinkList La,LinkList Lb,LinkList Lc) { 108 LinkList pa,pb,pc; 109 pa = La->next; 110 pb = Lb->next; 111 Lc = pc = La; 112 while(pa && pb) { 113 if(pa->data <= pb->data) { 114 pc->next = pa; 115 pc = pa; 116 pa = pa->next; 117 } else { 118 pc->next = pb; 119 pc = pb; 120 pb = pb->next; 121 } 122 } 123 pc->next = pa?pa:pb; 124 free(Lb); 125 return Lc; 126 } 127 128 //取得某一个元素的位序 129 int LocateList_L(LinkList L,ElemType e) { 130 LinkList p; 131 int i; 132 p = L->next; 133 i = 0; 134 while(p) { 135 if(p->data == e) { 136 ++i; 137 break; 138 } 139 p = p->next; 140 } 141 if(p == NULL) { 142 return 0; 143 } else { 144 return i; 145 } 146 } 147 148 //将单向链表逆置 149 LinkList InverseList_L(LinkList L) { 150 LinkList pre,cur,next; 151 pre = L->next; 152 cur = pre->next; 153 next = cur->next; 154 pre->next = NULL; 155 cur->next = pre; 156 pre = cur; 157 cur = next; 158 while(cur != NULL) { 159 next = cur->next; 160 cur->next = pre; 161 pre = cur; 162 cur = next; 163 } 164 L->next = pre; 165 return L; 166 } 167 168 int main() 169 { 170 LinkList lin,lin1,lin2; 171 int n,e,i; 172 lin = InitList_L(lin); 173 lin1 = InitList_L(lin1); 174 175 printf("请输入元素的个数: "); 176 scanf("%d",&n); 177 lin = CreateList_L(lin,n); 178 179 printf("链表中的元素为: "); 180 TraverseList_L(lin); 181 182 if(GetElem_L(lin,1,&e)) 183 printf("第i个元素为:%d ",e); 184 185 lin = ListInsert_L(lin,1,6); 186 printf("插入一个元素后的链表为:"); 187 TraverseList_L(lin); 188 189 lin = ListDelete(lin,1,&e); 190 printf("删除一个元素后的链表为:"); 191 TraverseList_L(lin); 192 printf("删除的元素为:%d ",e); 193 194 printf("请输入元素的个数: "); 195 scanf("%d",&n); 196 lin1 = CreateList_L(lin1,n); 197 198 //合并后的链表为: 199 printf("合并后的链表为:"); 200 lin2 = MergeList_L(lin,lin1,lin2); 201 TraverseList_L(lin2); 202 203 //取得某一元素的位序为: 204 i = LocateList_L(lin2,3); 205 if(i == 0) { 206 printf("未找到该元素: "); 207 } else { 208 printf("该元素的位序为:%d ",i); 209 } 210 //将单向链表逆置 211 printf("逆置后的链表为: "); 212 lin = InverseList_L(lin); 213 TraverseList_L(lin); 214 return 0; 215 }