可能有错误,只是一个初步的实现:
#include<iostream>
#include <string>
class node
{
public:
node():item(),next(NULL){}
node(int i):item(i),next(NULL){}
int item;
node *next;
};
class queue
{
private:
node *head;
node *tail;
size_t len;
public:
queue():head(NULL),tail(NULL),len(0){}
~queue();
void push(int);
void pop(int &);
size_t get_len();
bool is_empty();
void display();
};
queue::~queue()
{
if (len != 0)
{
node *new_node = head;
head = head->next;
delete new_node;
}
}
void queue::push(int i)
{
node *new_node = new node(i);
if (len == 0)
{
head = tail = new_node;
}
else
{
tail->next = new_node;
tail = new_node;
}
++len;
}
void queue::pop(int &i)
{
if(len == 0)
return ;
node *new_node = head;
head = head->next;
delete new_node;
--len;
}
size_t queue::get_len()
{
return len;
}
bool queue::is_empty()
{
return len == 0;
}
void queue::display()
{
if (len == 0)
std::cout << "the queue is empty!" << std::endl;
else
{
node *new_node = head;
for (size_t i = 0; i != len; ++i)
{
std::cout << new_node->item << " ";
new_node = new_node->next;
}
}
}
int main()
{
queue q;
q.push(1);
q.push(2);
q.push(3);
int e = 0;
q.pop(e);
q.display();
}