1 // 51、用循环队列实现,InitQueue(), DestroyQueue(), ClearQueue(), QueueEmpty()
2 #include <iostream>
3 #include <cstring>
4 using namespace std;
5 #define ok 1
6 #define test(j) cout<<j<<"*********"<<endl
7 typedef int status;
8 typedef int Datatype;
9 struct Node{
10 Datatype value;
11 struct Node *next;
12 };//定义节点
13
14 typedef struct{
15 struct Node *head;
16 struct Node *end;
17 int length;
18 }Queue;//定义队列
19
20 status InitQueue(Queue &q){
21 q.head = q.end = (Node*)malloc(sizeof(Node));
22 if(q.end == NULL){
23 cout<<"初始化失败"<<endl;
24 }
25 q.head->next = NULL;
26 q.end->next = q.head;
27 q.length = 0;
28 return ok;
29 }
30
31 status InsertQueue(Queue &q,Datatype e){
32 Node *p = (Node*)malloc(sizeof(Node));
33 if(!p){
34 cout<<"内存分配失败"<<endl;
35 }
36 p->value = e;
37 p->next = NULL;
38 q.end->next = p;
39 q.end = p;
40 if(q.head->next==NULL)
41 q.head->next = p;
42 q.end->next = q.head;
43 q.length++;
44 return 0;
45 }
46
47 status Gettop(Queue q,Datatype &a){
48 a = q.head->next->value;
49 return ok;
50 }
51
52 status OutQueue(Queue &q){
53 if(q.head == q.end){
54 cout<<"当前队列为空,没有元素可以删除"<<endl;
55 return ok;
56 }
57 q.head->next = q.head->next->next;
58 q.length--;
59 return ok;
60 }
61
62 status PrintQueue(Queue q){
63 Node *p;
64 p = q.head->next;
65 if(q.head == q.end){
66 cout<<"队列为空"<<endl;
67 return 0;
68 }
69 cout<<"当前共有"<<q.length<<"个元素"<<endl;
70 cout<<"-------------队列元素为--------------"<<endl;
71 while(p!=q.head){
72 cout<<p->value<<" ";
73 p=p->next;
74 }
75 cout<<endl;
76 return 0;
77 }
78
79 status QueueEmpty(Queue q){
80 if(q.head == q.end)
81 return 1;
82 else
83 return 0;
84 }
85
86 status ClearQueue(Queue &q){
87 struct Node *p,*t;
88 q.end = q.head;
89 p = q.head->next;
90 q.head->next = NULL;
91 while(p!=q.head){
92 t = p;
93 p = p->next;
94 free(t);
95 }
96 q.length = 0;
97 return ok;
98 }
99
100 status DestroyQueue(Queue &q){
101 if(q.head!=NULL){
102 q.end = q.head->next;
103 free(q.head);
104 q.head = q.end;
105 }
106 q.length = 0;
107 return ok;
108 }
109
110 int main(){
111 Queue q;
112 InitQueue(q);
113 Datatype a;
114 int n;
115 cout<<"输入队列中数的个数:";
116 cin>>n;
117 while(n--){//n个数进队
118 a = rand()%100;//保证每次输入的数是随机数
119 InsertQueue(q,a);
120 }
121 PrintQueue(q);
122 Gettop(q,a);
123 cout<<"----------当前头结点-----------"<<endl<<a<<endl;
124 cout<<"输入需要删除几个节点: ";
125 cin>>n;
126 while(n--){
127 OutQueue(q);
128 }
129 Gettop(q,a);
130 cout<<"----------当前头结点-----------"<<endl<<a<<endl;
131 PrintQueue(q);
132 //判断队列是否为空
133 if(QueueEmpty(q)){
134 cout<<"队列为空"<<endl;
135 }else{
136 cout<<"队列不为空"<<endl;
137 }
138 cout<<"----------清空队列-----------"<<endl;
139 ClearQueue(q);
140 PrintQueue(q);
141 if(QueueEmpty(q)){
142 cout<<"队列为空"<<endl;
143 }else{
144 cout<<"队列不为空"<<endl;
145 }
146 cout<<"再次输入队列中数的个数:";
147 cin>>n;
148 while(n--){//n个数进队
149 a = rand()%100;//保证每次输入的数是随机数
150 InsertQueue(q,a);
151 }
152 PrintQueue(q);
153 Gettop(q,a);
154 cout<<"----------当前头结点-----------"<<endl<<a<<endl;;
155 cout<<"-----------销毁队列-----------"<<endl;
156 DestroyQueue(q);
157 PrintQueue(q);
158 return 0;
159 }
160
161
162
163
164
165 //79、利用链表的插入运算建立线性链表,然后利用链表的查找、删除、计数、输出,排序,转置等运算实现链表的查找、计数操作,并单独写成函数的形式,能在屏幕上输出操作前后的结果。
166
167 #include <iostream>
168 #include <cstdio>
169 #include <cstdlib>
170 #include <cstring>
171 typedef int DataType;
172 typedef int Status;
173 typedef struct LinkList{
174 DataType val;
175 struct LinkList *next;
176 }LinkList;
177 using namespace std;
178
179 Status Getlist(LinkList *head,int n){//插入
180 LinkList *p=(LinkList*)malloc(sizeof(LinkList));
181 p=head;
182 srand((int)time(0));
183 for(int i=0;i<n;i++){
184 LinkList *q=(LinkList*)malloc(sizeof(LinkList));
185 q->val = rand()%100;// cin>>q->val;
186 p->next=q;
187 q->next=NULL;
188 p=q;
189 }
190 return 0;
191 }
192
193 Status Count(LinkList *head) {//计数
194 int sum=0;
195 int x;
196 cout<<"请输入要统计个数x的值:";
197 cin>>x;
198 LinkList *p;
199 if(!head)
200 return 0;
201 p=head->next;
202 while(p) {
203 if(p->val==x) {
204 sum++;
205 }
206 p=p->next;
207 }
208 cout<<"链表中X的元素的个数为:"<<sum<<endl;
209 return 0;
210 }
211
212 Status Find(LinkList *head){//查找
213 LinkList *p=(LinkList*)malloc(sizeof(LinkList));
214 int cnt=0,x;
215 bool prime = false;
216 cout<<"输入要查找的数:";
217 cin>>x;
218 p=head->next;
219 while(p!=NULL){
220 cnt++;
221 if(p->val == x){
222 cout<<x<<"为第"<<cnt<<"个数"<<endl;
223 prime = true;
224 }
225 p=p->next;
226 }
227 if(!prime)
228 cout<<"不存在所输入的数"<<endl;
229 return 0;
230 }
231
232 Status Delete(LinkList *head){//删除指定的数
233 LinkList *p=(LinkList*)malloc(sizeof(LinkList));
234 p=head;
235 int x;
236 cout<<"输入要删除的数:";
237 cin>>x;
238 while(p->next!=NULL){
239 if(p->next->val==x){
240 p->next=p->next->next;
241 continue;
242 }
243 if(p->next!=NULL)
244 p=p->next;
245 }
246 return 0;
247 }
248
249 Status Output(LinkList *head){//输出
250 LinkList *p=(LinkList*)malloc(sizeof(LinkList));
251 p=head;
252 while(p->next!=NULL){
253 p=p->next;
254 cout<<p->val<<" ";
255 }
256 cout<<endl;
257 return 0;
258 }
259
260 LinkList *Transpose(LinkList *head){//转置
261 LinkList *q=(LinkList*)malloc(sizeof(LinkList));
262 LinkList *p=(LinkList*)malloc(sizeof(LinkList));
263 if(head->next->next == NULL)
264 return head;
265 p=head->next;
266 while(p!=NULL){
267 LinkList *end=(LinkList*)malloc(sizeof(LinkList));
268 end->val = p->val;
269 end->next = q->next;
270 q->next = end;
271 p=p->next;
272 }
273 return q;
274 }
275
276 LinkList *BubbleSort( LinkList *head ){//排序
277 LinkList *q=(LinkList*)malloc(sizeof(LinkList));
278 LinkList *p=(LinkList*)malloc(sizeof(LinkList));
279 LinkList *end=(LinkList*)malloc(sizeof(LinkList));
280 int temp;
281 if(head->next->next == NULL)
282 return head;
283 q=p=head->next;
284 while(p->next!=NULL)
285 p=p->next;
286 end = p;
287 while(end != head->next){
288 for(p=head->next;p->next!=end;p=p->next){
289 q = p->next;
290 if(p->val>q->val){
291 temp = p->val;
292 p->val=q->val;
293 q->val = temp;
294 }
295 }
296 end = p;
297 q = p->next;
298 if(p->val>q->val){
299 temp = p->val;
300 p->val=q->val;
301 q->val = temp;
302 }
303 }
304 return head;
305 }
306 int main(){
307 int n;
308 LinkList *head=(LinkList*)malloc(sizeof(LinkList));
309 cout<<"输入线性链表长度:";
310 cin>>n;
311 Getlist(head,n);
312 Output(head);
313 Count(head);
314 Find(head);
315 cout<<"将线性链表排序如下:";
316 BubbleSort(head);
317 Output(head);
318 Delete(head);
319 cout<<"输出最终线性链队:";
320 Output(head);
321 cout<<"将线性链表转置:";
322 Output(Transpose(head));
323 return 0;
324 }