用类创建链队列

队列先进先出,指定头结点和尾节点,头结点出队列,尾节点如队列

Node.h:

<span style="font-size:18px;">#ifndef NODE_H
#define NODE_H

class Node
{
public:
	char value;
	Node* next;
};

#endif</span>

LinkQueue.h:
<span style="font-size:18px;">/*********************************************************
*@program: to come true the linkqueue
*@author: kingduo
*@date: 2015/4/29 First Release
************************************************************/
#ifndef LINKQUEUE_H
#define LINKQUEUE_H
#include "Node.h"


class LinkQueue
{
public:
	LinkQueue();
	~LinkQueue();
	void Enqueue(char value);
	char Dequeue();
	void Print();
	char Begin();
	void Clear();
	bool Isempty();
private:
	Node* head;
	Node* rear;
};

#endif</span>
LinkQueue.cpp:
<span style="font-size:18px;">#include <iostream>
#include "LinkQueue.h"
#include "Node.h"

using namespace std;

LinkQueue::LinkQueue()
{
	head = NULL;
	rear = NULL;
}

LinkQueue::~LinkQueue()
{
	while(head != NULL)
	{	
		Node* delete_node = head;
		head = delete_node->next;
		delete delete_node;
	}
}

void LinkQueue::Enqueue(char value)
{
	Node* new_node = new Node;
	new_node->value = value;
	new_node->next = NULL;
	if(head == NULL)
	{
		head = new_node;
		rear = new_node;
	}else
	{
		rear->next = new_node;
		rear = new_node;
	}
}

char LinkQueue::Dequeue()
{
	double num = 0.0;
	Node* delete_node = head;
	if(delete_node == NULL)
	{
		cout<<"The queue is empty."<<endl;
		return -1;
	}else
	{
		num = delete_node->value;
		head = delete_node->next;
		delete delete_node;
	}
	return num;
}

void LinkQueue::Print()
{
	Node* loop_node = head;
	if(loop_node == NULL)
		cout<<"The queue is empty"<<endl;
	else
	{
		while(loop_node != NULL)
		{
			cout<<loop_node->value<<endl;
			loop_node = loop_node->next;
		}
	}
}

char LinkQueue::Begin()
{
	if(head == NULL)
	{
		cout<<"The queue is empty."<<endl;
		return -1;
	}
	else
		return head->value;
}

void LinkQueue::Clear()
{
	while(head != NULL)
	{
		Node* delete_node = head;
		head = delete_node->next;
		delete delete_node;
	}
	rear = NULL;
	cout<<"Clear the queue."<<endl;
}

bool LinkQueue::Isempty()
{
	return head == NULL;
}</span>

main.cpp:

<span style="font-size:18px;">#include <iostream>
#include <ctype.h>
#include "LinkQueue.h"

using namespace std;

int main(void)
{
	char ch;
	double num;
	LinkQueue queue;
	cout<<"Please input your numbers."<<endl;
	while((ch = getchar()) != '\n')
	{
		queue.Enqueue(ch);
	}
	while(!queue.Isempty())
		cout<<queue.Dequeue();
	cout<<endl;
	return 0;
}</span>

;