#include <stdio.h>
#include <stdlib.h>
#define ERROR -1111
typedef int ElementType;
typedef struct Node* PtrToNode;
struct Node
{
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode Position;
typedef struct QNode* PtrToQNode;
Position Front,Rear; //队列的头尾指针
int Maxsize; //队列最大容量
};
bool IsEmpty(Queue Q)
{
return (Q->Front==NULL); //队列是否为空
}
ElementType Delete(Queue Q)
{
Position FrontCell;
ElementType FrontElem;
if(IsEmpty(Q))
{
printf("the queue is empty\n");
return ERROR;
}
else
{
FrontCell=Q->Front;
if(Q->Front==Q->Rear) //若队列只有一个元素
Q->Front=Q->Rear=NULL; //删除后队列位置为空
else
Q->Front=Q->Front->Next;
FrontElem=FrontCell->Data;
free(FrontCell);
return FrontElem;
}
#include <stdlib.h>
#define ERROR -1111
typedef int ElementType;
typedef struct Node* PtrToNode;
struct Node
{
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode Position;
typedef struct QNode* PtrToQNode;
struct QNode
{Position Front,Rear; //队列的头尾指针
int Maxsize; //队列最大容量
};
typedef PtrToQNode Queue;
bool IsEmpty(Queue Q)
{
return (Q->Front==NULL); //队列是否为空
}
ElementType Delete(Queue Q)
{
Position FrontCell;
ElementType FrontElem;
if(IsEmpty(Q))
{
printf("the queue is empty\n");
return ERROR;
}
else
{
FrontCell=Q->Front;
if(Q->Front==Q->Rear) //若队列只有一个元素
Q->Front=Q->Rear=NULL; //删除后队列位置为空
else
Q->Front=Q->Front->Next;
FrontElem=FrontCell->Data;
free(FrontCell);
return FrontElem;
}
}
//入队
bool AddQ(Queue Q,ElementType X)
{
Node *temp=(Node *)malloc(sizeof(Node));
temp->Data=X;
Q->Rear->Next=temp;
Q->Rear=temp;
Q->Rear->Next=NULL;
return true;
}