【问题标题】:Need to insert Stack into List需要将堆栈插入列表
【发布时间】:2015-05-31 13:41:04
【问题描述】:

我有一堆从 0 到 17 的数字,我需要将该堆栈放入 List 的第一个位置如何使用函数 Insert?还是我必须以某种方式更改 Insert ?

这是我的列表结构

struct List
{
    int data;
    struct List *Next;
};

这就是我的插入方式

void Insert(List **pps, int prvek)
{
    List *ps;
    ps = (List *)malloc(sizeof(List));
    if (!ps) {
        return;
    }
    ps->data = prvek;
    ps->Next = *pps;
    *pps = ps;
}

【问题讨论】:

  • 您忘记为编程语言添加标签
  • 用“我有一堆……”来定义你的意思;我认为还不清楚。另外,究竟你想插入什么?您的清单包含哪些类型的项目?
  • 只在前面插入和删除的列表是一个堆栈。
  • 您的插入功能对我来说看起来不错,您遇到什么问题?
  • 我认为这个 List 可以作为堆栈使用,因为它将新事物放在顶部;所以列表中的这个Insert 正在堆栈上执行Push

标签: c++ arrays list stack


【解决方案1】:

所以从您的评论中,我了解到您需要一个堆栈列表。您当前的代码已经能够创建堆栈。您可以将该堆栈存储到vector

如下所示,

#include <vector>

std::vector<List*> stackList;

stackList.push_back(firstStack);
stackList.push_back(secondStack);

....

【讨论】:

  • @Kunal 它给了我这个错误:/stackList.push_back(T); IntelliSense:没有重载函数实例“std::vector<_ty _alloc>::push_back [with _Ty=List *, _Alloc= std::allocator]" 匹配的参数列表参数类型是:(堆栈)对象类型是:std::vector>
猜你喜欢
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
相关资源
最近更新 更多