• 数组实现的简单循环队列


    #include <stdio.h>
    #include <stdlib.h>
    //设计一个队列的类型
    typedef int Item;
    struct queue
    {
    
     Item * head;
     Item * tail;
     int lenth;
     int size;
    };
    void deletes(struct queue *que);
    void insert(struct queue *que,Item k);
    void prin(struct queue* que);
    Item * add;
    int main(void)
    {
    
     //initialize the queue
     int arr[5];
     Item n[5]={1,2,3,4,5};
     Item a=10;  
     struct queue *que;
     que=(struct queue*)malloc(sizeof(struct queue));
     add=&arr[0];
     que->tail=que->head =&arr[0];
     que->size =0;
     que->lenth =5;
     for(int i=0;i<5;i++)
      insert(que,n[i]);//add an item
     prin(que);  //print the queue 
     deletes(que);//delete an item from the head
     prin(que);
     insert(que,a);
     prin(que);
     deletes(que);
     prin(que);
     insert(que,a);
     prin(que);
     insert(que,a);
     prin(que);
     return 0;
    }
    void deletes(struct queue *que)
    {
     printf("delete an item:%d
    ",*(que->head));
     (que->head)++;
     (que->size)--;
    }
      
    void insert(struct queue * que,Item k)
    {
     printf("insert an item: %d into the queue
    ",k);
     if(que->size==que->lenth )
     {
      printf("queue is full so that can not add an item
    ");
      exit(1);
     } 
     *(que->tail )=k;
     (que->tail )++;
     (que->size )++;
     if(que->size==que->lenth )
      printf("queue is full
    ");
     if(que->tail ==(add+que->lenth ))
      que->tail = add;
    }
    void prin(struct queue* que)
    {
     printf("print all of items in the queue:
    ");
     Item *ss;
     ss=que->head ;
     do
     {  
      printf("%d,",*(ss));
      ss++;
       if((ss)==(add +que->lenth ))
       ss =add ;
     }while(ss!=que->tail);
     printf("
    ");
    }
     
    
    
  • 相关阅读:
    JDBC_查询
    微信web小程序开发1
    小型人口普查系统2
    小型人口普查系统1
    JSP内置对象作用域
    Session和Cookie的区别
    Cookie
    Session
    c++中嵌入python
    目标文件obj的各段 2
  • 原文地址:https://www.cnblogs.com/jzhao/p/3213293.html
Copyright © 2020-2023  润新知