标签: stack

这段代码如何跳转到最后一个字符串并反转堆栈?

我正在学习C,这是一个自学成才的事情.这很好,但是当我有问题时会变得毛茸茸,所以我希望得到一些帮助!

我有一些代码,我被指示写,我理解代码,我理解堆栈如何工作(取一个,进入另一堆等),main()并将始终先运行.

但我不明白为什么,在numberOfBottles达到0之后,它不会继续循环.为什么它首先反转堆栈,它是如何跳转到最后一个字符串的呢?

#include <stdio.h>

void singSongFor(int numberOfBottles)
{
    if (numberOfBottles == 0)
    {
      printf("there are simply no more bottles of beer on the wall. \n\n");
    } 
    else 
    {
        printf("%d bottles of beer on the wall. %d bottles of beer. \n", numberOfBottles, numberOfBottles);
        int oneFewer = numberOfBottles - 1;
        printf("Take one down, pass it around %d bottles of beer on the wall. \n\n", oneFewer);

        singSongFor(oneFewer);
        printf("Put a bottle in the recycling, %d empty bottles …
Run Code Online (Sandbox Code Playgroud)

c stack

0
推荐指数
1
解决办法
90
查看次数

用C++编写一个简单的堆栈

嗨,我在C++中创建一个简单的堆栈类,并且是C++的新手.我有一些错误,我无法弄清楚它们的含义.一些帮助将不胜感激!这是我的代码:

Stack.h

#ifndef SStack
#define  SStack

#include <cstdlib>
#include <string>

class SStack
{
        public:
                // Constructor
                SStack( int cap);
                // Copy Constructor
                SStack( const SStack& s );
                ~SStack( );
                void push ( const std::string&  s);
                std::string& pop ();
                std::string&  top () const;
                bool IsEmpty () const;
                int size() const;
        int getCapacity() const;

        // NONMEMBER FUNCTIONS for the bag class
        // Precondition:  s1.size( ) + s2.size( ) <= s1.Capacity.
        // Postcondition: The stack returned is the union of s1 and s2. …
Run Code Online (Sandbox Code Playgroud)

c++ stack

0
推荐指数
1
解决办法
298
查看次数

C语言中的标识符存储在哪里

只是感兴趣.我正在修改我长期被遗忘的生锈的C知识,我想知道在哪里存储声明变量的标识符?

例如,如果我在main中声明以下char值1.

int main(){
char value1 = 'A';
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道char将存储在堆栈框架中的堆栈中.让我们说如果我们在value1上做了一个地址,就像这个和value1一样.我们会得到像0xF37012(一个假设的数字).这将是值所在的位置,但标识符呢?这与0xF37012有什么关联?是否在主要的某个地方的堆栈frane?或者所有标识符都存储在某个全局表或列表中?

c memory stack identifier

0
推荐指数
1
解决办法
337
查看次数

将堆栈传递给方法

将Stack传递给方法的正确方法是什么?我收到一个错误,说我的方法调用有一些无效的参数.

string userInput = inputText.Text;
Stack<double> numbers= new Stack<double>();
Stack<char> operators = new Stack<char>();
bool output;

output = calculateExpression(userInput, Stack<double> numbers, Stack<char> operators);
Run Code Online (Sandbox Code Playgroud)

我的定义:

double calculateExpression(string userInput, Stack<double> numbers, Stack<char> operators)
{}
Run Code Online (Sandbox Code Playgroud)

c# stack parameter-passing

0
推荐指数
1
解决办法
60
查看次数

添加新元素后,为什么所有元素都在堆栈中被替换?

C中的简单堆栈实现:

struct Stack {
  char* data;
  Stack* prev;
};

void push(char* data, Stack** stack) {
  Stack* node = (Stack*)malloc(sizeof(Stack));
  node->data = data;
  node->prev = *stack;
  *stack = node;
}

int main() {
  Stack* top = NULL;
  push("1", &top);
  push("2", &top);
  push("3", &top);
}
Run Code Online (Sandbox Code Playgroud)

然后top->prev->prev->data结果3top->prev->data和相同top->data.

谁能解释为什么?

c stack pointers data-structures

0
推荐指数
1
解决办法
55
查看次数

在另一个类中使用类函数

我有一个完美运行的LinkedList.h头文件.我试图使用LinkedList来实现堆栈.问题在于使用此MyStack类中LinkedList类的函数.InsertFront(key)是LinkedList类中的一个函数.现在,当我运行它时,我得到以下2个错误.

'list':未声明的标识符

'.InsertFront'的左边必须有class/struct/union

这两个都引用了以下代码的最后一行:

#include "LinkedList.h"
#include <iostream>
using namespace std;



class MyStack
{
    private:
    //LinkedList list;


    public:
        //Implement the following functions whose purpose should be obvious
        MyStack();
        ~MyStack();
        void Push(int key);
        int Pop();
        bool isEmpty();
        int Size();
        void Display(); // Just show the elements from top of stack to the bottom without removing them
        int Top(); // Return the element at the top of the stack without removing it
};


MyStack::MyStack()
{
    LinkedList list;
}

MyStack::~MyStack()
{

}

void …
Run Code Online (Sandbox Code Playgroud)

c++ stack class linked-list header-files

0
推荐指数
1
解决办法
74
查看次数

混淆大会CALL,RET语句

我想知道什么时候"CALL"指令执行.它首先在堆栈或函数参数上推送返回地址?

谢谢

assembly stack masm nasm

0
推荐指数
1
解决办法
215
查看次数

Ruby:堆栈级别太深(SystemStackError)

我试着解决这个问题http://www.nattee.net/~dae/algo/prob/hw03b_tiling/problem.pdf

所以我使用分而治之的方法来解决它,但是当我执行我的程序时,我得到了

tile.rb:7: stack level too deep (SystemStackError)
Run Code Online (Sandbox Code Playgroud)

这是我的代码

def tile (x, y, bx, by, ex, ey)
    mx = (bx+ex)/2
    my = (by+ey)/2

    if (by<=y && y<=my)
        if (bx<=x && x<=mx) # top-left
            puts "0 #{mx} #{my}"
        elsif (mx+1<=x && x<=ex) # top-right
            puts "1 #{mx} #{my}"
        end
    elsif (my+1<=y && y<=ey)
        if (bx<=x && x<=mx) # bottom-left
            puts "2 #{mx} #{my}"
        elsif (mx+1<=x && x<=ex) # bottom-right
            puts "3 #{mx} #{my}"
        end
    end

    tile(x,y,bx,by,mx,my) #top-left
    tile(x,y,mx+1,by,ey,my) #top-right
    tile(x,y,bx,my+1,mx+1,ey) …
Run Code Online (Sandbox Code Playgroud)

ruby algorithm stack

0
推荐指数
1
解决办法
2951
查看次数

i386 C进程 - 剩下的1 GB可寻址空间会发生什么?

据记载,在堆上分配的变量存储在低地址区域中并向堆栈增长,反之亦然.我决定测试一下:

#include <stdio.h>
#include <stdlib.h>

const char my_const_global_var = '0';
char my_global_var = '0';

int main(void) {
    char my_stack_var = '0';
    char* my_heap_var = (char*) malloc(1);
    *my_heap_var = '0';
}
Run Code Online (Sandbox Code Playgroud)

看起来my_const_global_var并且my_global_var在低地址区域(000XXXXX在堆之前和之后不久)解决了,但令我惊讶的是,my_stack_var正好在75%左右(大约是bffbdaXX).我猜我的全局/我会得到一个段错误)堆/堆栈变量超过3 GB的内存,所以我做了一个搜索,发现提到了一个3 GB的屏障,但没有提到剩下的1 GB可寻址空间会发生什么.

剩下的25%的内存地址空间会发生什么?

c memory heap stack

0
推荐指数
1
解决办法
104
查看次数

为什么我无法在C中为我的结构创建新节点?(使用Netbeans)

#include<stdio.h>
#include<stdlib.h>

struct node{
 int info;
 node *link;
 };

 node *top = NULL;
 void push();
 void pop();
 void display();

 main()
 {
  int choice;

  while(1)
  {
  printf("Enter your choice:\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
  scanf("%d",&choice);

  switch(choice)
  {
   case 1:
    push();
    break;
   case 2:
    pop();
    break;
   case 3:
    display();
    break;
   case 4:
    exit(1);
    break;
   default:
    printf("Wrong choice");
   }
//   getch();
  }
 }

 void push()
 {
  node *tmp;
  int pushed_item;
  tmp = new node;
  printf("Enter the value to be pushed in the stack:");
  scanf("%d",&pushed_item);
  tmp->info=pushed_item;
  tmp->link=top;
  top=tmp;
 } …
Run Code Online (Sandbox Code Playgroud)

c stack struct pointers netbeans

0
推荐指数
1
解决办法
84
查看次数