队列(顺序存储)
#define MAXSIZE 50
typedef struct {
int value[MAXSIZE];
int rear;
int front;
}Queue;
Queue *CreateQueue(){
Queue *queue;
queue=(Queue *)malloc(sizeof(Queue));
queue->front=0;
queue->rear=0;
return queue;
}
void AddQueue(Queue *queue,int value){
if((queue->rear+1)%MAXSIZE==queue->front){
printf("queue is full");
return;
}else {
queue->rear=(queue->rear+1)%MAXSIZE;
queue->value[queue->rear] = value;
}
}
int IsEmpty(Queue *queue){
if(queue->rear==queue->front)return 1;
else return 0;
}
void OutQueue(Queue* queue,int *value){
if(IsEmpty(queue)){
printf("Queue is empty");
return;
}else{
queue->front=(queue->front+1)%MAXSIZE;
*value=queue->value[queue->front];
}
}
队列(链式存储)注意
typedef struct Node {
int value;
struct Node *next;
} QNode;
typedef struct {
QNode *rear;
QNode *front;
} Queue;
void InitQueue(Queue **queue) {
QNode *p;
p = (QNode *) malloc(sizeof(QNode));
p->next = NULL;
(*queue)->front = p;
(*queue)->rear = p;
}
void InQueue(Queue *queue, int value) {
QNode *InElement;
InElement = (QNode *) malloc(sizeof(QNode));
InElement->value = value;
InElement->next = NULL;
queue->rear->next = InElement;
queue->rear = InElement;
}
int IsEmpty(Queue *queue) {
if (queue->rear = queue->front)return 1;
else return 0;
}
void OutQueue(Queue *queue, int *value) {
QNode *OutElement;
if (IsEmpty(queue)) {
printf("queue is empty");
return;
} else {
OutElement = queue->front->next;
queue->front->next=OutElement->next;//指针头结点指向下一个结点(pspspspsps)
*value=OutElement->value;
free(OutElement);
if(IsEmpty(queue)){//出队列后如果队列为空则置为空队列
queue->front=queue->rear;
}
}
}
更多关于java的文章请戳这里:(您的留言意见是对我最大的支持)
我的文章列表
Email:sxh13208803520@gmail.com