栈与队列

栈与队列

栈和队列最明显的区别就是:栈是先进后出,队列是先进先出。如何记忆,栈是相当于叠罗汉,最下面的没办法先走,得最后上来的先下来,这就是先进后出。而队列就把数据想象成人在排队,先排队就可以先走,这就是先进先出。

堆叠元素的顶部为栈顶,底部则是栈底。只能先对栈顶进行入栈(添加元素)和出栈(删除元素)的操作。

栈的实现原理

栈遵循先入后出的原则,因此我们只能在栈顶添加或删除元素。然而,数组和链表都可以在任意位置添加和删除元素,因此栈可以视为一种受限制的数组或链表。换句话说,我们可以“屏蔽”数组或链表的部分无关操作,使其对外表现的逻辑符合栈的特性。

基于链表实现的栈示例代码:

#include <iostream>
#include <vector>
using namespace std;

/* 链表节点结构体 */
struct ListNode {
    int val;         // 节点值
    ListNode *next;  // 指向下一节点的指针
    ListNode(int x) : val(x), next(nullptr) {}  // 构造函数

};

/* 基于链表实现的栈 */
class LinkedListStack {
  private:
    ListNode *stackTop; // 将头节点作为栈顶
    int stkSize;        // 栈的长度

  public:
    LinkedListStack() {
        stackTop = nullptr;
        stkSize = 0;
    }

    ~LinkedListStack() {
        // 遍历链表删除节点,释放内存
        freeMemoryLinkedList(stackTop);
    }
     void freeMemoryLinkedList(ListNode *&ListNode){
        while(ListNode!=nullptr){
            delete ListNode;
        }
    }

    /* 获取栈的长度 */
    int size() {
        return stkSize;
    }

    /* 判断栈是否为空 */
    bool isEmpty() {
        return size() == 0;
    }

    /* 入栈 */
    void push(int num) {
        ListNode *node = new ListNode(num);
        node->next = stackTop;
        stackTop = node;
        stkSize++;
    }

    /* 出栈 */
    int pop() {
        int num = top();
        ListNode *tmp = stackTop;
        stackTop = stackTop->next;
        // 释放内存
        delete tmp;
        stkSize--;
        return num;
    }

    /* 访问栈顶元素 */
    int top() {
        if (isEmpty())
            throw out_of_range("栈为空");
        return stackTop->val;
    }
    
   

    /* 将 List 转化为 Array 并返回 */
    vector<int> toVector() {
        ListNode *node = stackTop;
        vector<int> res(size());
        for (int i = res.size() - 1; i >= 0; i--) {
            res[i] = node->val;
            node = node->next;
        }
        return res;
    }
};
int main(){
    LinkedListStack a;
    int size=a.size();
    bool isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<endl;

    a.push(1);
    a.push(5);
    a.push(25);
    a.pop();
    int topNum=a.top();
    vector<int> vec=a.toVector();
    size=a.size();
    isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<topNum<<endl<<endl;
       for (int num : vec) { // 遍历并打印每个元素
        cout << num << " ";
    }
    return 0;
}

运行结果:

0
1

2
0
5

1 5

基于数组(动态数组vector模板)实现的栈示例代码:

#include <iostream>
#include <vector>
using namespace std;

/* 基于数组实现的栈 */
class ArrayStack {
  private:
    vector<int> stack;

  public:
    /* 获取栈的长度 */
    int size() {
        return stack.size();
    }

    /* 判断栈是否为空 */
    bool isEmpty() {
        return stack.size() == 0;
    }

    /* 入栈 */
    void push(int num) {
        stack.push_back(num);
    }

    /* 出栈 */
    int pop() {
        int num = top();
        stack.pop_back();
        return num;
    }

    /* 访问栈顶元素 */
    int top() {
        if (isEmpty())
            throw out_of_range("栈为空");
        return stack.back();
    }

    /* 返回 Vector */
    vector<int> toVector() {
        return stack;
    }
};

