順序隊列(迴圈隊列)基本操作實現 C語言

來源:互聯網
上載者:User

唯寫了幾個主要的操作,銷毀,清空等沒有寫

#include <stdio.h>#include <stdlib.h>#define OK 1#define ERROR 0#define QUEUE_SIZE 100typedef  int Status;typedef  int ElemType;typedef struct Qnode{        ElemType *base;        int front;        int rear;        int count;} Qnode;Status Init(Qnode &qq){       qq.base = (ElemType*)malloc(QUEUE_SIZE * sizeof(ElemType));       if (qq.base == NULL) return ERROR;       qq.front = 0;       qq.rear = 0;       qq.count = 0;       return OK;}Status EnQueue(Qnode &qq, ElemType e){              if ((qq.rear + 1) % QUEUE_SIZE == qq.front)       return ERROR;       qq.base[qq.rear] = e;       qq.rear = (qq.rear + 1)%QUEUE_SIZE;       qq.count++;       return OK;}Status DeQueue(Qnode &qq, ElemType &e){       if (qq.rear == qq.front)       return ERROR;       e = qq.base[qq.front];       qq.front = (qq.front + 1) % QUEUE_SIZE;       qq.count--;       return OK;}Status GetTop(Qnode &qq, ElemType &e){       if (qq.rear == qq.front)       return ERROR;       e = qq.base[qq.front];       return OK;}Staus Destroy(Qnode qq){      free(qq.base);      qq.base = NULL;      qq.count = 0;      return OK;}int main(){    Qnode qq;    Init(qq);    int m, i;    printf("how many nums?\n");    scanf("%d", &m);    for (i = 0; i < m; i++)    {        int e;        scanf("%d", &e);        EnQueue(qq, e);    }    int e;    GetTop(qq, e);    printf("%d\n" , e);    for (i = 0; i < m; i++)    {        DeQueue(qq, e);        printf("%d ", e);    }    printf("\n");    system ("pause");    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.