My blog 队列
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#define ll long long
#define queue_size 100
#define queueincrement 10
#define Qelemtype int
#define Status int
#define False 0
#define OK 1
using namespace std;
typedef struct Qnode
{
Qelemtype data;
struct Qnode *next;
}Qnode,*QueuePtr;
//*Queueptr是指定义了一个可以指向结构体Qnode的指针,
typedef struct {
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}sq_queue;
//初始化
Status Init_queue(sq_queue *Q)
{
Q->front= Q->rear = (QueuePtr)malloc(sizeof(Qnode)); //申请空间
if(Q->front == NULL) return False;
Q->front->next=NULL;
return OK;
}
//销毁
Status Destory_queue(sq_queue *Q)
{
while(Q->front)//画图理解。
{
Q->rear=Q->front->next;
free(Q->front);
Q->front=Q->rear;
}
return OK;
}
//判断是否为空
Status Empty_queue(sq_queue Q)
{
if(Q.front==Q.rear)
return OK;
else return False;
}
//清空
Status Clear_queue(sq_queue *Q)
{
if(Empty_queue(*Q)) return OK;//空队列就不需要清空了
QueuePtr pre,p;
p=Q->front->next;//头
Q->front->next=NULL;
Q->rear=Q->front;//移动尾指针到头指针处 利用p 进行队列头指针之后的结点删除
while(p)
{
pre=p->next;
free(p);
p=pre;
}
return OK;
}
//队列的长度
Status Length_queue(sq_queue Q)
{
int len=0;
if(!Empty_queue(Q))
{
QueuePtr s=(QueuePtr)malloc(sizeof(Qnode));
s=Q.front->next;
while(s)
{
s=s->next;
len++;
}
return len;
}else return False;
}
//队头元素
Status Gethead_queue(sq_queue Q,Qelemtype *e)
{
if(!Empty_queue(Q))
{
*e=(Q.front->next->data);
return OK;
}else return False;
}
//尾进
Status Insert_queue(sq_queue *Q,Qelemtype e)
{
QueuePtr s=(QueuePtr)malloc(sizeof(Qnode));
if(!s) return False;//内存分配失败
s->data=e;
s->next=NULL;
Q->rear->next=s;//把新结点插入队尾
Q->rear=s;//把新结点设为队尾结点
return OK;
}
//头出 并返回其值
Status Delete_queue(sq_queue *Q,Qelemtype *e)
{
if(!Empty_queue(*Q))
{
QueuePtr p=Q->front->next;//p指向要出队的结点
*e=p->data;//值
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;//队头就是队尾 则删除后队尾指向队头
free(p);
}else return False;
}
//遍历
void Traverse_queue(sq_queue Q)
{
QueuePtr s;
s=Q.front->next;
while(s!=NULL)
{
cout<< s->data <<" ";
s=s->next;
}
cout<<endl;
}
int main()
{
sq_queue Q;
Qelemtype e;
if(!Init_queue(&Q))//分配内存失败
{
cout<<"Memory allocation failed!"<<endl;
return 0;
}
else{
for(int i=1;i<=20;i++)
{
int a=rand()%10+1;
Insert_queue(&Q,a);
}
cout<<"Length of queue: ";
cout<<Length_queue(Q)<<endl;
cout<<"Queue: ";
Traverse_queue(Q);//验证
cout<<"The number of front: ";
Gethead_queue(Q,&e);//头
cout<<e<<endl;
//出
int flag=1;
char c;
while(flag)
{
cout<<"Pop_front: ";
if(Delete_queue(&Q,&e))
cout<<e<<endl;
else {
cout<<"Error"<<endl;
break;
}
cout<<"Continuing??(Y or N) ";
cin>>c;
if(c=='Y'||c=='y') flag=1;
else flag=0;
}
//进
int flag1=1;
char c1;
while(flag1)
{
cout<<"Input number you push_rear: ";
cin>>e;
Insert_queue(&Q,e);
cout<<"After pushing: ";
Traverse_queue(Q);
cout<<"continuing??(Y or N) ";
cin>>c1;
if(c1=='Y'||c1=='y') flag1=1;
else flag1=0;
}
cout<<"Clear…i…n…g…"<<endl;
//清空
Clear_queue(&Q);
if(Empty_queue(Q)) cout<<"Clear accomplished..."<<endl;
else cout<<"Clear failed"<<endl;
//销毁
if(Destory_queue(&Q)) cout<<"Destory accomplished..."<<endl;
}
return 0;
}
/*如果你使用的变量x是个结构体,应该用.访问其成员,如:x.num
如果你使用的变量x是个结构体指针,应该用->访问其成员,如:x->num*/
循环队列
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#define ll long long
#define Max_size 10
#define Qelemtype int
#define Status int
#define False 0
#define OK 1
using namespace std;
typedef struct
{
Qelemtype *data;
int front,rear;//头、尾。 如果队列不满的话,尾指向队列尾元素的下一个位置~
}sq_queue;
//初始化
Status Init_queue(sq_queue *Q)
{
Q->data = (Qelemtype *)malloc(Max_size*sizeof(Qelemtype));
if(Q->data==NULL) return False;
Q->front=0;
Q->rear=0;
return OK;
}
//销毁
Status Destory_queue(sq_queue *Q)
{
if(Q->data) free(Q->data);//malloc free 配套使用
Q->data=NULL;
Q->front=Q->rear=0;
return OK;
}
//清空
Status Clear_queue(sq_queue *Q)
{
Q->front=Q->rear=0;
return OK;
}
//判断是否为空
Status Empty_queue(sq_queue Q)
{
if(Q.rear==Q.front)
return OK;
return False;
}
//长度
Status Length_queue(sq_queue Q)
{
return (Q.rear-Q.front+Max_size)%Max_size;
}
//队头元素
Status Gethead_queue(sq_queue Q,Qelemtype *e)
{
if(!Empty_queue(Q))
{
*e=Q.data[Q.front];
return OK;
}else return False;
}
//在队尾插入元素
Status Insert_queue(sq_queue *Q,Qelemtype e)
{
if((Q->rear+1)%Max_size==Q->front)//满
{
return False;
}
Q->data[Q->rear]=e;
Q->rear=(Q->rear+1)%Max_size;//注意下一个位置。
return OK;
}
//队首删除
Status Delete_queue(sq_queue *Q,Qelemtype *e)
{
if(Empty_queue(*Q))
return False;
*e=Q->data[Q->front];
Q->front=(Q->front+1)%Max_size;
return OK;
}
//遍历
void Traverse_queue(sq_queue Q)
{
if(Empty_queue(Q))
cout<<"error"<<endl;
else
{
for(int i=Q.front;i<Q.rear;i++)
{
cout<<Q.data[i]<<" ";
}
cout<<endl;
}
}
int main()
{
Qelemtype e;
sq_queue Q;
if(!Init_queue(&Q))//分配内存失败
{
cout<<"Memory allocation failed!"<<endl;
return 0;
}
else
{
for(int i=1;i<=5;i++)//Max_size 为10 当 数据数量为 9 时 即 full
{
int a=rand()%10+1;
Insert_queue(&Q,a);
}
cout<<"Length of queue: ";
cout<<Length_queue(Q)<<endl;
cout<<"Queue: ";
Traverse_queue(Q);//检验队列
cout<<"The head element: ";
Gethead_queue(Q,&e);
cout<<e<<endl;
//插入 进
int flag1=1;
char c1;
while(flag1)
{
cout<<"Input number you push_rear: ";
cin>>e;
if(Insert_queue(&Q,e))
{
cout<<"After pushing: ";
Traverse_queue(Q);
}else
{
cout<<"The queue is full~"<<endl;
break;
}
cout<<"Continuing??(Y or N) ";
cin>>c1;
if(c1=='Y'||c1=='y') flag1=1;
else flag1=0;
}
//删除 出
int flag=1;
char c;
while(flag)
{
cout<<"Pop_front: ";
if(Delete_queue(&Q,&e))
cout<<e<<endl;
else {
cout<<"None"<<endl;
break;
}
cout<<"Continuing??(Y or N) ";
cin>>c;
if(c=='Y'||c=='y') flag=1;
else flag=0;
}
cout<<"Clear…i…n…g…"<<endl;
//清空
Clear_queue(&Q);
if(Empty_queue(Q)) cout<<"Clear accomplished..."<<endl;
else cout<<"Clear failed"<<endl;
//销毁
if(Destory_queue(&Q)) cout<<"Destory accomplished..."<<endl;
}
return 0;
}