Chinaunix首页 | 论坛 | 博客
  • 博客访问: 862379
  • 博文数量: 149
  • 博客积分: 3671
  • 博客等级: 中校
  • 技术积分: 1701
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-03 16:52
文章分类

全部博文(149)

文章存档

2011年(57)

2010年(92)

分类: C/C++

2011-03-21 11:20:00

直接上代码

 

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef struct Node
5 {
6 int data;
7 struct Node* next;
8 }QueueNode;
9
10 typedef struct LinkList
11 {
12 QueueNode* head;
13 QueueNode* rear;
14 }LinkList;
15
16  void initLinkQueue(LinkList* Q)
17 {
18 Q->head=Q->rear=NULL;
19 }
20
21  int isEmpty(LinkList* Q)
22 {
23 return Q->head==NULL&&Q->rear==NULL;
24 }
25
26  void inQueue(LinkList* Q,int item)
27 {
28 QueueNode* pi=(QueueNode*)malloc(sizeof(QueueNode));
29 if(pi==NULL)
30 {
31 printf("分配内存失败!\n");
32 exit(1);
33 }
34 pi->data=item;
35 pi->next=NULL;
36 if(isEmpty(Q))
37 {
38 Q->head=pi;
39 Q->rear=pi;
40 }
41 else
42 {
43 Q->rear->next=pi;
44 Q->rear=pi;
45 }
46 }
47
48
49 int outQueue(LinkList* Q)
50 {
51 QueueNode* pdel;
52 int temp;
53 if(isEmpty(Q))
54 {
55 printf("There is no element!\n");
56 exit(1);
57 }
58 temp=Q->head->data;
59 pdel=Q->head;
60 Q->head=Q->head->next;
61 if(Q->head==NULL)
62 Q->rear=NULL;
63 free(pdel);
64 return temp;
65 }
66
67 int peepQueue(LinkList* Q)
68 {
69 if(Q->head==NULL)
70 {
71 printf("There is no element!\n");
72 exit(1);
73 }
74 return Q->head->data;
75 }
76
77
78 /* 6.清除链队中的所有元素 */
79 void clearQueue(LinkList* Q)
80 {
81 QueueNode* temp=Q->head;
82 while(temp)
83 {
84 Q->head=Q->head->next;
85 free(temp);
86 temp=Q->head;
87 }
88 Q->rear=NULL;
89 }
90
91

 

1 #include "LinkQueue.h"
2
3 int main()
4 {
5 int temp;
6 LinkList* Q=(LinkList*)malloc(sizeof(LinkList));
7 initLinkQueue(Q);
8 inQueue(Q,10);
9 temp=peepQueue(Q);
10 printf("%d\n",temp);
11 temp=isEmpty(Q);
12 printf("isEmpty: %d\n",temp);
13 temp=outQueue(Q);
14 printf("%d\n",temp);
15 temp=isEmpty(Q);
16 printf("isEmpty: %d\n",temp);
17 inQueue(Q,20);
18 inQueue(Q,11);
19 inQueue(Q,19);
20 temp=peepQueue(Q);
21 printf("%d\n",temp);
22 clearQueue(Q);
23 temp=isEmpty(Q);
24 printf("isEmpty: %d\n",temp);
25
26 }

http://www.cnblogs.com/hstcghost/category/237817.html

阅读(1354) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~