int rear ;//队尾指针
int front;//对头指针
const int MAXQUEUE = 10;//该队列最大能放多少数据
int queue[MAXQUEUE] ;
int InQueue(int value)
{
if(rear>=MAXQUEUE)
return 0;
rear++;//队尾指针
queue[rear]=value;
return 1;
}
int OutQueue(int *value)
{
if(front == rear)
return 0;
front++;
*value = queue[front];
return 1;
}
//用循环队列来存储数据
int InQueue(int value)
{
if(front==-1||rear == MAX_SIZE||rear+1==front)//表示该队列已经放满了
return 0;
if(rear == MAX_SIZE)//队尾指针满了之后从头又放数据
rear=0;
rear++;
queue[rear]=value;
return 1;
}
int OutQueue(int *value)
{
if(rear == front)
return 0;
front++;
if(front==MAX_SIZE)
front=0;
*value = queue[front];
return 1;
}
-------------------------------------------------------------------
//用指针来实现队列
#include<stdio.h>
struct Queue_node{
int data;
Queue_node *next;
};
Queue_node *rear = NULL;
Queue_node *front = NULL;
int InQueue(int value)
{
Queue_node *q = new Queue_node;
if(q==NULL)
return 0;
q->data = value;
q->next=NULL;
if(front == NULL)
{
front = q;
}
else
rear->next = q;//队尾的下一个节点是q
rear = q;
return 1;
}
int OutQueue(int *value)
{
Queue_node *p = NULL;
if(front ==NULL)
p = front;
front = front->next;//队首去掉一个节点
*value = p->data;
return 1;
}
--------------------------------------------------------
双队列存储数据
#include<stdio.h>
#include<stdlib.h>
struct Queue{//输入限制性
int data;
Queue *next;
};
Queue *front = NULL;
Queue *next = NULL;
//输入限制性队列--一边输入两边输出
int InQueue(int value)
{
Queue *q = new Queue;
if(q==0) return 0;
q->data = value;
q->next =NULL;
if(front ==NULL)
front = q;
else rear ->next = q;
return 1;
}
int OutQueue(int *value)
{
Queue * p = NULL;
if(front ==NULL)
return 0;
p = front;
front= front->next;
*value = p->data;
return 1;
}
int OutQueueByrear(int *value);
{
Queue *p =NULL;
if(rear == NULL)
return 0;
if(rear ==front)
{
*value = p->data;
free(rear);
rear =NULL;
front = NULL;
}else
{
p = front;
while(p->next!= rear)
p = p->next;
*value = rear->data;
free(rear);
rear=p;
rear ->next = NULL;
}
return 1;
}
//输出限制性的双队列
struct queue{
int data;
queue *next;
};
int OutQueue(int *value)
{
queue *q = NULL;
if(front==NULL)
return 0;
q = front;
front = front->next;
free(q);
return 1;
}
int InQueueByrear(int *value)
{
queue *p = new queue;
if(p==NULL) return 0;
p->data = value;
p->next= NULL;
if(rear==NULL)
front = p;
else rear->next = p;
rear = p;
return 1;
}
int InQueueByfront(int *value)
{
queue *q = new queue;
if(q==NULL) return 0;
q->data = value;
q->next= front;
front = q;
if(rear ==NULL)
rear = q;
return 1;
}
队列,数组实现,指针实现的源码详细讲解
最新推荐文章于 2023-06-05 13:54:10 发布