zoukankan      html  css  js  c++  java
  • C++ 通用队列类

     1  #include"stdio.h"
      2  #include"malloc.h"
      3  class Queue
      4 {
      5         private:
      6                  typedef struct queue
      7                 {
      8                         int data;
      9                         struct queue *next;
     10                 }link;
     11                 link*tail;
     12          public:
     13                 int queue_init()
     14                 {
     15                         tail=(link*)malloc(sizeof(link));
     16                         if(tail!=NULL)
     17                         {
     18                                 tail->next=tail;
     19                                 return 1;
     20                         }
     21                         else    return 0;
     22                 }
     23                 int inqueue(int n)
     24                 {
     25                         link *p;
     26                         p=(link*)malloc(sizeof(link));
     27                         if(p==NULL)   return 0;
     28                         else
     29                         {
     30                                 p->data=n;
     31                                 p->next=tail->next;
     32                                 tail->next=p;
     33                                 return 1;
     34                         }
     35                 }
     36                 int outqueue()
     37                 {
     38                         link *p=tail,*q=p;
     39                         while(p->next!=tail)
     40                         {
     41                                 q=p;
     42                                 p=p->next;
     43                         }
     44                         if(p==q)   return 0;
     45                         else
     46                         {
     47                                 q->next=p->next;
     48                                 free(p);
     49                                 return 1;
     50                         }
     51                 }
     52                 void trans()
     53                 {
     54                         link*p=tail->next;
     55                         while(p!=tail)
     56                         {
     57                                 printf("%d",p->data);
     58                                 p=p->next;
     59                         }
     60                 }
     61
     62
     63 };
     64  int main()
     65 {
     66         Queue s;
     67         int n;
     68         s.queue_init();
     69         while(n)
     70         {      

     71                    scanf("%d",&n);
     72                 if(n==0)   break;
     73                 else if(n>0)
     74                         s.inqueue(n);
     75                 else
     76                          s.outqueue();
     77                 s.trans();
     78         }
     79 }
     80
     81
                   

  • 相关阅读:
    Reactor系列(四)subscribe订阅
    Reactor系列(三)创建Flux,Mono(续)
    Reactor系列(二)Flux Mono创建
    Reactor系列(一)基本概念
    Stream系列(十五)File方法使用
    Stream系列(十四)parallet方法使用
    OpenCV二值化、归一化操作
    C# 队列
    linux shell脚本程序路径作为变量
    C++中头文件(.h)和源文件(.cpp)都应该写些什么
  • 原文地址:https://www.cnblogs.com/3ddan/p/3278803.html
Copyright © 2011-2022 走看看