队列是一种先进先出的数据结构。
1、用数组实现循环队列,用Q[N]来实现一个最多容纳n-1个元素的队列。Q.head指向队列头元素,Q.tail指向下个新元素要插入的位置
1)Q.head==Q.tail 时,队列为空
2)Q.head==Q.tail+1 时,队列满
#include <stdio.h>
#include <assert.h>
//#include <stdlib.h>
#define max 5
#define type int
/*typedef struct queue{
type data[max];
struct queue * head;
struct queue * tail;
}queue;*/
static type queue[max];
int head;
int tail;
void queue_insert(type);
void queue_delete();
int isfull();
int isempty();
void queue_print();
int main()
{
head=tail=0;
queue_insert(1);
queue_insert(2);
queue_insert(3);
queue_insert(4);
queue_insert(5);
queue_print();
printf("-------------\n");
queue_delete();
queue_delete();
queue_print();
printf("--------------\n");
return 0;
}
int isempty()
{
return head==tail?1:0 ;
}
int isfull()
{
return (tail+1)%max==head ?1:0;
}
void queue_insert(type value)
{
//assert(!isfull());
if(!isfull()){
queue[tail]=value;
tail=(tail+1)%max;
//printf("%d\n",value);
}
else
perror("insert faiture!\n");
}
void queue_print()
{
int i;
assert(!isempty());
//for(i=head;(i)%max!=tail;i++)//tail指向空位置 i++错误,可能引起越界
for(i=head;(i)%max!=tail;i=(i+1)%max)
printf("%d%c",queue[i],(i+1)%max==tail?'\n':' ');
}
void queue_delete()
{
assert(!isempty());
head=(head+1)%max;
}
2、动态数组实现队列
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#define type int
static type *queue;
static int size;
static int head;
static int tail;
int isempty();
int isfull();
void creat_queue();
void queue_insert(type value);
void queue_delete();
void queue_print();
int main()
{
head=tail=0;
creat_queue();
queue_insert(1);
queue_insert(2);
queue_insert(3);
queue_insert(4);
queue_insert(5);
queue_print();
printf("----------------------\n");
queue_delete();
queue_delete();
queue_print();
printf("----------------------\n");
queue_insert(3);
//queue_insert(4);
queue_insert(5);
queue_print();
printf("----------------------\n");
return 0;
}
int isempty()
{
return head==tail?1:0;
}
int isfull()
{
return (tail+1)%size==head?1:0 ;
}
void queue_insert(type value)
{
if(!isfull()){
queue[tail]=value;
printf("insert %d successfully\n",queue[tail]);
tail=(tail+1)%size;
}
else
perror("queue is full\n");
}
void queue_print()
{
int i;
for(i=head;(i%size)!=tail;i=(i+1)%size)
printf("%d%c",queue[i],(i+1)%size==tail?'\n':' ');
}
void creat_queue()
{
printf("please input the size of queue:\n");
scanf_s("%d",&size);
queue=(type*)malloc(sizeof(type)*size);
assert(queue!=NULL);
}
void queue_delete()
{
assert(!isempty());
head=(head+1)%size;
}
3、链表实现队列
对于链表实现的队列,插入元素用 尾插法,删除元素在头部删除。
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define type int
static int count=0;
typedef struct qnode{
type data;
struct qnode * next;
}qnode;
struct qnode * head=0;
struct qnode * tail=0;
int isempty();
int isfull();
void queue_insert(type);
void queue_delete();
void destroy();
void queue_print();
int main()
{
queue_insert(1);
queue_insert(2);
queue_insert(3);
queue_insert(4);
queue_print();
printf("------------\n");
queue_delete();
queue_delete();
queue_print();
printf("------------\n");
queue_insert(1);
queue_insert(2);
queue_insert(3);
queue_insert(4);
queue_print();
destroy();
return 0;
}
void queue_insert(type value)
{
struct qnode* cur;
if(!isfull()){
cur=(qnode *)malloc(sizeof(struct qnode));
assert(cur!=NULL);
cur->data=value;
if(head==NULL){
head=cur;
tail=cur;
}
else{
tail->next=cur;
tail=cur;
}
count++;
}
}
int isfull()
{
return 0;
}
int isempty()
{
return head==NULL?1:0;
}
void queue_print()
{
struct qnode* cur;
assert(!isempty());
for(cur=head;cur!=tail;cur=cur->next)
printf("%d ",cur->data);
printf("%d\n",cur->data);
}
void queue_delete()
{
struct qnode * cur;
if(!isempty()){
if(head==tail){
head=tail=NULL;
return ;
}
cur=head;
head=cur->next;
free(cur);
}
}
void destroy()
{
struct qnode* cur;
struct qnode* del;
assert(!isempty());
for(del=head;del!=tail;del=cur){
cur=del->next;
queue_delete();
}
if(del==tail)
queue_delete();
}