zoukankan      html  css  js  c++  java
  • 简单队列实现

     1 class MyQueue
     2 {
     3 public:
     4 MyQueue();
     5 bool Get(int *&p);
     6 bool Put(int a);
     7 int GetLength();
     8 private:
     9 struct Node * rear;
    10 struct Node * front;
    11 int length;
    12 };
    13 
    14 MyQueue::MyQueue()
    15 {
    16 struct Node *tmp = new struct Node;
    17 tmp->next = NULL;
    18 rear=front=tmp;
    19 length=0;
    20 }
    21 
    22 bool MyQueue::Put(int a)
    23 {
    24 struct Node *tmp;
    25 
    26 if(length < MAX)
    27 {
    28 rear->data=a;
    29 
    30 struct Node *tmp = new struct Node;
    31 tmp->next=NULL;
    32 rear->next = tmp;
    33 rear = tmp;
    34 length++;
    35 
    36 return true;
    37 }
    38 else
    39 return false;
    40 }
    41 
    42 bool MyQueue::Get(int *&p)
    43 {
    44 if(rear == front)
    45 {
    46 return false;
    47 }
    48 else
    49 {
    50 struct Node *tmp = front;
    51 *p = tmp->data;
    52 front = front->next;
    53 --length;
    54 delete tmp;
    55 tmp = NULL;
    56 return true;
    57 }
    58 }
    59 
    60 MyQueue::~MyQueue()
    61 {
    62 if(rear!=front)
    63 {
    64 while(front !=NULL)
    65 {
    66 struct Node *tmp = front; 
    67 front = front->next;
    68 delete tmp;
    69 tmp= NULL;
    70 }
    71 rear = front =NULL;
    72 }
    73 }
    74 
    75 int MyQueue::GetLength()
    76 {
    77 return length;
    78 }
  • 相关阅读:
    Java8新特性一览表
    FastDFS 单机部署指南
    EntityManager的Clear方法的使用
    元类
    python中的函数的分类
    python中的多任务
    正则表达式
    GIL和copy
    文件管理
    logging日志模块配置
  • 原文地址:https://www.cnblogs.com/susidian/p/10013169.html
Copyright © 2011-2022 走看看