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

时间:2016-10-22 09:14:35

标签: c#

我正在处理一个涉及堆栈和队列的c#程序。队列将输入字符串排队,并在对队列的输入进行堆栈操作的同时将其出列。

现在发生的事情是在checkMatch()中,程序给出了一个异常错误,表明Stack是空的。 我已经使用了调试/步骤,我在checkMatch函数中看到Stack真的是空的,我不明白为什么。 我用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;
}

heres我在确认输入checkMatch函数时的堆栈计数时所做的一些调试/步骤的图像

heres异常错误图片

1 个答案:

答案 0 :(得分:1)

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

请参阅here如何打印堆叠物品而不弹出它们。

在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);