循环队列

 参见:http://www.cnblogs.com/emanlee/archive/2007/09/17/895463.html

#include<stdlib.h>
//定义队列的结构
struct queue
{
   
int q[4];//存放数据元素
   int front;//队头指针,指向队头
   int rear;//队尾指针,队尾指针始终指向队尾元素的后一个位置
};
//初始化队列
struct queue * InitialQueue()
{
 
struct queue * head;
 head
=(struct queue *)
     malloc(
sizeofstruct queue ));
 head
->front=0;
 head
->rear=0;
 
return head;
}

void EnterIntoQueue(struct queue * head,
                    
int value)
{
    
if(head->front== (head->rear+1)%4)
    {
       printf(
"Queue is full. Enter failed.\n");
        
return;
    }
    head
->q[head->rear]=value;
    head
->rear=( head->rear+1)%4;
 }

void DeleteFromQueue(struct queue * head)
{
 
if(head->front==head->rear)
 {
     printf(
"Queue is empty, Delete failed\n");
 }
 
else
    {
        head
->front=(head->front+1)%4;
    }
}

void ShowQueue(struct queue * head)
{
/* 输出要分开设计 */
  
int i;
  printf(
"\n队列元素\n");
  
for(i=0;i<=3;i++)
      printf(
" %d ",head->q[i]);
}

void main()
{

 
struct queue * head;
 head
=InitialQueue();
 EnterIntoQueue(head,
1);
 ShowQueue(head);
 EnterIntoQueue(head,
2);
 ShowQueue(head);
}

相关文章:

  • 2022-01-23
  • 2021-07-13
  • 2021-06-12
  • 2021-12-11
  • 2021-12-20
  • 2021-09-29
  • 2021-07-27
  • 2021-12-06
猜你喜欢
  • 2021-08-02
  • 2022-01-13
  • 2021-10-29
  • 2021-12-06
  • 2022-01-09
  • 2021-05-24
  • 2022-01-26
相关资源
相似解决方案