很棒的队列类程序

 

//#include==============================================================================================================
#include <iostream>
using namespace std;

//struct node======================================================================================================
struct node   //定义队列
{   int data;
    struct node *next; };

//class Queue======================================================================================================
class Queue  //定义队列类
{private:
    node *rear; //队列类中的两个node型的指针变量,用rear指向队列尾,front指向队列头
    node *front;
 public:
    Queue();
 void InitQueue();//构造新队列函数
    void show();
    int get();
    void put(int newVal); //在队尾增加一个新元素

//empty()--------------------------------------------------------------------------------------------------------
bool empty()  //判断队列是否为空(内联函数)
   {if(front==rear)
       {cout<<"此队为空队,你还没有输入元素,请继续选择操作!"<<endl<<endl;
        return 0;}
   else
        {cout<<"此队列不为空"<<endl;
      return 1;} 
} //end of empty

//getCount---------------------------------------------------------------------------------------------------------------
int getCount() //(内联函数)
{node *p=front;
 node *q;
int Count=0;
if(rear==front)
  cout<<"元素个数是: 0"<<endl;
else
if(rear==front->next)
  cout<<"元素个数是: "<<Count+1<<endl;
else
{while(p->next!=NULL)
  {Count+=1;
   q=p;
   p=q->next;}
cout<<"元素个数是"<<Count<<endl;}
return 0;} //end of getCount
};//end of class

//Queue()========================================================================================================
Queue::Queue()//定义创建空队列
{front=new node;
 rear=front;
 front->next=NULL; }// end of Queue

//InitQueue()--------------------------------------------------------------------------------------------------------
void Queue::InitQueue()//初始化队列(头结点没有用)
{node *p=new node;//创建新的节点,用p指向新的节点
 int i=0;
 cout<<"请输入要进队的整数值"<<endl<<endl<<"(注意:如果输入的不是整数,程序将会出现“死”状态而无法继续操作)"<<endl<<endl<<"(用-1结束数据输入):";
 cin>>p->data;//给第一个节点输入数据
 while(p->data!=-1)
   {i+=1;
    if(i==1)
     {front->next=p; //队首指针指向第一个有元素的节点
      rear=p;}
    else
     {rear->next=p;
      rear=p; }
    p=new node;
 cin>>p->data;}//给新节点输入数值
   rear->next=NULL; }//队列建完即数据输入结束之后,最后一个节点的指针域赋为空指针

//get()==============================================================================================================
int Queue::get() //取出队头元素,并释放结点空间
{node *p=front->next;
 node *q;
 if(front!=rear&&front->next->next!=NULL)
  {cout<<"头结点的元素是:"<<p->data<<endl;
   q=p;
   p=q->next;
   front->next=p;
   free(q);}
else if(front!=rear&&front->next->next==NULL)
  {cout<<"头结点的元素是:"<<p->data<<endl;
   rear=front;
   free(p);}
else
  cout<<"此队列为空队列,请输入元素之后再执行此操作"<<endl;

return 0;}

//put====================================================================================================================

void Queue::put(int newVal)//在队尾增加一个新元素
{node *p=new node;
 p->data=newVal;
 rear->next=p;
 rear=p;
 rear->next=NULL;
}//end of put

//show()======================================================================================================
void Queue::show()//显示队列的元素
{ node *p=front;
  if(front->next!=NULL)
     {cout<<"你刚才输入的元素按照你输入的顺序依次是: "<<endl;
      while(p->next!=NULL)
      {cout<<p->next->data<<" ";  //先输出第一个节点的数据、并且循环输出接下来节点的数据
       p=p->next;}//指针下移,为输出数据作准备
   cout<<"(其中101010是自定义结束符不是数据)"<<endl; }
  else
      cout<<"此队列没有元素可以输出,请输入元素之后再执行此操作" <<endl;}

//int main()======================================================================================================
int main()
{ int choose=0;//定义选择
  int newVal;//定义新节点数据变量
  Queue p;//定义类成员
do{
 cout<<endl;
 cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
 cout<<"      【1】初始化队列并输入队列元素         【2】判断队列是否为空       "<<endl<<endl;
 cout<<"      【3】输出队列的元素                   【4】计算队列元素的个数     "<<endl<<endl;
 cout<<"      【5】取出队头元素,并释放结点空间     【6】在队尾增加一个新元素   "<<endl<<endl;
 cout<<"      【7】退出程序,不做任何操作                                       "<<endl;
 cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
 cout<<endl<<"请选择操作"<<" ";
 cin>>choose;
 cout<<endl;
 switch(choose)
 {case 1: p.InitQueue();//调用创建成员函数  创建新链队列
          cout<<endl;
          break;
  case 2: p.empty();break;
  case 3: p.show();break;
  case 4: p.getCount();break; //取队列中元素的个数
  case 5: p.get();break;  //取出队头元素,并释放结点空间
  case 6: cout<<"请输入新节点的数据"<<" ";
          cin>>newVal;
       p.put(newVal);
    break;
  default:break;}  //end of switch
 }while(choose!=7);

 return 0;}//end of main

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值