int main(){
     ArrayStack a;
    int size=a.size();
    bool isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<endl;

    a.push(1);
    a.push(5);
    a.push(25);
    a.pop();
    int topNum=a.top();
    vector<int> vec=a.toVector();
    size=a.size();
    isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<topNum<<endl<<endl;
       for (int num : vec) { // 遍历并打印每个元素
        cout << num << " ";
    }

    return 0;
}

由于测试代码一样,所以运行结果与上面一致。

队列

队列是一种遵循先入先出规则的线性数据结构,可以在一端添加元素,并在另一端删除元素。队列模拟了排队现象,即新来的人不断加入队列尾部,而位于队列头部的人逐个离开。

我们将队列头部称为“队首”,尾部称为“队尾”,将把元素加入队尾的操作称为“入队”,删除队首元素的操作称为“出队”

队列实现原理

基于链表的实现示例代码:

#include <iostream>
#include <vector>
using namespace std;

/* 链表节点结构体 */
struct ListNode {
    int val;         // 节点值
    ListNode *next;  // 指向下一节点的指针
    ListNode(int x) : val(x), next(nullptr) {}  // 构造函数

};


/* 基于链表实现的队列 */
class LinkedListQueue {
  private:
    ListNode *front, *rear; // 头节点 front ,尾节点 rear
    int queSize;

  public:
    LinkedListQueue() {
        front = nullptr;
        rear = nullptr;
        queSize = 0;
    }

    ~LinkedListQueue() {
        // 遍历链表删除节点,释放内存
        freeMemoryLinkedList(front);
    }
    void freeMemoryLinkedList(ListNode *&front)
    {
        while(front != nullptr){
            delete front;
        }
    }
    /* 获取队列的长度 */
    int size() {
        return queSize;
    }

    /* 判断队列是否为空 */
    bool isEmpty() {
        return queSize == 0;
    }

    /* 入队 */
    void push(int num) {
        // 在尾节点后添加 num
        ListNode *node = new ListNode(num);
        // 如果队列为空,则令头、尾节点都指向该节点
        if (front == nullptr) {
            front = node;
            rear = node;
        }
        // 如果队列不为空,则将该节点添加到尾节点后
        else {
            rear->next = node;
            rear = node;
        }
        queSize++;
    }

    /* 出队 */
    int pop() {
        int num = peek();
        // 删除头节点
        ListNode *tmp = front;
        front = front->next;
        // 释放内存
        delete tmp;
        queSize--;
        return num;
    }

    /* 访问队首元素 */
    int peek() {
        if (size() == 0)
            throw out_of_range("队列为空");
        return front->val;
    }

    /* 将链表转化为 Vector 并返回 */
    vector<int> toVector() {
        ListNode *node = front;
        vector<int> res(size());
        for (int i = 0; i < res.size(); i++) {
            res[i] = node->val;
            node = node->next;
        }
        return res;
    }
};

int main(){
    LinkedListQueue a;
     int size=a.size();
    bool isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<endl;

    a.push(1);
    a.push(5);
    a.push(25);
    a.push(1);
    a.push(5);
    a.push(25);
    a.push(4);
    a.push(5);
    a.pop();
    a.pop();
    int topNum=a.peek();
    vector<int> vec=a.toVector();
    size=a.size();
    isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<topNum<<endl<<endl;
       for (int num : vec) { // 遍历并打印每个元素
        cout << num << " ";
    }
    return 0;
}

运行结果:

0
1

6
0
25

25 1 5 25 4 5

从运行结果可以看到队列和栈的不同在于删除元素在头部删除的。

数组实现队列的示例代码:

#include <iostream>
#include <vector>
using namespace std;
/* 基于环形数组实现的队列 */
class ArrayQueue {
  private:
    int *nums;       // 用于存储队列元素的数组
    int front;       // 队首指针,指向队首元素
    int queSize;     // 队列长度
    int queCapacity; // 队列容量

  public:
    ArrayQueue(int capacity) {
        // 初始化数组
        nums = new int[capacity];
        queCapacity = capacity;
        front = queSize = 0;
    }

