• 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
                   

  • 相关阅读:
    js-封装几个常用函数
    js获取地址栏的几种方法
    vue 子组件传值给父组件,兄弟组件传参以及实现动态组件
    实现同一个组件页面通过不同的tab标签页打开
    echarts柱状图一组数不同柱子的颜色修改
    echarts实现环形图
    vue 中JSONPath的使用方法之一
    vue element-ui 左侧菜单栏 el-menu属性实现动态菜单
    vue 前端服务器代理,proxyTable简要叙述
    分享一篇IBN(Intent-based networking)调研报告
  • 原文地址:https://www.cnblogs.com/3ddan/p/3278803.html
Copyright © 2020-2023  润新知