美文网首页
c++ stack栈 - queue对列 - list双向链表

c++ stack栈 - queue对列 - list双向链表

作者: 简书网abc | 来源:发表于2024-01-12 00:38 被阅读0次
#include <future>
#include <iostream>
#include <stack>

using namespace std;

// 栈 不提供迭代器和遍历, 先进后出
void test01() {
    stack<int> s;
    s.push(10); // 进栈容器
    s.push(20);
    s.push(30);

    if( !s.empty() ) {
        cout << "栈的大小: " << s.size() << endl;
        while (!s.empty()) {
            cout << s.top() << " "; // 取出栈顶元素.
            s.pop();                // 删除栈顶元素.
        }
        cout << endl;
    }
    cout << "test01 end ... stack 栈操作 " << endl;
}

// 队列 不提供迭代器和遍历 先进先出
#include <queue>
void test02() {
    queue<int> q;
    q.push(10); // 进对列容器
    q.push(20);
    q.push(30);

    if( !q.empty() ) {
        cout << "队列的大小: " << q.size() << endl;
        while (!q.empty()) {
            cout << q.front() << " "; // 从对列头部取出数据,火车头.
            q.pop();                // 删除对列头部数据.
        }
        cout << endl;
    }
    cout << "test01 end ... queue 对列操作 " << endl;
}


#include <list>
void printListInt(list<int> &li) {
    list<int>::iterator it = li.begin();
    for (;  it != li.end() ; it++) {
        cout << *it << " ";
    }
    cout << endl;
}

// 双向链表 list
void test03() {
    list<int> li;
    li.push_back(10);
    li.push_back(20);
    li.push_back(30);
    li.push_back(40);
    li.push_back(50);
    printListInt(li);

    // list容器是双向迭代器,不支持+2, 支持++
    list<int>::iterator it = li.begin();
    it++;
    it++;
    li.insert(it, 3, 99);
    printListInt(li);

    // 链表的排序,标准stl-sort不支持
    li.sort();
    printListInt(li);
    li.reverse();           // 链表反转
    printListInt(li);

    cout << "test03 end ... list 双向链表操作 " << endl;
}

int main() {
    test01();
    test02();
    test03();
    return 0;
}

相关文章

  • STL容器

    STL容器类型 序列式容器:vector,list(双向链表),deque,stack,queue,heap,pr...

  • 数据结构

    一、常用的数据结构 ** djfdkfj 数组Array栈Stack队列Queue链表Linked List树Tr...

  • 数据结构部分

    栈 Stack队列 Queue链表 Linked List数组 Array哈希表 Hash Table二叉树 Bi...

  • Collection和Collections

    Collection是一个InterfaceSet List Queue(先进先出队列) Deque(双向链表)都...

  • C# 中的集合

    集合(Collection)类是专门用于数据存储和检索的类。这些类提供了对栈(stack)、队列(queue)、列...

  • 常用数据结构

    数组Array,集合(List,Set,队列Queue,栈Stack),散列表Map 一、数组:长度固定,元素类型...

  • C++学习四:高级

    STL容器相关 一、vector向量 二、stack栈 三、队列queue 四、list 五、set 六、谓词 七...

  • container/list

    container/list list是一个双向链表。可以用来实现队列和栈结构。 用例

  • 数据结构与算法之数组与链表

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 数据结构与算法之栈与队列

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

网友评论

      本文标题:c++ stack栈 - queue对列 - list双向链表

      本文链接:https://www.haomeiwen.com/subject/mflxodtx.html