堆栈弹出功能中的头指针崩溃程序

时间:2017-12-06 04:44:36

标签: c++ pointers linked-list crash stack

我一直试图在各处找到这个错误,我不知道我搞砸了哪里。我正在编写一个读取文本的程序,将括号推入链接列表堆栈,并在读入匹配的括号时弹出它们。但是,在pop函数中,它会崩溃。

问题在于我的pop()或我的push()函数。

OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to gateway [172.16.24.1] port 22.
debug1: Connection established.
debug1: identity file /home/ops/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ops/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ops/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ops/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ops/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ops/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ops/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ops/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.7
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.7 pat OpenSSH_6.6.1* compat 0x04000000
debug1: Authenticating to gateway:22 as 'ops'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:TgaQlbi1L+JBQBbqhJZHIpDnXtJGZtw5y3r2fj7qFAM
debug1: Host 'gateway' is known and matches the ECDSA host key.
debug1: Found key in /home/ops/.ssh/known_hosts:2
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/ops/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 535
Enter passphrase for key '/home/ops/.ssh/id_rsa':

以下是驱动程序。

void IntStack::push(int data){
    assert(!isFull());
        Node *temp = new Node;
        temp->data = data;
        temp->next = NULL;
}

int IntStack::pop(){
    int result = -1;
    if(isEmpty()){
        result = head->data; //Crashes on this line
        Node *temp;
        temp = head->next;
        delete head;
        head = temp;
    }

    return result;
}

任何帮助将不胜感激。我认为它可能是一个无效的指针..虽然我太缺乏经验,无法告诉。我知道这不是利用堆栈的最有效方式,但它适用于需要使用单链表的类。代码的结尾是不完整的,因为我从未通过我的pop函数。

编辑:其余代码如下。

 #include <iostream>
 #include <fstream>
 #include <string>
 #include "IntStack.h"

 using namespace std;

 bool ArePair(int first,char last){
     if(first == '(' && last == ')') return true;
     else if(first == '{' && last == '}') return true;
     else if(first == '[' && last==']') return true;
     return false;
 }

 int main(){
     string fileName;

     cout << "Hello, please enter a filename: ";
     cin >> fileName;

     ifstream inFile;

     inFile.open(fileName.c_str());

     while (!inFile){
         cerr << "ERROR: Cannot open " << fileName << ". Please re-enter.\n";
         cin >> fileName;
         inFile.open(fileName.c_str());
     }

     string current;
     IntStack iStack;
     int par; //parenthesis

     while(inFile){
        inFile >> current;

        for(int i=0;i<current.length();i++){
             if(current[i] == '('||current[i] == '{'||current[i] == '['){
                 par = current[i];
                 iStack.push(par);
                }
             else if(current[i] == ')'||current[i] == '}'||current[i] == ']'){
                 if(!iStack.isEmpty() || !ArePair(par,current[i])){
                     cout << "debug\n";
                 }
                 else{
                    iStack.pop();
                 }
             }
         }
     }



     inFile.close();

 }

0 个答案:

没有答案