简单队列实现
Posted susidian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单队列实现相关的知识,希望对你有一定的参考价值。
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 }
以上是关于简单队列实现的主要内容,如果未能解决你的问题,请参考以下文章