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
               

posted on 2013-08-23 23:37  寻找心的巨人  阅读(469)  评论(0编辑  收藏  举报

导航