码迷,mamicode.com
首页 > 其他好文 > 详细

简单队列实现

时间:2018-11-24 21:07:11      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:new   class   front   ++   put   return   struct   vat   队列实现   

 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 }

 

简单队列实现

标签:new   class   front   ++   put   return   struct   vat   队列实现   

原文地址:https://www.cnblogs.com/susidian/p/10013169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!