关于队列的一些操作

关于队列的一些操作(注:以链表方式写的),持续更新中.....

#include<iostream>
using namespace std;

class Node
{
public:
    int data;
    Node *next;
};

class Linkqueue
{
public:
     Linkqueue(); //初始化
     ~Linkqueue();//销毁
    bool IsEmpty(); //判断是否为空
    void EnQueue(); //加入数据
    void DeQueue(); //删除数据
    void showqueue(); //显示数据
    void findQueue(int value); //查找数据
    int getlength(); //得到长度
    void qingkong();//清空

private:
    Node *front;//队首
    Node *rear;//队尾
    int N;
};

//队列的初始化
 Linkqueue::Linkqueue()
{
    front = rear = NULL;
}

//摧毁队列
 Linkqueue::~Linkqueue()
{
     delete front;
     delete rear;
}

//队列的判空
bool Linkqueue::IsEmpty( )
{
    if (N == 0)
    {
        return true;
    }
    else
        return false;
}

//队列节点的插入    //尾插法
void Linkqueue::EnQueue( )
{
    Node *pnew = new Node;
    pnew->next = NULL;
    cout << "请输入插入节点的数值 = ";
    cin >> pnew->data;
    if (N == 0)
    {
        front = rear = pnew;
        N++;
    }
    else
    {
        rear->next = pnew;
        rear = pnew;
        N++;
    }
    
}

//队列节点的删除   //删头法
void Linkqueue::DeQueue( )
{
    Node *p=front;
    front = front->next;
    delete p;
    N--;
}

//显示队列
void Linkqueue::showqueue( )
{
    Node *p = front;
    if (p == NULL)
    {
        cout << "队列为空" << endl;
    }
    while (p != NULL)
    {
        cout << p->data << " ";
        p = p->next;
    }
}

//队列查询
void Linkqueue::findQueue( int value)
{
    Node *p = front;
    int n = 0;
    cout << "数据值为" << value << "的位置是:";
    while(p)
    {
        if (p->data == value)
        {
            cout << n + 1 << endl;
            break;
        }
        else
        {
            n = n + 1;
            p = p->next;
        }
    }
    
}

//计算队列长度
int Linkqueue::getlength( )
{
    return N;
}

//清空队列
void Linkqueue::qingkong()
{
    Node *p = front;
    while (front != NULL  )
    {
        front = front->next;
        delete p;
        p = front;
        N--;
    }
}

int main()
{
    Linkqueue L;
    int i;
    cout << "1.判断是否为空   2.插入节点   3.删除节点   4.显示队列   5.查询   6.队列长度  7.清空  0.退出" << endl;
    do
    {
        cout << "请输入要执行的操作:";
        cin >> i;
        switch (i)
        {
        case 1:
            if (L.IsEmpty() == 1)
                cout << "这是一个空队列" << endl;
            else
            {
                cout << "这不是空队列" << endl;
            }
            break;
        case 2:
            L.EnQueue();
            break;
        case 3:
            L.DeQueue();
            break;
        case 4:
            L.showqueue();
            break;
        case 5:
            int a;
            cout << "请输入你要查询的数字:";
            cin >> a;
            L.findQueue(a);
            break;
        case 6:
            cout << "队列大小为:" << L.getlength() << endl;
            break;
       case 7:
           L.qingkong();
           break;
        default:
            cout << "输入的操作值错误" << endl;
            break;
        }
    } while (i != 0);
    system("pause");
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值