- UID
- 1029342
- 性别
- 男
|
14,ListInsert操作
复制代码
1 #include"defs.h"
2
3 int ListInsert(linklist L, int i, int e)
4 {
5 linklist p = L; //p指向头结点
6 linklist q, s;
7 int j = 0;
8 if (i<1 || i>ListLength(L)+1)
9 exit(0);
10
11 while (j < i-1) //找到第i-1个结点
12 {
13 ++j;
14 p = p->next;
15 }
16 q = p->next; //q指向第i个结点
17
18 s = (linklist)malloc(sizeof(struct DULNode));
19 s->data = e;
20
21 s->next = q; //先改变向右的指针
22 p->next = s;
23 s->prior = p; //改变向左的指针
24 q->prior = s;
25 return 0;
26 }
复制代码
15,ListDelete操作
复制代码
1 #include"defs.h"
2
3 int ListDelete(linklist L, int i, int *e)
4 {
5 linklist p = L;
6 linklist q;
7 int j = 0;
8 if (i<1 || i>ListLength(L)) //位置是否合理
9 exit(0);
10
11 while (j < i-1) //找到第i-1个元素
12 {
13 ++j;
14 p = p->next;
15 }
16 q = p->next; //p指向第i个结点
17 *e = q->data;
18
19 p->next = q->next;
20 q->next->prior = p;
21 free(q);
22 return 0;
23 }
复制代码
16,TravelList操作
复制代码
1 #include"defs.h"
2
3 void TravelList(linklist L)
4 {
5 linklist p = L->next;
6 int j = 0;
7 while (p != L)
8 {
9 ++j;
10 printf("第%d个元素值为:%d\n", j, p->data);
11 p = p->next;
12 }
13 }
复制代码
17,TravelListBack操作,逆序输出表中元素
复制代码
1 #include"defs.h"
2
3 void TravelListBack(linklist L)
4 {
5 linklist p = L->prior;
6 while (p != L)
7 {
8 printf("%d ", p->data);
9 p = p->prior;
10 }
11 printf("\n");
12 } |
|