zoukankan      html  css  js  c++  java
  • 算法8---队列及其实现

    队列及其实现

    和上一部分关于栈的部分一样,不讲基本知识,直接实现,走你!

    队列和栈相似,也包括一些基本的队列的操作,初始化,出队列,入队列,判空,判满,清空等操作。

      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 #define QUEUELEN 15
      6 
      7 typedef struct
      8 {
      9     char name[10];
     10     int age;
     11 }DATA;
     12 
     13 typedef struct
     14 {
     15     DATA data[QUEUELEN];
     16     int head;
     17     int tail;
     18 }queueType;
     19 
     20 queueType *initQueue()
     21 {
     22     queueType *q;
     23     if (q=(queueType *)malloc(sizeof(queueType)))
     24     {
     25         q->head=0;
     26         q->tail=0;
     27         return q;
     28     }
     29     else
     30         return NULL;
     31 }
     32 
     33 
     34 int queueEmpty(queueType *q)
     35 {
     36     int flag;
     37     flag=(q->head==q->tail);
     38     return flag;
     39 }
     40 
     41 int queueFull(queueType *q)
     42 {
     43     int flag;
     44     flag=(q->tail==QUEUELEN);
     45     return flag;
     46 }
     47 
     48 void clearqueue(queueType *q)
     49 {
     50     q->head=0;
     51     q->tail=0;
     52 }
     53 
     54 void freequeue(queueType *q)
     55 {
     56     if (q!=NULL)
     57     {
     58         free(q);
     59     }
     60 }
     61 
     62 int inqueue(queueType *q,DATA data)
     63 {
     64     if (q->tail==QUEUELEN)
     65     {
     66         printf("the queue if full!
    ");
     67         return 0;
     68     }
     69     else
     70     {
     71         q->data[q->tail++]=data;
     72         return 1;
     73     }
     74 }
     75 
     76 DATA outqueue(queueType *q)
     77 {
     78     if (queueEmpty(q))
     79     {
     80         printf("empty  queue!
    ");
     81         exit(0);
     82     }
     83     else
     84         return q->data[q->head++];
     85 }
     86 
     87 DATA readqueue(queueType *q)
     88 {
     89     if (queueEmpty(q))
     90     {
     91         printf("empty  queue!
    ");
     92         exit(0);
     93     }
     94     else
     95         return q->data[q->head];
     96 }
     97 
     98 int queuelength(queueType *q)
     99 {
    100     int len;
    101     len=q->tail-q->head;
    102     return len;
    103 }
    104 
    105 int main()
    106 {
    107     queueType *queue;
    108     DATA data,data1;
    109     queue=initQueue();
    110     printf("push queue!
    ");
    111     printf("input name,age to push data!
    ");
    112     do
    113     {
    114         scanf("%s%d",data.name,&data.age);
    115         if (strcmp(data.name,"0")==0)
    116         {
    117             break;
    118         }
    119         else
    120         {
    121             inqueue(queue,data);
    122         }
    123     }while(1);
    124 
    125     do
    126     {
    127         printf("pop stack operation!
    ");
    128         data1=outqueue(queue);
    129         printf("the out queue data is (%s,%d)
    ",data1.name,data1.age);
    130     }while(1);
    131 
    132 
    133     freequeue(queue);
    134     return 0;
    135 
    136 }
  • 相关阅读:
    AD域渗透总结
    CTF
    ew做代理 进一步内网渗透
    CVE-2019-0708复现
    win 内网frp反弹到内网liunx
    Liunx反弹shell的几种方式
    sql到python正则
    PHP面向对象学习-属性 类常量 类的自动加载 构造函数和析构函数 访问控制(可见性)
    PHP反序列化学习
    PHP面向对象学习
  • 原文地址:https://www.cnblogs.com/tao-alex/p/5879131.html
Copyright © 2011-2022 走看看