/*
**********************************************************
*FileName:queue.h
*Function:
*Version:version_1
*Author:yuanweikang
*Date:2015/7/30
*Note:
**********************************************************
*/
#ifndef QUEUE_H
#define QUEUE_H
#define Status int
#define TRUE 1
#define FALSE 0
#define DATATYPE int
typedef struct node
{
DATATYPE data;
struct node *next;
} Node;
typedef struct
{
Node *front;
Node *rear;
} Queue;
Node *CreateNode(DATATYPE data);
Queue *CreateQueue(DATATYPE data);
Status EnQueue(Queue *queue, DATATYPE data);
Status DeQueue(Queue *queue, DATATYPE *data);
int LengthQueue(Queue *queue);
Status ClearQueue(Queue *queue);
Status DestroyQueue(Queue **queue);
Status PrintQueue(Queue *queue);
Status GetFrontQueue(Queue *queue, DATATYPE *data);
Status GetRearQueue(Queue *queue, DATATYPE *data);
#endif
/*
**********************************************************
*FileName:queue.c
*Function:
*Version:version_1
*Author:yuanweikang
*Date:2015/7/30
*Note:
**********************************************************
*/
#include
#include
#include "queue.h"
Node *CreateNode(DATATYPE data)
{
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL)
{
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
Queue *CreateQueue(DATATYPE data)
{
Node *newNode = CreateNode(data);
if (newNode == NULL)
{
return NULL;
}
Queue *newQueue = (Queue *)malloc(sizeof(Queue));
if (newQueue == NULL)
{
/*空间分配失败后,退出之前需要释放已经分配的空间*/
free(newNode);
newNode = NULL;
return NULL;
}
newQueue->front = newQueue->rear = newNode;
newQueue->front->next = NULL;
return newQueue;
}
Status EnQueue(Queue *queue, DATATYPE data)
{
if (queue == NULL)
{
return FALSE;
}
Node *newNode = CreateNode(data);
if (newNode == NULL)
{
return FALSE;
}
queue->rear->next = newNode;
queue->rear = newNode; // 队尾指针重新指向最末
return TRUE;
}
Status DeQueue(Queue *queue, DATATYPE *data)
{
if (queue == NULL)
{
return FALSE;
}
if (queue->front == queue->rear)
{
return FALSE;
}
Node *oldNode = queue->front->next;
queue->front->next = oldNode->next;
*data = oldNode->data;
if (oldNode == queue->rear)
{
queue->rear = queue->front;
}
free(oldNode);
oldNode = NULL;
return TRUE;
}
int LengthQueue(Queue *queue)
{
if (queue == NULL)
{
return -1;
}
int length = 0;
Node *tempNode = queue->front->next;
while (tempNode != NULL)
{
tempNode = tempNode->next;
length++;
}
return length;
}
Status ClearQueue(Queue *queue)
{
if (queue == NULL)
{
return FALSE;
}
Node *oldNode = queue->front->next;
while (oldNode != NULL)
{
queue->front->next = oldNode->next;
if (oldNode == queue->rear)
{
queue->rear = queue->front;
}
free(oldNode);
oldNode = NULL;
oldNode = queue->front->next;
}
return TRUE;
}
Status DestroyQueue(Queue **queue)
{
if (queue == NULL)
{
return FALSE;
}
if (ClearQueue(*queue) == FALSE)
{
return FALSE;
}
free((*queue)->front);
(*queue)->front = NULL;
free(*queue);
*queue = NULL;
queue = NULL;
return TRUE;
}
Status PrintQueue(Queue *queue)
{
if (queue == NULL)
{
return FALSE;
}
Node *tempNode = queue->front->next;
printf("Queue: ");
while (tempNode != NULL)
{
printf("%d ", tempNode->data);
tempNode = tempNode->next;
}
printf("\n");
return TRUE;
}
Status GetFrontQueue(Queue *queue, DATATYPE *data)
{
if (queue == NULL)
{
return FALSE;
}
if (queue->front == queue->rear)
{
return FALSE;
}
*data = queue->front->next->data;
return TRUE;
}
Status GetRearQueue(Queue *queue, DATATYPE *data)
{
if (queue == NULL)
{
return FALSE;
}
if (queue->front == queue->rear)
{
return FALSE;
}
*data = queue->rear->data;
return TRUE;
}
/*
**********************************************************
*FileName:main.c
*Function:
*Version:version_1
*Author:yuanweikang
*Date:2015/7/30
*Note:
**********************************************************
*/
#include
#include "queue.h"
int main(int argc, char *argv[])
{
int i = 0;
DATATYPE data = 0;
Queue *queue = CreateQueue(0);
if (queue == NULL)
{
printf("create queue fail\n");
return 1;
}
for (i=1; i<=5; i++)
{
if (EnQueue(queue, i) == FALSE)
{
printf("enqueue fail\n");
return 1;
}
}
if (PrintQueue(queue) == FALSE)
{
printf("print queue fail\n");
return 1;
}
if (DeQueue(queue, &data) == FALSE)
{
printf("dequeue fail\n");
return 1;
}
printf("equeue data=%d\n", data);
if (PrintQueue(queue) == FALSE)
{
printf("after dequeue print queue fail\n");
return 1;
}
if (GetFrontQueue(queue, &data) == FALSE)
{
printf("get front queue fail\n");
return 1;
}
printf("front queue data=%d\n", data);
if (GetRearQueue(queue, &data) == FALSE)
{
printf("get rear queue fail\n");
return 1;
}
printf("rear queue data=%d\n", data);
if (ClearQueue(queue) == FALSE)
{
printf("clear queue fail\n");
return 1;
}
if (PrintQueue(queue) == FALSE)
{
printf("after clear queue print queue fail\n");
return 1;
}
if (DestroyQueue(&queue) == FALSE)
{
printf("destroy queue fail\n");
return 1;
}
return 0;
}