数据结构学习之——手写队列,栈(入队、出队、压栈、出栈)

本文详细介绍了栈和队列两种基本数据结构的实现方式,包括栈的压栈、出栈操作,以及队列的入队、出队、判断队空等功能。通过C++代码展示了如何使用链表来实现这两种数据结构。

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

一、栈实现

基本功能:

  1. 压栈
  2. 出栈

代码实现:

#include <iostream>
using namespace std;

typedef struct node {
    int data;
    struct node* pNext;
}LinkStack;

class LkStack
{
public:
    LinkStack* pushStack( int n);
    LinkStack* popStack(int pdata);
private:
    LinkStack* _top;
};

LinkStack* LkStack::pushStack( int n)
{
    LinkStack* p = nullptr;
    p = reinterpret_cast<LinkStack*>(sizeof(LinkStack));
    p->data = n;
    p->pNext = _top;
    _top = p;
    return _top;

}
LinkStack* LkStack::popStack(int pdata)
{
    LinkStack* p;
    if(_top!=nullptr)
    {
        p = _top;
        pdata = _top->data;
        _top = p->pNext;
        delete p;
    }
    return _top;
}
//int main()
//{
//    cout<<"success"<<endl;
//}

二、队列

基本功能:

  1. 判断队空
  2. 取队头元素
  3. 入队
  4. 出队

设计原理:
在这里插入图片描述
代码实现:

#include <iostream>
using namespace std;

typedef struct node {
    int data;
    struct node* next;
}LinkListNode;

typedef struct {
    LinkListNode* front, * rear;
}LinkQueue;

class LkQueue
{
public:
    LkQueue();
    bool IsEmpty_LkQueue();
    bool Get_LkQueue(int* x);
    void Insert_LkQueue(int x);
    LinkListNode* Del_LkQueue();
    void Destroy_LkQueue();

    void dis();

private:
    LinkQueue* lq = &node;
    //定义指针域lq,front始终指向队头,rear指向队尾
    LinkQueue node;
};
LkQueue::LkQueue()
{
    lq->front = static_cast<LinkListNode*>(malloc(sizeof(LinkListNode)));
    lq->front->next = nullptr;
    lq->rear = lq->front;
}
bool LkQueue::IsEmpty_LkQueue()
{
    if (lq->front == lq->rear)
        return true;
    return false;
}

bool LkQueue::Get_LkQueue(int* x)
{
    if (IsEmpty_LkQueue())
        return false;
    x = &(lq->front->next->data);
    return true;
}

void LkQueue::Insert_LkQueue(int x)
{
    lq->rear->next = static_cast<LinkListNode*>(malloc(sizeof(LinkListNode)));
    lq->rear = lq->rear->next;
    lq->rear->data = x;
    lq->rear->next = nullptr;

}

LinkListNode* LkQueue::Del_LkQueue()
{
    LinkListNode* s = nullptr;
    if (!IsEmpty_LkQueue())
    {
        s = lq->front->next;
        if (s->next == nullptr)
            lq->front = lq->rear;//队列中只有一个元素,将队列置空
        else
            lq->front->next = s->next;
        return s;
    }
    return nullptr;
}

void LkQueue::Destroy_LkQueue()
{
    LinkListNode* s;
    while (!IsEmpty_LkQueue())
    {
        s = Del_LkQueue();
        free(s);
    }
    free(lq->front);
    lq->front = nullptr;
    lq->rear = nullptr;
}
void LkQueue::dis()
{
    LinkListNode* s = nullptr;
    s = lq->front;
    cout << "success" << endl;
    while (s != lq->rear)
    {
        cout << s->next->data << " ";
        s = s->next;
    }
    cout << endl;
}


int main()
{
    cout << "success0111" << endl;
    LkQueue s;
    s.Insert_LkQueue(2);
    s.Insert_LkQueue(5);
    s.Insert_LkQueue(7);
    s.dis();
    s.Del_LkQueue();
    s.dis();
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值