标签: stack

如何使用堆栈的反向方法?

我需要使用该Reverse方法反转堆栈的顺序。

但是我已经尝试过了,但是它不起作用:

    Dim StackObject As New Stack(Of String)
    StackObject.Push("S")
    StackObject.Push("T")
    StackObject.Push("A")
    StackObject.Push("C")
    StackObject.Push("K")

    StackObject = StackObject.Reverse.Cast(Of String)() ' <-- InvalidCastException

    For Each str As String In StackObject
        MsgBox(str)
    Next
Run Code Online (Sandbox Code Playgroud)

我编写了这个通用函数来反转堆栈,但是我想使用Reverse方法而不是所有这些不必要的代码来做到这一点:

Private Function Reverse_Stack(Of T)(stack As Stack(Of T))

    Dim new_stack As New Stack(Of T)

    While Not stack.Count = 0
        new_stack.Push(stack.Pop)
    End While

    Return new_stack

End Function
Run Code Online (Sandbox Code Playgroud)

.net linq vb.net stack types

-1
推荐指数
1
解决办法
724
查看次数

在C++中实现堆栈类

我正在尝试使用链表实现堆栈类这里是我的stack.h

// File: Stack.h

#ifndef STACK_H
#define STACK_H
class Stack
{

private:
    struct linklst{
        int num;
        int* next;
    };

    linklst* top;

public:
    Stack();
    ~Stack();

    void push(int i);
    int pop();
    bool isempty();

};

#endif
Run Code Online (Sandbox Code Playgroud)

和我的堆栈.cpp

// Stack.cpp

#include"Stack.h"
using namespace std;

 Stack::Stack(){
     top = new linklst();
     top->num = -1;
     top->next = nullptr;

 };

 Stack::~Stack() {

     linklst * r = new linklst();
     while (true)
     {

         r = top;
         top = top->next;
         delete r;


     }

     delete top;


 };

 void Stack::push(int i){

     linklst * …
Run Code Online (Sandbox Code Playgroud)

c++ stack pointers linked-list

-1
推荐指数
1
解决办法
2158
查看次数

C++:格式不是字符串文字,也不是格式参数

我一直在尝试打印一个字符串文字,但似乎我做错了,因为我收到了编译警告.这可能是由于错误的格式化或我对c_str()功能的误解,我认为应该返回一个string.

parser.y: In function ‘void setVal(int)’:
parser.y:617:41: warning: format not a string literal and no format arguments [-Wformat-security]
Run Code Online (Sandbox Code Playgroud)

617行:

sprintf(temp, constStack.top().c_str());
Run Code Online (Sandbox Code Playgroud)

有这些声明

#include <stack>
const int LENGTH = 15;
char *temp = new char[LENGTH];
stack<string> constStack;
Run Code Online (Sandbox Code Playgroud)

如何为字符串提供正确的格式?

c++ stack char bison

-1
推荐指数
1
解决办法
7059
查看次数

C segfaults中的大型阵列,时代的Sievs

所以,我有这个代码.

void main(void){

    int n = 9999;
    int *array = calloc(n, sizeof(int));
    int i, j;

    // Populate array up to N
    for(i = 0; i < n; i++){
        array[i] = 2 + i;
    }

    // Run sievs
    sievs(array, n);

    print_prime(array, n);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果N足够大,程序将核心转储.我的理论是在堆栈上分配了一些东西,并且它不足以容纳那么多数据,但是我使用的是calloc,所以它应该是heep.

这两个打印函数看起来像这样:

void print_prime(int *a, int n){
    int i;
    for (i = 0; i < n; i++){
        if(a[i] != -1)
            printf("Prime: %d \n", a[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有任何此代码导致它?我不明白为什么.

这是siev功能:

void sievs(int *array, int n){

    int i, j;

    for(i = …
Run Code Online (Sandbox Code Playgroud)

c memory stack

-1
推荐指数
1
解决办法
73
查看次数

错误:非法启动类型

为什么这一小段代码在第6行和第10行(for循环)中给出了非法的类型错误启动....我找不到任何不匹配的大括号......

class StackDemo{
    final int size = 10;
    Stack s = new Stack(size);

    //Push charecters into the stack
    for(int i=0; i<size; i++){
        s.push((char)'A'+i);
    }
    //pop the stack untill its empty
    for(int i=0; i<size; i++){
        System.out.println("Pooped element "+i+" is "+ s.pop());
    }
}
Run Code Online (Sandbox Code Playgroud)

我实现了Stack类,

java stack compiler-errors

-1
推荐指数
1
解决办法
2万
查看次数

字符串不可变?

有人能回答我吗?

public class ReplaceString{

    public static void main(String arg[]) {
        String a = "Hariom";
        a = a.replace('H', 'b');
        System.out.println(a);
    }
}
Run Code Online (Sandbox Code Playgroud)

java string heap stack immutability

-1
推荐指数
2
解决办法
237
查看次数

堆栈与C#中的类型参数错误

我正在使用.NETFramework,Version = v4.5并尝试创建以下堆栈:

using System;
using System.Collections;
using System.Collections.Generic;
...
Stack<int> s = new Stack<int>(); 
Run Code Online (Sandbox Code Playgroud)

为什么我收到此错误?

类'System.Collections.Stack'没有类型参数

没有类型参数的堆栈工作正常.

.net c# generics stack

-1
推荐指数
1
解决办法
101
查看次数

如何在C++ 11中声明堆栈引用?

我正在实现堆栈引用.但是我收到了"Segmentation fault(core dumped)"的错误.我在Ubuntu 14.04上使用g ++(Ubuntu 4.8.2-19ubuntu1)4.8.2.非常感谢.

代码如下所示.

#include<iostream>
#include<stack>

using namespace std;

int main() {
    stack<int>* S;
    S->push(4);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ stack c++11

-1
推荐指数
2
解决办法
107
查看次数

内存是分配:堆还是堆栈?

#include <iostream>
using namespace std;

struct number
{
    int value;
    int pos;
public:
    number(int a,int b)
    {
        value=a;
        pos=b;
    }
};
int main() {
    // your code goes here
    number(1,2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,在哪里分配内存number(1,2)?在堆或堆栈上?我们如何验证它?

c++ memory heap stack

-1
推荐指数
1
解决办法
87
查看次数

如何确定嵌入式程序使用的堆栈空间大小

我想知道确定程序使用多少堆栈空间的最佳方法是什么,是否有任何技术或工具来生成统计信息,而不是手动计数?

该程序希望分析是代码编写器中的C程序,如果这有所不同.

谢谢

c embedded stack stack-trace

-1
推荐指数
1
解决办法
152
查看次数