1 #include<string.h>
2 #include<ctype.h>
3 #include<malloc.h>
4 #include<limits.h>
5 #include<stdio.h>
6 #include<stdlib.h>
7 #include<math.h>
8
9 #define TRUE 1
10 #define FALSE 0
11 #define OK 1
12 #define ERROR 0
13 #define INFEASIBLE -1
14
15 typedef int Status;
16 typedef int Boolean;
17 typedef int ElemType;
18
19
20 struct LNode{
21 ElemType data;
22 struct LNode *next;
23 };
24 typedef struct LNode *LinkList;
25
26
27 Status InitList(LinkList *L){
28 //操作结果:构造一个空的线性表L
29 *L=(LinkList)malloc(sizeof(struct LNode));
30 if(!*L)
31 exit(OVERFLOW);
32 (*L)->next=NULL;
33 return OK;
34 }
35
36 Status DestoryList(LinkList *L){
37 //初始条件:线性表L已经存在。操作结果:销毁线性表L
38 LinkList q;
39 while(*L){
40 q=(*L)->next;
41 free(*L);
42 *L=q;
43 }
44 return OK;
45 }
46
47 Status ClearList(LinkList L){//不改变L
48 /*
49 初始条件:线性表L已经存在。
50 操作结果:将L重置为空表。
51 */
52 LinkList p,q;
53 p=L->next;
54 while(p){
55 q=p->next;
56 free(p);
57 p=q;
58 }
59 L->next=NULL;
60 return OK;
61 }
62
63 Status ListEmpty(LinkList L){
64 /*
65 初始条件:线性表L已经存在。
66 操作结果:若L为空表,则返回TRUE,否则返回为FALSE
67 */
68 if(L->next)
69 return FALSE;
70 else
71 return TRUE;
72 }
73
74 Status ListLenth(LinkList L){
75 /*
76 初始条件:线性表L已经存在。
77 操作结果:返回L中数据元素的个数
78 */
79 int i=0;
80 LinkList p=L->next;
81 while(p){
82 i++;
83 p=p->next;
84 }
85 return i;
86 }
87
88 Status GetElem(LinkList L,int i,ElemType *e){
89 /*
90 L为带头节点的单链表的头指针。当第i个元素存在时,其值赋给e并返回OK,否则返回ERROR
91 */
92 int j=1;
93 LinkList p=L->next;
94 while(p&&j<i){//顺指针向后查找,直到p指向第i个元素或p为空
95 p=p->next;
96 j++;
97 }
98 if(!p||j>i)//第i个元素不存在
99 return ERROR;
100 *e=p->data;
101 return OK;
102 }
103
104 Status LocateElem(LinkList L,ElemType e,Status(*compare)(ElemType,ElemType)){
105 /*
106 初始条件:线性表L已经存在,compare()是数据元素判定函数(满足为1,否则为0)
107 操作结果:返回L中第1个与e满足关系compare()的数据元素的位序。
108 */
109 int i=0;
110 LinkList p=L->next;
111 while(p){
112 i++;
113 if(compare(p->data,e))
114 return i;
115 p=p->next;
116 }
117 return 0;
118 }
119
120 Status PriorElem(LinkList L,ElemType cur_e,ElemType *pre_e){
121 /*
122 初始条件:线性表L已经存在。
123 操作结果:若cur_e是L的元素,且不是地一个,则用pre_e返回它的前驱。
124 */
125 LinkList q,p=L->next;
126 while(p->next){
127 q=p->next;
128 if(q->data==cur_e){
129 *pre_e=p->data;
130 return OK;
131 }
132 p=q;
133 }
134 return INFEASIBLE;
135 }
136
137
138 Status NextElem(LinkList L,ElemType cur_e,ElemType *next_e){
139 /*
140 初始条件:线性表L已经存在。
141 操作结果:若cur_e是L的数据元素,且个不是最后一个,则用next_e返回它的后继
142 */
143 LinkList p=L->next;
144 while(p->next){
145 if(p->data==cur_e){
146 *next_e=p->next->data;
147 return OK;
148 }
149 p=p->next;
150 }
151 return INFEASIBLE;
152
153 }
154
155 Status LinkInsert(LinkList L,int i,ElemType e){
156 /*在带头节点的单链线性表中的第i个位置插入元素e*/
157 int j=0;
158 LinkList p=L,s;
159 while(p&&j<i-1){
160 p=p->next;
161 j++;
162 }
163 if(!p||j>i-1)//i小于1或者大于表长
164 return ERROR;
165 s=(LinkList)malloc(sizeof(struct LNode));//生成新节点
166 s->data=e;
167 s->next=p->next;
168 p->next=s;
169 return OK;
170 }
171
172 Status ListDelete(LinkList L,int i,ElemType *e){
173 /*在带头节点的单链线性表L中,删除第i个元素,并由e返回其值*/
174 int j=0;
175 LinkList p=L,q;
176 while(p->next&&j<i-1){
177 p=p->next;
178 j++;
179 }
180 if(!p->next||j>i-1)
181 return ERROR;
182 q=p->next;
183 p->next=q->next;
184 *e=q->data;
185 free(q);
186 return OK;
187
188 }
189
190 Status ListTraverse(LinkList L,void(*vi)(ElemType)){
191 /*
192 初始条件:线性表L已存在。
193 操作结果:依次对L的每个数据元素调用函数vi()。一旦vi()失败,则操作失败。
194 */
195 LinkList p=L->next;
196 while(p){
197 vi(p->data);
198 p=p->next;
199 }
200 printf("\n");
201 return OK;
202 }
203
204
205 void CreateList(LinkList *L,int n){
206 /*逆位序(插在表头)输入n个元素的值,建立带表头节点的单链表L*/
207 int i;
208 LinkList p;
209 *L=(LinkList)malloc(sizeof(struct LNode));
210 (*L)->next=NULL;
211 printf("请输入%d个数据元素\n",n);
212 for(i=n;i>0;--i){
213 p=(LinkList)malloc(sizeof(struct LNode));
214 scanf("%d",&p->data);
215 p->next=(*L)->next;
216 (*L)->next=p;
217 }
218 }
219
220 void CreateList2(LinkList *L,int n){
221 /*正位序输入n个元素的值,建立带头节点的单链表*/
222 int i;
223 LinkList p,q;
224 *L=(LinkList)malloc(sizeof(struct LNode));
225 (*L)->next=NULL;
226 q=*L;
227 printf("请输入%d个数据\n",n);
228 for(i=1;i<=n;i++){
229 p=(LinkList)malloc(sizeof(struct LNode));
230 scanf("%d",&p->data);
231 q->next=p;
232 q=q->next;
233 }
234 p->next=NULL;
235 }