-
Notifications
You must be signed in to change notification settings - Fork 2
/
Queue.cpp
42 lines (40 loc) · 930 Bytes
/
Queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
using namespace std;
typedef int ElemType;
class QueueNode
{
public:
ElemType data;
QueueNode * next;
};
class Queue
{
public:
void InitQueue() {
QueueNode * p = new QueueNode();
this->front= p;
this->rear=p;
this->front->next = NULL;
}
bool IsEmpty() {
if(this->front==this->rear) return true;
else return false;
}
void EnQueue(ElemType x) {
QueueNode *p = new QueueNode();//动态内存分配,堆内存
p->data=x;
p->next=NULL;
this->rear->next=p;
this->rear=p;
}
ElemType DeQueue() {
if(this->front==this->rear) return -1;
QueueNode * p = this->front->next;
ElemType ret = p->data;
this->front->next=p->next;
if(this->rear==p) this->rear=this->front;
delete p;
return ret;
}
QueueNode *front,*rear;
};