数据结构实验--链式队列(C++实现)

本文介绍了一种使用链表实现的链式队列的数据结构,并提供了详细的源代码实现。该实现包括基本的队列操作如入队、出队等,并通过示例展示了如何进行元素遍历、队列清空等高级操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是源代码:

-------------------------------------------------------------

#include<iostream>
using namespace std;

#define OK 1
#define ERROR 0

typedef int Status;
typedef struct QNode
{
 int data;
 struct QNode *next;
}QNode,*QNodePtr;


/////////////////////////////////////////////////
//LinkQueue类的声明
//
class LinkQueue
{
private:
 QNodePtr front;
 QNodePtr rear;
public:
 LinkQueue();
 ~LinkQueue();
 Status ClearQueue();
 Status QueueEmpty();
 int    QueueLength();
 Status GetHead(int& e);
 Status EnQueue(int e);
 Status DeQueue(int& e);
 Status QueueTraverse(Status visit(QNode& e));
 Status PutQueue();
};

 

/////////////////////////////////////////////////
//LinkQueue类的实现
//
LinkQueue::LinkQueue()
{
 front=static_cast<QNodePtr>(new QNode);
 front->next=NULL;
 rear=front;
}

LinkQueue::~LinkQueue()
{
 QNodePtr temp=front;
 while (front!=rear)
 {
  front=front->next;
  delete temp;
  temp=front;
 }
 delete temp;
 front=NULL;
 rear=NULL;
}

 

Status LinkQueue::ClearQueue()
{
 QNodePtr pLink=front->next,temp=front->next;
 while (pLink!=rear)
 {
  pLink=pLink->next;
  delete temp;
  temp=pLink;
 }
 rear=front;
 delete temp;
 return OK;
}


Status LinkQueue::QueueEmpty()
{
 return front==rear;
}


int LinkQueue::QueueLength()
{
 int length=0;
 QNodePtr pLink=front->next;
 while (pLink!=rear->next)
 {
  pLink=pLink->next;
  length++;
 }
 return length;
}


Status LinkQueue::GetHead(int& e)
{
 if (!QueueEmpty())
 {
  e=front->next->data;
  return OK;
 }
 else
 {
  return ERROR;
 }
}

 

Status LinkQueue::EnQueue(int e)
{
 QNodePtr temp;
 temp=static_cast<QNodePtr>(new QNode);
 if (!temp)
 {
  exit(1);
 }
 temp->data=e;
 temp->next=NULL;
 rear->next=temp;
 rear=temp;
 return OK;
}


Status LinkQueue::DeQueue(int& e)
{
 if (front==rear)
 {
  return ERROR;
 }
 QNodePtr temp=front->next;
 e=temp->data;
 front->next=temp->next;
 if (temp==rear)
 {
  rear=front;
 }
 delete temp;
 return OK;
}

Status LinkQueue::QueueTraverse(Status visit(QNode& e))
{
 QNodePtr pLink=front->next;
 while (pLink!=rear->next)
 {
  
  if (!visit(*pLink))
  {
   return ERROR;
  }
  pLink=pLink->next;
 }
 return OK;
}

Status LinkQueue::PutQueue()
{
 QNodePtr pLink=front->next;
 cout<<"Queue-->";
 while (pLink!=rear->next)
 {
  cout<<pLink->data<<"   ";
  pLink=pLink->next;
 }
 cout<<endl;
 return OK;
}


/////////////////////////////////////////////////
//其他辅助函数
//
Status Add(QNode& e)
{
 e.data++;
 return OK;
}

Status Sub(QNode& e)
{
 e.data--;
 return OK;
}


/////////////////////////////////////////////////
//主函数
//
int main()
{
 int i,e;
 LinkQueue queue;
 queue.PutQueue();
 for (i=0;i<5;i++)
 {
  cout<<"Input the "<<i+1<<" elem:";
  cin>>e;
  queue.EnQueue(e);
  queue.PutQueue();
 }
 queue.QueueTraverse(Add);
 queue.PutQueue();
 queue.QueueTraverse(Sub);
 queue.PutQueue();
 while (!queue.QueueEmpty())
 {
  queue.DeQueue(e);
  cout<<"The element that have been dequeued is:"<<e<<endl;
  queue.PutQueue();
 }
 for (i=0;i<5;i++)
 {
  cout<<"Input the "<<i+1<<" elem:";
  cin>>e;
  queue.EnQueue(e);
  queue.PutQueue();
 }
 queue.ClearQueue();
 queue.PutQueue();
 return OK;
}

 

 

//测试正确,未详细调试~!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值