队列的链表储存结构及实现

//队列的链表有附加头指针,指向队列的第一个数据结点的前一位置
typedef int Elemtype;
struct quenode
{
	Elemtype data;
	quenode *next;
};

class LsQueue
{

private:
	quenode *front;
	quenode *rear;


public:

	LsQueue();
	~LsQueue();
	int IsEmpty();              //判断是否为空
	void Display();				//输出队列元素
	void AddQ(Elemtype x);		//入队
	Elemtype DelQ();			//出队
	Elemtype GetFront();		//输出队顶
}
//构造函数  初始化一个空队列
LsQueue::LsQueue()
{
	quenode *p;
	p = new quenode;
	p->next = NULL;
	front = rear = p;
}

//判断队列是否为空

LsQueue::~LsQueue()
{
	quenode *p;
	if(front->next !=NULL)   //加判断 如果已经是空队列,即front->next ==NULL  则不能再析构
	{ 
		p = front->next;
		while (p != NULL)
		{
			front->next = p->next;
			delete p;
			p = front->next;
		}
		delete front;
		delete rear;
		rear = front = NULL;
	}

}

int LsQueue::IsEmpty()
{
	if (front == rear)
		return 1;
	else
		return 0;
}


//取队首元素 不出队
Elemtype LsQueue::GetFront()
{
	Elemtype x;
	quenode *p;
	if (front == rear)
	{
		cout << "\n Queu Is Empty " << endl;
		x = -1;
	}
	else
	{
		p = front->next;
		x = p->data;
	}
	return x;
}


//入队
void LsQueue::AddQ(Elemtype x)
{
	quenode *s;
	s = new quenode;
	s->data = x;
	s->next = NULL;
	rear->next = s;
	rear = s;
}

//出队操作  在队头进行
Elemtype LsQueue::DelQ()
{
	Elemtype x;
	quenode *p;
	if (front == rear)
	{
		cout << "\n  队列为空" << endl;
		x = -1;
	}
	else
	{
		p = front->next;
		front->next = p->next;
		if (p->next == NULL)       //此时表明队列链表只有一个元素  删除后头尾指针重合
			rear = front;		   //防止尾指针丢失
		x = p->data;
		delete p;
	}
	return x;

}

void LsQueue::Display()
{
	quenode *p;
	p = front->next;
	while (p !=NULL)
	{
		cout << "\n 元素:" << p->data << endl;;
		p = p->next;
	}
	cout << "结束" << e
}
int main()
{
	Elemtype e;
	LsQueue Q;
	cout << "\n 队列链式存储结构演示";
	cout << "进队 data= ?"; cin >> e;
	Q.AddQ(e);
	Q.Display();
	e = Q.DelQ();
	Q.Display();


    std::cout << "Hello World!\n"; 
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael.Scofield

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值