好吧,最近在一次采访中有人问我这个问题,我对此很感兴趣。基本上,我有一个带有一组特定值的堆栈,我想在函数中传递堆栈对象,并在某个索引处返回该值。这里要注意的是,在函数完成之后,我需要未修改的堆栈。这很棘手,因为Java会按值传递对象的引用。我很好奇,如果有纯粹是一个Java的方式做使用push(),pop(),peek(),isempty()和原始数据类型。我反对将元素复制到数组或字符串中。目前,我得到的最干净的是使用克隆,找到下面的代码:
import java.util.Stack;
public class helloWorld {
public int getStackElement( Stack<Integer> stack, int index ){
int foundValue=null;//save the value that needs to be returned
int position=0; //counter to match the index
Stack<Integer> altStack = (Stack<Integer>) stack.clone();//the clone of the original stack
while(position<index)
{
System.out.println(altStack.pop());
position++;
}
foundValue=altStack.peek();
return foundValue;
}
public static void main(String args[]){
Stack<Integer> stack = new Stack<Integer>();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.push(60);
helloWorld obj= new helloWorld();
System.out.println("value is-"+obj.getStackElement(stack,4)); …Run Code Online (Sandbox Code Playgroud) 该文章说,下面的代码将炸毁堆栈:
function fetchLatest() {
$.getJSON('/wait?id=' + last_seen, function(d) {
$.each(d, function() {
last_seen = parseInt(this.id, 10) + 1;
ul.prepend($('<li></li>').text(this.text));
});
fetchLatest();
});
}
Run Code Online (Sandbox Code Playgroud)
客户端JavaScript只是使用jQuery的getJSON方法对一个简单的URL端点执行长轮询这种递归操作可能是一个坏主意,因为它最终会破坏浏览器的JavaScript堆栈,但它可以用于演示.
这是真的吗?如果是这样,我们怎么能阻止这个?
我得到一个包含运算符+, - ,*,/和括号的算术公式(可能会也可能不会改变运算符的自然优先级).一个例子如下:a/b + f - (c + d)*e - a*c.并且我被要求使用堆栈(实现为链接列表)以跟踪操作数和运算符:我的程序应该如何工作的示例如下:
我难以理解的问题是如何区分操作数的优先级!
这是我写的代码的不完整版本:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct btnode Btree;
typedef struct node s_Node;
struct btnode {
char info;
Btree *left;
Btree *right;
};
struct node {
char element;
s_Node*next;
};
typedef struct{
s_Node *top_stack;
} stack_t;
int IsOperator(char c);
main () {
FILE* fp;
stack_t …Run Code Online (Sandbox Code Playgroud) 请看一下我的代码.我试图将一些Car对象添加到MutableArray*parkPlace(属性属于Park类)
我期待的结果应如下所示:
CarParkSim[2662:303] Parking: Car Number:1 At:3 o'clok Position:0
CarParkSim[2662:303] Parking: Car Number:2 At:4 o'clok Position:1
CarParkSim[2662:303] Parking: Car Number:3 At:5 o'clok Position:2
Run Code Online (Sandbox Code Playgroud)
但是,我得到的实际结果是这样的:
2012-12-22 12:13:46.085 CarParkSim[2662:303] Parking: Car Number:1 At:3 o'clok Position:0
2012-12-22 12:13:46.087 CarParkSim[2662:303] Parking: Car Number:2 At:4 o'clok Position:0
2012-12-22 12:13:46.088 CarParkSim[2662:303] Parking: Car Number:3 At:5 o'clok Position:0
Run Code Online (Sandbox Code Playgroud)
我确实检查了NSmutablearray,它是空的......似乎把任何东西都推到了堆栈......任何建议?谢谢
无论如何,这里是代码:Park类:
#import <Foundation/Foundation.h>
#import "Stack.h"
#import "Car.h"
@interface Park : NSObject
@property(strong,nonatomic)NSMutableArray *parkPlace;
@property(strong,nonatomic)Car *car;
-(void)carMoveIntoPark:(Car *)car;
@end
#import "Park.h"
@implementation Park
-(void)carMoveIntoPark:(Car *)car
{
if ([self.parkPlace count] …Run Code Online (Sandbox Code Playgroud) 所以我,试图解决一项任务.一个已经有代码,但系统输出,"堆栈溢出"我是c +中的新手,我的英语不好所以我很抱歉误解=)
#include <iostream>
using namespace std;
int main (){
int n;
int x;
int k = 0; // ??????? ??? ???????? ???????
int a [200000];
scanf("%d\n",&n);
for (int i = 0; i< n; ++i){
std::cin >> x;
if (x > 0){
k++;
a[k] = x;
}else if(x == 0){
for (int q = 1; q <= k; ++q){ // ???????????
a[k+q] = a[q];
}
k *= 2;
}else{
printf("%d %d\n",a[k],k);
k--;
}
}
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
看起来像算法正常工作,但只有问题是堆栈.非常感谢!
我有一个类Floor有一个Stack块,我不知道如何初始化它.我试过这样的:
public class Floor {
private Stack<Block> stack;
private static int size;
public void setStack(Stack<Block> stack) {
this.stack = stack;
}
public void addBlock(Block b){
stack.push(b);
}
}
public class InputDevice {
Block a0=new Block('I',false);
Floor [] floor=new Floor[5];
Stack<Block> stack=new Stack<Block>();
floor[0].setStack(stack);
floor[0].addBlock(a0);
}
Run Code Online (Sandbox Code Playgroud) 我是java的新手,我正在尝试编写一个Linked-List Stack ..
public class Stack {
private Node first;
private class Node {
int item;
Node next;
}
public boolean IsEmpty()
{
return first==null;
}
public void push(int item)
{
Node oldfirst=first;
first=new Node();
first.item=item;
first.next=oldfirst;
}
public int pop ()
{
int item=first.item;
first=first.next;
return item;
}
}
Run Code Online (Sandbox Code Playgroud)
import javax.swing.*;
public class main {
public static void main(String[] args) {
Stack ob=null;
int num=0;
while (true)
{
num=Integer.parseInt(JOptionPane.showInputDialog("Enter the number"));
ob.push(num);
if (num==0)
break;
}
int k;
k=ob.pop(); …Run Code Online (Sandbox Code Playgroud) 以下代码的行为让我感到惊讶:
map<string,long>* map_ptr;
if(true){
map<string, long> my_map;
my_map["a"] = 1;
my_map["b"] = 2;
my_map["c"] = 3;
map_ptr = &my_map;
}
//works ONLY IF this for loop comes before the next for loop
for(map<string, long>::iterator itr = map_ptr->begin(); itr != map_ptr->end(); ++itr)
cout << itr->second << endl;
// does not work prints random bits
for(map<string, long>::iterator itr = map_ptr->begin(); itr != map_ptr->end(); ++itr)
cout << itr->first << endl;
Run Code Online (Sandbox Code Playgroud)
我知道在if语句中创建的变量只包含其中的范围,但我认为声明指向它们的指针将起作用.我对这段代码的堆栈结构知之甚少,但我认为变量虽然超出了范围,但仍然存在于堆栈中,因此它们仍然存在.但似乎发生了比我所知更多的事情.让我感到惊讶的是,为什么第一个for循环打印出正确的东西,但只有在第二个for循环之前执行它.我想这与该类型有关,因为long它内置于string不存在的地方,但这远远不足以解释正在发生的事情.
请帮我理解这段代码.谢谢!
我在哪里可以找到Haskell中Stack的一个很好的解释.目前我所知道的是:
传统的抽象数据类型完全隐藏了数据的内部表示
可以使用列表实现多态堆栈,而无需告知消费者其内部工作方式.
我正在OCaml中实现一个Prolog解释器.我遇到的问题是主要功能.我本质上是试图将我的解释器堆栈存储在函数调用中,并修改此堆栈的副本,然后将其传递给此特定函数调用的递归调用.当该递归调用报告失败时,此原始函数调用应该使用我保持未修改的原始堆栈并进行不同的递归调用(以实现回溯).
现在,这是问题所在.当我的意图只是修改tempstack时,堆栈和临时堆栈(tempstack)都被修改了.我花了好几个小时试图找出问题,我很确定这就是它.这是主要的功能片段..
let rec main stack clauselist counter substitutions variablesinquery answers =
try
let currentsubgoal = Queue.top (Stack.top stack) in
if counter <> List.length clauselist
then
let tempstack = Stack.copy stack in
try
let unifier = mgu1 currentsubgoal (List.nth clauselist counter) in
let newsubgoals =
match List.nth clauselist counter with
Fact(a) -> []
| Rule(Headbody(a,b)) -> b
in
let tempstack = stacknewsubgoals tempstack unifier newsubgoals in
let tempsubs = match substitutions with
S(m) -> match unifier with S(n) -> S(m …Run Code Online (Sandbox Code Playgroud) stack ×10
java ×3
c++ ×2
binary-tree ×1
c ×1
haskell ×1
interface ×1
interpreter ×1
javascript ×1
null ×1
object ×1
objective-c ×1
ocaml ×1
pointers ×1
prolog ×1
reference ×1