#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_
#define SUCCESS 100
#define FAILURE 101
typedef int DataType;
struct node
{
DataType data;
struct node *next;
};
typedef struct node Node;
struct queue
{
Node *front;
Node *rear;
};
typedef struct queue Q;
int InitQueue(Q **q);
int EnterQueue(Q *q, DataType e);
int EmptyQueue(Q *q);
int LengthQueue(Q *q);
int ClearQueue(Q *q);
DataType GetFront(Q *q);
int TraverseQueue(Q *q, void(*p)(DataType));
DataType DeleteQueue(Q *q);
int DestroyQueue(Q **q);
#endif
#include <stdio.h>
#include "LinkQueue.h"
void visit(DataType e)
{
printf("%d ", e);
}
int main()
{
Q *queue;
int ret, i;
ret = InitQueue(&queue);
if(FAILURE == ret)
{
printf("Init FAilure!\n");
}
else
{
printf("Init Success!\n");
}
for (i = 0; i < 5; i++)
{
ret = EnterQueue(queue, i + 1);
if(FAILURE == ret)
{
printf("Enter Queue Failure!\n");
}
else
{
printf("Enter Queue %d Success!\n", i + 1);
}
}
ret = EmptyQueue(queue);
if (FAILURE == ret)
{
printf("LinkQueue is not Empty!\n");
}
else
{
printf("LinkQueue is Empty!\n");
}
printf("LinkQueue length is %d\n", LengthQueue(queue));
ret = GetFront(queue);
if(FAILURE == ret)
{
printf("Getfront failure!\n");
}
else
{
printf("Getfront is %d\n", ret);
}
ret = TraverseQueue(queue, visit);
if (FAILURE == ret)
{
printf("\nTraverse Failure!\n");
}
else
{
printf("\nTraverse Success!\n");
}
ret = ClearQueue(queue);
if (FAILURE == ret)
{
printf("Clear Failure!\n");
}
else
{
printf("Clear Success!\n");
}
ret = DestroyQueue(&queue);
if (FAILURE == ret)
{
printf("Destory Failure!\n");
}
else
{
printf("Destory Success!\n");
}
return 0;
}
#include <stdio.h>
#include "LinkQueue.h"
#include <stdlib.h>
int InitQueue(Q **q)
{
(*q) = (Q*)malloc(sizeof(Q));
if(NULL == (*q))
{
return FAILURE;
}
Node *p = (Node*)malloc(sizeof(Node));
if (NULL == p)
{
return FAILURE;
}
p->next = NULL;
(*q)->front = (*q)->rear = p;
return SUCCESS;
}
int EnterQueue(Q *q, DataType e)//进栈,从队尾进
{
if(NULL == q)
{
return FAILURE;
}
Node*r = (Node *)malloc(sizeof(Node));
if (NULL == r)
{
return FAILURE;
}
r->data = e;
r->next = NULL;
q->rear->next = r;
q->rear = r;
return SUCCESS;
}
int EmptyQueue(Q *q)
{
if (NULL == q)
{
return SUCCESS;
}
return (q->front == q->rear) ? SUCCESS : FAILURE ;
}
int LengthQueue(Q *q)
{
int count = 0;
if(NULL == q)
{
return FAILURE;
}
Node * p = q->front->next;
while(p)
{
count++;
p = p->next;
}
return count;
}
DataType GetFront(Q *q)
{
if (NULL == q || q->rear == q->front)
{
return FAILURE;
}
return q->front->next->data;
}
int TraverseQueue(Q *q, void(*p)(DataType))
{
if(NULL == q)
{
return FAILURE;
}
Node *r = q->front->next;
while(r)
{
p(r->data);
r = r->next;
}
return SUCCESS;
}
DataType DeleteQueue(Q *q)
{
DataType e;
if (NULL == q || q->front == q->rear)
{
return FAILURE;
}
Node *p = q->front->next;
e = p->data;
q->front->next = p->next;
if (p == q->rear)
{
q->front = q->rear;
}
free(p);
return e;
}
int ClearQueue(Q *q)
{
if (NULL == q)
{
return FAILURE;
}
Node *p = q->front->next;
while(p)
{
q->front->next = p->next;
free(p);
p = q->front->next;
}
q->rear = q->front;
return SUCCESS;
}
int DestroyQueue(Q **q)
{
if (NULL == q)
{
return FAILURE;
}
free((*q)->front);
free(*q);
*q = NULL;
return SUCCESS;
}