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
                   

  • 相关阅读:
    42. Trapping Rain Water
    223. Rectangle Area
    645. Set Mismatch
    541. Reverse String II
    675. Cut Off Trees for Golf Event
    安装 VsCode 插件安装以及配置
    向上取整 向下取整 四舍五入 产生100以内随机数
    JS 判断是否为数字 数字型特殊值
    移动端初始配置,兼容不同浏览器的渲染内核
    Flex移动布局中单行和双行布局的区别以及使用
  • 原文地址:https://www.cnblogs.com/3ddan/p/3278803.html
Copyright © 2011-2022 走看看