node.h
typedef int elementype;
class Node
{
public:
elementype data; //当前数据
Node *next; //下一结点地址
};
queue.h
#include "node.h"
class Queue
{
private:
Node *front,*rear;
int size;
public:
Queue();
void Insert(elementype x);
elementype Delete();
int length();
bool isEmpty();
};
queue.cpp
#include "queue.h"
Queue::Queue()
{
rear = new Node;
front = rear;
rear->next = NULL;
size =0;
}
void Queue::Insert(elementype x)
{
Node *newNode = new Node;
newNode->data = x;
newNode->next = NULL;
rear->next = newNode;
rear = newNode;
size++;
}
elementype Queue::Delete()
{
Node *temp_node = front->next;
elementype temp;
if( size != 0 )
{
temp = temp_node->data;
front = temp_node;
front->next = temp_node->next;
}
else
temp = -1;
size--;
return temp;
}
int Queue::length()
{
return size;
}
bool Queue::isEmpty()
{
if(front - rear == 0)
return true;
else
return false;
}