堆栈是空的...为什么?

灾难大师

我正在处理涉及堆栈和队列的ac#program。队列使输入字符串入队,同时在此期间对来自队列的输入执行堆栈操作时将其出队。

现在发生的事情是在checkMatch()中,程序给出了一个异常错误,指出堆栈为空。我已经使用了调试/调试功能,但我发现在checkMatch函数中,堆栈确实是空的,我也不明白为什么。我已经在c ++中完成了相同的程序来对其进行测试,而在c ++中我根本没有收到此错误,实际上,我得到了我想要的输出。

但是经过一整天的长时间研究,并尝试了一堆东西,包括浅拷贝,克隆等。当程序进入checkMatch函数时,我似乎仍然无法使堆栈包含某些东西。在调试活动中,我知道在checkMatch函数中堆栈和输入已重置,因此为什么我收到此堆栈为空的异常。

这是代码:

public static void loadtheInput(Queue<string> input)
{
    input.Enqueue("A"); 
    input.Enqueue("B");
    input.Enqueue("C");
    input.Enqueue("D");
}

public static void printtheLine(StreamWriter DisplayOutTxt, Queue<string> sameMatch, Stack<string> stack, Queue<string> input, string operations)
{
    string returnMatched = "";
    string returnStack = "";
    string returnInput = "";

    if (stack.Count == 0) //if stack is empty, printtheLine so DisplayOutTxt the table header
    {
        stack.Push("A"); 
        stack.Push("C");
    }
    returnMatched = printQueue(matched);
    returnStack = printStack(stack);
    returnInput = printQueue(input);
}

public static string printStack(Stack<string> stack)
{
    string DisplayOutTxt = "";
    while (stack.Count > 0) 
    {
        DisplayOutTxt += stack.Peek(); 
        stack.Pop(); 
    }
    return DisplayOutTxt;
}

private static string printQueue( Queue<string> queue)
{
    string DisplayOutTxt = "";

    if (queue.Count == 0) //if the queue is empty
    {
        DisplayOutTxt = " "; //set DisplayOutTxt to a space
    }
    else
    {
        while (queue.Count > 0) //queue not empty
        {
            DisplayOutTxt += queue.Peek(); //concat front of queue to DisplayOutTxt
            queue.Dequeue(); //dequeue the front string
        }
    }
    return DisplayOutTxt;
}

public static void checkMatch(StreamWriter DisplayOutTxt, Queue<string> sameMatch, Stack<string> stack, Queue<string> input, ref string operations)
{
    printtheLine(DisplayOutTxt, sameMatch, stack, input, operations); //print line of DisplayOutTxt

    //here is where i start facing the problem. stack (and input) are both empty once they step into this checkMatch function!
    //I think its a reference issue, but i just cant figure out what to do after everything Ive tried

    if (stack.Peek() == input.Peek()) //if the next stuff in stack and input match each other
    {
        // some code is here
    }
}

static int Main()
{
    StreamWriter DisplayOutTxt = new StreamWriter("output.txt");

    Queue<string> sameMatch = new Queue<string>();
    Stack<string> stack = new Stack<string>();
    Queue<string> input = new Queue<string>();

    string operations = "";
    loadtheInput(input); //load input into input queue and load all productions into parse table

    while (input.Count > 0) //while input vector is not empty
    {
        checkMatch(DisplayOutTxt, sameMatch, stack, input, ref operations); //call function to check for sameMatch stuff
    }
    DisplayOutTxt.Flush();
    DisplayOutTxt.Close();
    return 0;
}

继承人一些调试/步距的图像我没有以确定由被输入的checkMatch功能时的栈计数

继承人异常错误图像

珍妮·马蒂卡宁(Janne Matikainen)

在您的printStack函数中,您正在清除堆栈。通过循环并弹出每个项目。

请参阅此处,如何在不弹出堆栈项目的情况下打印它们。

在C#中,Stack参数将是引用类型,因此在函数中对其进行修改将使原始Stack发生变化。但是,随着Stack实现IEnumerable,您可以枚举项目,而无需修改原始项目。

你可以用这样的东西

public static string printStack(IEnumerable<string> stack)
{
    string DisplayOutTxt = "";

    foreach (var obj in stack)
    {
        DisplayOutTxt += obj;
    }

    return DisplayOutTxt;
}

但是要做起来要容易得多

returnStack = string.Join("", stack);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么将空节点放入堆栈

来自分类Dev

为什么在创建自己的异常时得到空的堆栈跟踪?

来自分类Dev

为什么堆栈不回滚

来自分类Dev

为什么堆栈不回滚

来自分类Dev

为什么ViewBag是空的?

来自分类Dev

为什么堆栈使用数组,为什么不使用链表

来自分类Dev

如果在类内的构造函数<2中启动了空array [],为什么检测到堆栈破坏

来自分类Dev

为什么这会导致C堆栈溢出?

来自分类Dev

为什么我的堆栈跟踪缺少步骤?

来自分类Dev

为什么异常堆栈跟踪未登录?

来自分类Dev

为什么在MEAN堆栈中使用mongoDB

来自分类Dev

为什么不总是发生堆栈溢出?

来自分类Dev

为什么要使用神经堆栈?

来自分类Dev

为什么以下原因堆栈溢出?

来自分类Dev

为什么会变成堆栈溢出?

来自分类Dev

为什么我的堆栈剪裁图像的右侧?

来自分类Dev

为什么我的堆栈跟踪被截断?

来自分类Dev

为什么这会导致C堆栈溢出?

来自分类Dev

用Java堆栈,为什么要使用if?

来自分类Dev

为什么引导模式关闭事件“堆栈”?

来自分类Dev

为什么FormData为空?

来自分类Dev

为什么数组为空

来自分类Dev

为什么孩子是空的?JavaFX

来自分类Dev

为什么我的gridview是空的?

来自分类Dev

为什么退回的承诺是空的?

来自分类Dev

为什么git taggerdate是空的?

来自分类Dev

为什么JsonSerializer给出空?

来自分类Dev

为什么打印为空?

来自分类Dev

为什么UITableView为空?