现在的位置: 首页 > 综合 > 正文

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

2012年08月29日 ⁄ 综合 ⁄ 共 2781字 ⁄ 字号 评论关闭

下面是源代码:

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

#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;
}

 

 

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

抱歉!评论已关闭.