侧边栏壁纸
博主头像
王小木人

这是很长,很好的一生

  • 累计撰写 141 篇文章
  • 累计创建 43 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

队列 C++实现

王小木人
2021-05-22 / 0 评论 / 0 点赞 / 1,102 阅读 / 921 字
#include<iostream>
using namespace std;
 struct Node{
 int data;
 Node *next;
};
class LinkQueue{
private:
Node *front;
Node *rear;
public:
LinkQueue();//构造函数,
~LinkQueue();//析构函数
     void EnQueue(int e);//入队
void QueueDisplay();//遍历队列
};
LinkQueue::LinkQueue()
{
front=new Node;
front->next=NULL;
     rear=new Node;
rear=front;
}
LinkQueue::~LinkQueue()
{
    Node *p;
while(front!=NULL)
{
      p=front;
  front=front->next;
  delete p;
}
}
void LinkQueue::EnQueue(int e)
{
  Node *s;
  s=new Node;
  s->data=e;
  if(front->next==NULL)
  front->next=s;
  rear->next=s;
  s->next=NULL;
  rear=s;
}
void LinkQueue::QueueDisplay()
{
 Node *p=front->next;
            
            cout<<"遍历队列:"<<endl;
            while(p!=NULL)
            {
                cout<<p->data<<" ";
                p=p->next;
            }
            cout<<endl;
}








int main()
{
LinkQueue *L=new LinkQueue();




L->EnQueue(1);
L->EnQueue(2);
L->EnQueue(3);
L->EnQueue(4);
L->EnQueue(5);
L->QueueDisplay();




  return 0;
}
0

评论区