使用链表实现队列

#ifndef _QUEUE_H
#define _QUEUE_H
#include <iostream>
using namespace std;
class Queue{
private:
	struct queueNode{
		int data;
		queueNode *next;
	};
	queueNode *front;
	queueNode *tail;
public:
	Queue();
	~Queue();
	bool enQueue(int data);
	bool deQueue();	
	bool isEmpty();
	void display();
};
Queue::Queue():front(NULL),tail(NULL){}
Queue::~Queue(){}

bool Queue::isEmpty(){
	if(front==NULL && tail==NULL)
		return true;
	else
		return false;
}
void Queue::display(){
	if(isEmpty())
		cout << "The Queue is Empty..." << endl;
	queueNode *tmp=front;
	while (tmp!=NULL){
		cout << tmp->data << "  ";
		tmp=tmp->next;
	}
	cout << endl;
}
bool Queue::enQueue(int data){
	if(isEmpty()){
		//若队列为空
		front=tail=new queueNode();
		tail->data=data;
		tail->next=NULL;
	}else{
		//队列非空
		queueNode* tmp=new queueNode();
		tmp->data=data;
		tmp->next=NULL;
		tail->next=tmp;
		tail=tail->next;
	}
	return true;
}
bool Queue::deQueue(){
	if(isEmpty())           //为空时
		return false;
	else if(front==tail){   //只有一个节点时
		queueNode *tmp=tail;
		front=tail=NULL;
		delete tmp;
		tmp=NULL;
		return true;
	}
	else{                
		queueNode *tmp=front;
		front=front->next;
		delete tmp;
		tmp=NULL;
		return true;
	}
}

#endif//~_QUEUE_H

 

#include "Queue.h"
#include <iostream>
using namespace std;
int main(){
	Queue que;
	cout << que.isEmpty() << endl;
	que.display();

	que.enQueue(3);
	que.enQueue(5);
	que.display();

	que.deQueue();
	que.deQueue();
	que.display();

	que.enQueue(1);
	que.enQueue(2);
	que.enQueue(3);
	que.display();

	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值