1 #ifndef QUEUE_H_
2 #define QUEUE_H_
3
4 #include <stdbool.h>
5 #define QUEUEMAX 10
6
7 typedef int Item;
8
9 typedef struct node
10 {
11 Item item;
12 struct node * next;
13 } Node;
14
15 typedef struct queue
16 {
17 Node * front;
18 Node * rear;
19 int length;
20 } Queue;
21
22 void InitializeQueue(Queue * pq);
23
24 bool QueueIsEmpty(const Queue * pq);
25
26 bool QueueIsFull(const Queue * pq);
27
28 int QueueItemCount(const Queue * pq);
29
30 bool EnQueue(Item item, Queue * pq);
31
32 bool DeQueue(Item * pitem, Queue * pq);
33
34 void ShowQueue(const Queue * pq);
35
36 void EmptyTheQueue(Queue * pq);
37 #endif
#include <stdio.h>
#include "queue.h"
void eatline(void);
int main(void)
{
Queue line;
Item temp;
InitializeQueue(&line);
printf("<a. add d. delete s. show q. quit>\n");
char ch;
while('q' != (ch = getchar()))
{
eatline();
if('a' != ch && 'd' != ch && 's' != ch)
{
printf("invalid input!\n");
printf("<a. add d. delete s. show q. quit>\n");
continue;
}
if('a' == ch)
{
if(QueueIsFull(&line))
printf("Queue is full!\n");
else
{
printf("Integer to add: ");
scanf("%d", &temp);
eatline();
int result = EnQueue(temp, &line);
printf("Putting %d into queue <%s>\n", temp, (true == result? "success" : "failed"));
}
}
else if('d' == ch)
{
if(QueueIsEmpty(&line))
printf("No data to delete!\n");
else
{
int result = DeQueue(&temp, &line);
printf("Remove %d from queue <%s>\n", temp, (true == result ? "success" : "failed"));
}
}
else if('s' == ch)
{
if(QueueIsEmpty(&line))
printf("No data to show!\n");
else
{
printf("Here is the queue content:\n");
ShowQueue(&line);
}
}
printf("<a. add d. delete s. show q. quit>\n");
}
EmptyTheQueue(&line);
printf("Bye!\n");
return 0;
}
void eatline(void)
{
while('\n' != getchar())
continue;
}
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
static void CopyToNode(Item item, Node * pn);
static void CopyToItem(Item * pitem, Node * pn);
void InitializeQueue(Queue * pq)
{
pq->front = pq->rear = NULL;
pq->length = 0;
}
bool QueueIsEmpty(const Queue * pq)
{
return pq->length == 0;
}
bool QueueIsFull(const Queue * pq)
{
return pq->length == QUEUEMAX;
}
int QueueItemCount(const Queue * pq)
{
return pq->length;
}
bool EnQueue(Item item, Queue * pq)
{
if(QueueIsFull(pq))
return false;
Node * pn = (Node *)malloc(sizeof(Node));
if(NULL == pn)
{
printf("Unable to allocate memory!\n");
exit(1);
}
CopyToNode(item, pn);
pn->next = NULL;
if(QueueIsEmpty(pq))
pq->front = pn;
else
pq->rear->next = pn;
pq->rear = pn;
pq->length++;
return true;
}
bool DeQueue(Item * pitem, Queue * pq)
{
if(QueueIsEmpty(pq))
return false;
Node * pn;
pn = pq->front;
CopyToItem(pitem, pn);
pq->front = pq->front->next;
free(pn);
pq->length--;
if(0 == pq->length)
pq->rear = NULL;
return true;
}
void ShowQueue(const Queue * pq)
{
Node * pn = pq->front;
printf("[");
while(NULL != pn)
{
printf("%d ", pn->item);
pn = pn->next;
}
printf("]\n");
}
void EmptyTheQueue(Queue * pq)
{
Item dummy;
while(!QueueIsEmpty(pq))
DeQueue(&dummy, pq);
}
static void CopyToNode(Item item, Node * pn)
{
pn->item = item;
}
static void CopyToItem(Item * pitem, Node * pn)
{
*pitem = pn->item;
}