    ~ArrayQueue() {
        delete[] nums;
    }

    /* 获取队列的容量 */
    int capacity() {
        return queCapacity;
    }

    /* 获取队列的长度 */
    int size() {
        return queSize;
    }

    /* 判断队列是否为空 */
    bool isEmpty() {
        return size() == 0;
    }

    /* 入队 */
    void push(int num) {
        if (queSize == queCapacity) {
            cout << "队列已满" << endl;
            return;
        }
        // 计算队尾指针,指向队尾索引 + 1
        // 通过取余操作实现 rear 越过数组尾部后回到头部
        int rear = (front + queSize) % queCapacity;
        // 将 num 添加至队尾
        nums[rear] = num;
        queSize++;
    }

    /* 出队 */
    int pop() {
        int num = peek();
        // 队首指针向后移动一位,若越过尾部,则返回到数组头部
        front = (front + 1) % queCapacity;
        queSize--;
        return num;
    }

    /* 访问队首元素 */
    int peek() {
        if (isEmpty())
            throw out_of_range("队列为空");
        return nums[front];
    }

    /* 将数组转化为 Vector 并返回 */
    vector<int> toVector() {
        // 仅转换有效长度范围内的列表元素
        vector<int> arr(queSize);
        for (int i = 0, j = front; i < queSize; i++, j++) {
            arr[i] = nums[j % queCapacity];
        }
        return arr;
    }
};


int main(){
    ArrayQueue a(5);
     int size=a.size();
    bool isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<endl;

    a.push(1);
    a.push(5);
    a.push(25);
    a.push(1);
    a.push(5);
    a.push(25);
    a.push(4);
    a.push(5);
    a.pop();
    a.pop();
    a.push(68);
    int topNum=a.peek();
    vector<int> vec=a.toVector();
    size=a.size();
    isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<topNum<<endl<<endl;
       for (int num : vec) { // 遍历并打印每个元素
        cout << num << " ";
    }
    return 0;
}

运行结果:

0
1

队列已满
队列已满
队列已满
4
0
25

25 1 5 68

由于不是动态数组实现的原因,所以在开始定义队列时大小就已经定义好了,无法给更改。

下面给出动态数组的实现示例代码:

#include <iostream>
#include <vector>
using namespace std;
/* 基于环形数组实现的队列 */
class ArrayQueue {
  private:
    vector<int> queue;
  public:
    /* 获取队列的容量 */
    int capacity() {
        return queue.capacity();
    }

    /* 获取队列的长度 */
    int size() {
        return queue.size();
    }

    /* 判断队列是否为空 */
    bool isEmpty() {
        return queue.size() == 0;
    }

    /* 入队 */
    void push(int num) {
       queue.push_back(num);
    }
    int pop(){
        int num=queue.front();
        queue.erase(queue.begin());
        return num; 
    }

    /* 访问队首元素 */
    int peek() {
        if (isEmpty())
            throw out_of_range("队列为空");
        return queue.front();
    }

    vector<int> toVector() {
        return queue;
    }
};


int main(){
    ArrayQueue a;
     int size=a.size();
     int popNum;
    bool isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<endl;

    a.push(1);
    a.push(5);
    a.push(25);
    a.push(1);
    a.push(5);
    a.push(25);
    a.push(4);
    a.push(5);
    popNum=a.pop();
    cout<<popNum<<endl;
    popNum=a.pop();
    cout<<popNum<<endl<<endl;
    a.push(68);
    int topNum=a.peek();
    vector<int> vec=a.toVector();
    size=a.size();
    isempty=a.isEmpty();
    cout<<size<<endl<<isempty<<endl<<topNum<<endl<<endl;
       for (int num : vec) { // 遍历并打印每个元素
        cout << num << " ";
    }
    return 0;
}

运行结果为:

0
1

1
5

7
0
25

25 1 5 25 4 5 68
posted @ 2024-10-31 10:44  无情马里奥  阅读(57)  评论(0)    收藏  举报