1 void MergeList(LinkList &ha, LinkList &hb, LinkList &hc)
2 {
3 LinkList pa, pb;
4 pa = ha;
5 pb = hb;
6 while (pa->next && pb->next)
7 {
8 pa = pa->next;
9 pb = pb->next;
10 }
11 if (!pa->next)
12 {
13 hc = hb;
14 while (pb->next)
15 pb = pb->next;
16 pb->next = ha->next;
17 }
18 else
19 {
20 hc = ha;
21 while (pa->next)
22 pa = pa->next;
23 pa->next = hb->next;
24 }
25 }
26
27
28 Status DeleteAndInsertSub(LinkList &la, LinkList &lb, int i, int j, int len)
29 {
30 LinkList p, q, s, prev = NULL;
31 int k = 1;
32 if (i < 0 || j < 0 || len < 0)
33 return INFEASIBLE;
34 p = la;
35 while (p && k < i)
36 {
37 prev = p;
38 p = p->next;
39 k++;
40 }
41 if (!p)
42 return INFEASIBLE;
43 q=p;
44 k = 1;
45 while (q && k < len)
46 {
47 q = p->next;
48 k++;
49 }
50 if (!q)
51 return INFEASIBLE;
52 if (!prev)
53 la = q->next;
54 else
55 prev->next = q->next;
56 if (j = 1)
57 {
58 q->next = lb;
59 lb = p;
60 }
61 else
62 {
63 s = lb;
64 k = 1;
65 while (s && k < j - 1)
66 {
67 s = s->next;
68 k++;
69 }
70 if (!s)
71 return INFEASIBLE;
72 q->next = s->next;
73 s->next = p;
74 }
75 return OK;
76 }
77
78
79 Status ListDelete_L(LinkList &L, ElemType mink, ElemType maxk)
80 {
81 LinkList p, q, prev = NULL;
82 if (mink > maxk)
83 return ERROR;
84 p = L
85 prev = p;
86 p = p->next;
87 while (p && p-> data < maxk)
88 {
89 if (p->data <= mink)
90 prev = p;
91 p = p->next;
92 }
93 else
94 {
95 prev->next = p->next;
96 q = p;
97 p = p->next;
98 free(q);
99 }
100 }
101 return OK;
102 }
103
104 Status ListOppose_Sq(SqList &L)
105 {
106 int i;
107 ElemType x;
108 for (i = 0; i < L.length / 2; i++)
109 {
110 x = L.elem[i];
111 L.elem[i] = L.elem[L.length - 1 - i];
112 L.elem[L.length - 1 - i] = x;
113 }
114 return OK;
115 }
116
117 Status ListOppose_L(LinkList &L)
118 {
119 linkList p, q;
120 p = L;
121 p = p->next;
122 L - next = NULL;
123 while (p)
124 {
125 q = p;
126 p = p->next;
127 q->next = L->next;
128 L->next = q;
129 }
130 return OK;
131 }
132 Status ListOppose_L(LinkList&L)
133 {
134 Linklist p, q;
135 p = L;
136 p = p->next;
137 L->next = NULL;
138 while (P)
139 {
140 q = p;
141 p = p->next;
142 q->next = L->next;
143 L->next = q;
144 }
145 return OK;
146 }
1 #include <stdio.h>
2 #include <stdlib.h>
3 struct _Node
4 {
5 int data;
6 struct _Node *next;
7 };
8 typedef struct _Node node_t;//
9 typedef struct _Linklist
10 {
11 node_t * phead;//就算下面那个结构体有一个尾指针,也不是双向,尾指针只能用来索引尾节点,没法往前索引了。只有单向,主要看方向
12 //不是看头结点那个表头单向可以用双向也可以用
13 node_t * ptail;
14 int len;
15 }Linklist;
16 static node_t * GetNode(int i) // 新建并初始化节点
17 {
18 node_t *pNode;
19 pNode = (node_t *) malloc(sizeof(node_t));
20 if (!pNode)
21 {
22 printf("Error,the memory is not enough!\n");
23 exit(-1);
24 }
25 pNode->data = i;
26 pNode->next = NULL;
27 return pNode;
28 }
29 void init_list(Linklist *plist) // 用第一个节点初始化循环单链表
30 {
31 node_t *p;
32 p = GetNode(1);
33 // printf("The New Node is: %d\n", p -> data); // **** TEST ****
34 plist->phead = p;
35 plist->ptail = p;
36 p->next = plist->phead;
37 plist->len = 1;
38 }
39 static void Create_List(Linklist *plist, int n) // 把其余数据添加到循环单链表中
40 {
41 int i = 0;
42 node_t *pNew;
43 for (i = 2; i <= n; i++)
44 {
45 pNew = GetNode(i);
46 /******** TEST ********
47 printf("The New Node is: %d\n", pNew -> data);
48 ******** TEST ********/
49 plist->ptail->next = pNew;
50 plist->ptail = pNew;
51 pNew->next = plist->phead;
52 plist->len++;
53 }
54 printf("Completes the e-way circulation chain table the foundation!\n");
55 }
56 void Print_List(Linklist *plist) // 输出链表内容
57 {
58 node_t *pCur = plist->phead;//
59 do
60 {
61 printf("The %d person.\n", pCur->data);
62 pCur = pCur->next;
63 } while (pCur != plist->phead);
64 printf("The length of the List: %d\n", plist->len);
65 }
66 void joseph(Linklist *plist, int m) //约瑟夫回环函数实现
67 {
68 node_t *pPre = plist->ptail;
69 node_t *pCur = plist->phead;
70 int i;
71 while (plist->len != 1)
72 {
73 i = 0;
74 while (i < m - 1)
75 {
76 pPre = pPre->next;
77 i++;
78 }
79 pCur = pPre->next;
80 pPre->next = pCur->next;
81 free(pCur);
82 plist->len--;
83 }
84 printf("The last one is: %d\n", pPre->data);
85 }
86 int main()
87 {
88 int n = 0;
89 printf("Please input the Length of the Circle list: ");
90 scanf_s("%d", &n);
91 int m = 0;
92 printf("Please input the Stop point: ");
93 scanf_s("%d", &m);
94 Linklist pList;
95 init_list(&pList);
96 Create_List(&pList, n);
97 Print_List(&pList);
98 joseph(&pList, m);
99 system("pause");
100 return 0;
101 }