#include
#include"sqqueue.h"
int main(int argc, const char *argv[])
{
psqqueue queue=create_sqqueue();
add_sqqueue(queue,1);
add_sqqueue(queue,2);
add_sqqueue(queue,3);
add_sqqueue(queue,4);
printf("当前队列为\n");
look_sqqueue(queue);
printf("出队 ");
int p=mv_sqqueue(queue);
printf("%d \n",p);
printf("当前队列为\n");
look_sqqueue(queue);
len_sqqueue(queue);
return 0;
}
#include
#include
#include"sqqueue.h"
psqqueue create_sqqueue()
{
psqqueue queue=(psqqueue)malloc(sizeof(sqqueue));
queue->foot=0;
queue->head=0;
return queue;
}
psqqueue add_sqqueue(psqqueue queue,datatype num)
{
queue->data[queue->head]=num;
if((queue->head+1)%N==queue->foot)
{
printf("队列满了");
return NULL;
}
queue->head=(queue->head+1)%N;
return NULL;
}
datatype mv_sqqueue(psqqueue queue)
{
if(queue->foot==queue->head)
{
printf("队列是空的");
return (datatype)-1;
}
datatype num=queue->data[queue->foot];
queue->foot=(queue->foot+1)%N;
return num;
}
void look_sqqueue(psqqueue queue)
{
if(queue->foot==queue->head)
{
printf("队列是空的");
return;
}
int x=queue->foot;
while(x%N!=queue->head)
{
printf("%d ",queue->data[x]);
x=(x+1)%N;
}
printf("\n");
return;
}
void len_sqqueue(psqqueue queue)
{
if(queue->foot==queue->head)
{
printf("队列长度为0\n");
return;
}
if(queue->head>queue->foot)
{
printf("队列长度为%d\n",queue->head-queue->foot);
return;
}
if(queue->head
{
printf("队列长度为%d\n",N-queue->foot+queue->head);
}
}
#ifndef __SQQUEUE_H__
#define __SQQUEUE_H__
#define N 5
typedef int datatype;
typedef struct xx
{
datatype data[N];
int head;
int foot;
}sqqueue,*psqqueue;
psqqueue create_sqqueue();//创建空队列
psqqueue add_sqqueue(psqqueue queue,datatype num);//入队
datatype mv_sqqueue(psqqueue queue);//出队
void look_sqqueue(psqqueue queue);//遍历
void len_sqqueue(psqqueue queue);//队列长度
#endif
#include
#include"linkqueue.h"
int main(int argc, const char *argv[])
{
plinkpos pos=create_linkqueue();
add_linkqueue(pos,1);
add_linkqueue(pos,2);
add_linkqueue(pos,3);
look_linkqueue(pos);
rm_linkqueue(pos);
look_linkqueue(pos);
return 0;
}
#ifndef __LINKQUEUE_H__
#define __LINKQUEUE_H__
typedef int datatype;
union msg
{
int len;
datatype num;
};
typedef struct queue
{
union msg text;
struct queue * last;
}linkqueue,*plinkqueue;
typedef struct a{
plinkqueue head;
plinkqueue foot;
}linkpos,*plinkpos;
plinkpos create_linkqueue();//创建队列
void add_linkqueue(plinkpos pos,datatype num);//入队
datatype rm_linkqueue(plinkpos head);//出队
void look_linkqueue(plinkpos head);//遍历
#endif
#include
#include
#include"linkqueue.h"
plinkpos create_linkqueue()
{
plinkpos pos=(plinkpos)malloc(sizeof(linkpos));
pos->head=(plinkqueue)malloc(sizeof(linkqueue));
pos->head->text.len=0;
pos->head->last=NULL;
pos->foot=pos->head;
return pos;
}
void add_linkqueue(plinkpos pos,datatype num)
{
plinkqueue temp=(plinkqueue)malloc(sizeof(linkqueue));
temp->last=NULL;
temp->text.num=num;
temp->last=pos->foot->last;
pos->foot->last=temp;
pos->foot=pos->foot->last;
pos->head->text.len++;
return;
}
datatype rm_linkqueue(plinkpos pos)
{
if(pos->foot==NULL)
{
printf("队列为空无法删除");
return (datatype)-1;
}
datatype T;
plinkqueue temp=pos->head->last;
T=pos->head->last->text.num;
pos->head->last=temp->last;
pos->head->text.len--;
return T;
}
void look_linkqueue(plinkpos pos)
{
if(pos->head->last==NULL)
{
printf("队列是空的");
return;
}
plinkqueue c=pos->head;
while(c->last!=NULL)
{ c=c->last;
printf("%d ",c->text.num);
}
printf("\n");
return;
}