数据结构 队列的相关操作

用单链表实现的队列,带一个头节点

实现简单的功能,程序中可能含有bug;

 

  1 //this program used for basic operation about queue  using list
  2 // this list has a head node
  3 //
  4 //Author:zxJay
  5 //
  6 
  7 #include<stdio.h>
  8 #include<stdlib.h>
  9 
 10 #define OK      0
 11 #define ERROR   -1
 12 //define the structure node
 13 typedef struct queue
 14 {
 15         int value;
 16         struct queue *next;
 17 }Queue;
 18 
 19 // front point to head node
 20 typedef struct linkqueue
 21 {
 22         Queue *front;
 23         Queue *tail;
 24 }LinkQueue;
 25 
 26 #define QUEUE_LEN  sizeof(Queue)
 27 //init a queue
 28 
 29 int initQueue(LinkQueue *);
 30 
 31 //whether the queue is empty or not
 32 int isEmptyQueue(LinkQueue *);
 33 
 34 //get head value
 35 
 36 int getHeadValue(LinkQueue *);
 37 
 38 //enter a value  into queue
 39 int enterQueue(LinkQueue *,int value);
 40 
 41 //pop a vaue from the queue
 42 int deQueue(LinkQueue *);
 43 
 44 //destory the Queue
 45 int destoryQueue(LinkQueue *);
 46 
 47 //Clear the Queue
 48 
 49 int clearQueue();
 50 
 51 //return len of the Queue
 52 
 53 int lenQueue(LinkQueue *);
 54 
 55 //printf the Queue
 56 int printQueue(LinkQueue *);
 57 
 58 int main()
 59 {
 60         int i = 0;
 61         int nodeNum = 0;
 62         int nodeValue = 0;
 63         LinkQueue Q;
 64         initQueue(&Q);
 65         printf("input number of Node:\n");
 66         scanf("%d",&nodeNum);
 67         printf("please put into node values:\n");
 68         for(i=0;i<nodeNum;i++)
 69         {
 70                 scanf("%d",&nodeValue);
 71                 enterQueue(&Q,nodeValue);
 72         }
 73         printQueue(&Q);
 74         printf("The head node value:%d\n",getHeadValue(&Q));
 75         printf("The len of Queue:%d\n",lenQueue(&Q));
 76         printf("The first value to pop:%d\n",deQueue(&Q));
 77         printf("The len of Queue after pop:%d\n",lenQueue(&Q));
 78         printf("Destory Queue !\n");
 79         destoryQueue(&Q);
 80         return 0;
 81 }
 82 
 83 int initQueue(LinkQueue *Q)
 84 {
 85         printf("Init a queue ......\n");
 86         if(!(Q->front = Q->tail = (Queue *)malloc(QUEUE_LEN)))
 87         {
 88                 printf("ERROR:Malloc Error !\n");
 89                 return ERROR;
 90         }
 91         Q->front->next = NULL;
 92         return OK;
 93 }
 94 
 95 int enterQueue(LinkQueue *Q,int enterValue)
 96 {
 97         Queue *tmpNode = NULL;
 98         if(!(tmpNode = (Queue*)malloc(QUEUE_LEN)))
 99         {
100                 printf("ERROR:Malloc Error !\n");
101                 return ERROR;
102         }
103 
104         tmpNode->value = enterValue;
105         tmpNode->next = NULL;
106         Q->tail->next = tmpNode;
107         Q->tail = tmpNode;
108 
109 }
110 
111 
112 int isEmptyQueue(LinkQueue *Q)
113 {
114         return  (Q->front->next == NULL);
115 }
116 
117 
118 
119 int printQueue(LinkQueue *Q)
120 {
121         Queue *tmpNode = NULL;
122         tmpNode = Q->front->next;
123         if(NULL == tmpNode)
124         {
125                 printf("ERROR:Queue is NULL\n");
126                 return ERROR;
127 
128         }
129 
130         while(tmpNode != Q->tail)
131         {
132                 printf("%d  ",tmpNode->value);
133                 tmpNode = tmpNode->next;
134         }
135         printf("%d\n",Q->tail->value);
136 
137 }
138 
139 
140 int getHeadValue(LinkQueue *Q)
141 {
142         if(isEmptyQueue(Q))
143         {
144                 printf("ERROR:The Queue is empty !\n");
145                 return ERROR;
146         }
147         return Q->front->next->value;
148 
149 }
150 
151 int lenQueue(LinkQueue *Q)
152 {
153         int len = 0;
154         if(isEmptyQueue(Q))
155         {
156                 return 0;
157         }
158         Queue *tmpNode = Q->front;
159         while(tmpNode != Q->tail)
160         {
161                 len++;
162                 tmpNode = tmpNode->next;
163         }
164         return len;
165 
166 }
167 
168 
169 int deQueue(LinkQueue *Q)
170 {
171         if(isEmptyQueue(Q))
172         {
173                 printf("ERROR:The Queue is empty !\n");
174                 return ERROR;
175         }
176         int value;
177         value = Q->front->next->value;
178         Q->front->next = Q->front->next->next;
179         return value;
180 }
181 
182 
183 
184 int destoryQueue(LinkQueue  *Q)
185 {
186         Queue *tmpNode = NULL;
187         if(isEmptyQueue(Q))
188         {
189                 free(Q->front);
190                 return OK;
191         }
192         while(Q->front != Q->tail)
193         {
194                 tmpNode = Q->front->next;
195                 free(Q->front);
196                 Q->front = tmpNode;
197 
198         }
199         free(Q->front);
200         return OK;
201 
202 }
View Code

 

posted @ 2013-11-23 14:29  zxjay  阅读(572)  评论(0编辑  收藏  举报