添加多个节点

时间:2016-10-21 05:25:18

标签: c++ linked-list stack bad-alloc

背景:
我们正在课堂上学习链表和堆栈。我无法理解如何/在何处声明新堆栈。

问题:
我的程序一直工作,直到它第二次需要添加一个新节点,然后它崩溃并在内存位置给出了这个错误" ... std :: bad_alloc的未处理异常..."

经过几次调试程序(因为它崩溃了)我没有重新启动视觉工作室就无法编译,这非常可怕。)

代码段:

Source.cpp

    #include<iostream>
    #include<string>
    #include<fstream>
    #include"ListStack.h"
    using namespace std;

    void openInputFile(ifstream&);
    string delimiterMatching(ifstream&);


    void main() {

        ifstream inFile;
        openInputFile(inFile);
        string errorMessage = delimiterMatching(inFile);
        inFile.close();

    }

    void openInputFile(ifstream &inputFile) {
        //...
        string fileName;
        cout << "File path: ";
        getline(cin, fileName); //Inputing file name from user

        inputFile.open(fileName.c_str()); //Opening the file
        //...
    }

    string delimiterMatching(ifstream &myFile) {

    char myChar;
    char findThis;
    char nextChar;
    char nextNextChar;
    LLStack myStack;

    cout << "Checking file!\n";

    myFile >> myChar;
    while (!myFile.eof()) {
        if (myChar == '(' || myChar == '[' || myChar == '{') {
            if (myChar == '(') {
                findThis = ')';
            }
            else if (myChar == '[') {
                findThis = ']';
            }
            else if (myChar == '{') {
                findThis = '}';
            }
            myStack.push(myChar);
        }
        else if (myChar == ')' || myChar == ']' || myChar == '}') {
            if (myChar != findThis) {
                return "ERROR";
            }
        }
        else if (myChar == '/') {
            myFile >> nextChar;
            cout << nextChar << endl;
            if (nextChar == '*') {
                myFile >> nextChar;
                myFile >> nextNextChar;
                while (nextChar != '*' && (nextNextChar != '/')) {
                    {
                        if (myFile.eof()) {
                            return "ERROR";
                        }
                        else {
                            nextChar = nextNextChar;
                            myFile >> nextNextChar;
                        }
                    }
                }
            }
            else {
                myChar = nextChar;
                continue;
            }
        }
        else {
            //"ignore other characters"??
            myFile >> myChar;
            cout << myChar << endl;
        }
    }
 if (myStack.isEmpty()) {
        return "Error free";
    }
    else {
        return "ERROR ";
    }

    system("pause");
}

ListStack.h

#pragma once

#ifndef LL_Stack
#define LL_Stack

#include"LList.h"

class LLStack {

private:
    LList list;

public:

    void push(char el) {
        list.push_back(el);
    }

};

#endif

LList.cpp

#include<iostream>
#include"LList.h"

LList::~LList() {
    for (LLNode *p; !isEmpty();) {
        p = head->next;
        delete head;
        head = p;
    }
}

void LList::push_back(char el) { //"insert el at the end of the list"
    if (tail != 0) { //if list not empty
        tail->next = new LLNode(el); //according to debugger this is causing the problem
        tail = tail->next;
    }
    else head = tail = new LLNode(el);
}

LLIST.H

#pragma once

#ifndef LINKED_LIST
#define LINKED_LIST

class LLNode {
public:
    LLNode() {
        next = 0;
    }
    LLNode(char el, LLNode *ptr = 0) {

        info = el; next = ptr;
    }
    char info;
    LLNode *next;// , *prev;
};

class LList { //For access to the list
public:
    LList() {
        head = tail = 0;
    }
    ~LList();
    int isEmpty() {
        return head == 0;
    }

    void push_back(char el); //"insert el at the end of the list"

private:
    LLNode *head, *tail;
};

#endif

example.txt中

 /*
 * words
 * more words and, such
 *  all the words
 *  so many words
 */


int main() { 

    for (int i = 0; i < 500; i++) { //I'm really hoping to type more words here
        if (so many words){
        ) // should be a curly brace
    }

    return 0;
}

研究/思想:
我不知道自己做错了什么,但经过一番研究后,我很确定它与我在{{1}中声明新堆栈的方式有关。 }}

我找到了this question where someone seems to be having the exact same problem,但我很难理解答案以及它们如何适用于我的项目以及是否犯了同样的错误。

问题:
如何解决此错误?

0 个答案:

没有答案