Sequence_Cqueue(順序循環隊列)

#include<stdio.h>
#define MAXSIZE 5
typedef int Datatype;
typedef struct {
Datatype a[MAXSIZE];
int front;
int rear;
}sequennce_cqueue;


void init(sequennce_cqueue *sq)
{
sq->front = 0;
sq->rear = 0;
}


int empty(sequennce_cqueue sq)
{
return (sq.front == sq.rear ? 1 : 0);
}


void display(sequennce_cqueue sq)
{
int i;
if (empty == 1)
printf("順序表爲空!\n");
else
{
if (sq.rear > sq.front)
{
for (i = sq.front; i < sq.rear; i++)
printf("%5d", sq.a[i]);
putchar('\n');
}
else
if(sq.rear < sq.front)
{
for (i = sq.front; i < MAXSIZE; i++)
printf("%5d", sq.a[i]);
for (i = 0; i < sq.rear; i++)
printf("%5d", sq.a[i]);
putchar('\n');
}
}
}


Datatype get(sequennce_cqueue sq)//取得隊首的值
{
if (empty == 1)
{
printf("順序表爲空!,無法獲取隊首的值。\n");
return 0;
}
return sq.a[sq.front];
}


void insert(sequennce_cqueue *sq, Datatype x)
{
int i;
if ((sq->rear+1)%MAXSIZE==sq->front)
printf("隊列已滿,無法進行插入!\n");
else
{
sq->a[sq->rear] = x;
sq->rear = (sq->rear + 1) % MAXSIZE;
}
}


void dele(sequennce_cqueue *sq)
{
if (empty == 1)
{
printf("順序表爲空!,無法進行刪除。\n");
}
else
sq->front=(sq->front+1)%MAXSIZE;
}


void main()
{
int i;
sequennce_cqueue SQ;
init(&SQ);
printf("隊列空爲1,非空爲0:%d\n", empty(SQ));
for (i = 0; i < 5; i++)
insert(&SQ, i * 2);
display(SQ);
printf("隊列空爲1,非空爲0:%d\n", empty(SQ));
printf("首部的元素爲:%d\n", get(SQ));
dele(&SQ);
display(SQ);
printf("首部的元素爲:%d\n", get(SQ));
insert(&SQ, 666);
display(SQ);
printf("首部的元素爲:%d\n", get(SQ));
}
發佈了46 篇原創文章 · 獲贊 16 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章