2017-12-06 6 views
-1

このリンクリストのスタックを理解するのが世界で最も困難です。私は編集が許可されていない固定の ".h"ファイルを与えられています。プッシュとポップの機能を書いています。しかし、私のポップアップ機能はプログラムをクラッシュさせているのですが、私はその理由を不明です。私のポップ機能は次の通りです:C++リンクリストスタックポップ関数クラッシュ

int IntStack::pop(){ 
    int result = -1; 
    if(isEmpty()){ 
     result = head->data; //program crashes here 
     Node *temp; 
     temp = head->next; 
     delete head; 
     head = temp; 
    } 
    return result; 
} 

私のノードは以下の通りです。なぜこれがクラッシュするとどのように私はそれを修正することができますするよう

struct Node{ 
    int data; 
    Node* next; 
}; 

任意の洞察力 いただければ幸いです。また、関数にパラメータを持たせることはできません。ありがとうございました

編集:私は機能に変更を提案し、ここに私のコードの残りの部分です。

#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(); 

} 

私は私が私のポップ機能の頭の上に無効なポインタを持っているかもしれないと思うが、私はわかりませんよ。それとも私のプッシュ機能ですか?

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

デバッガでコードを実行してみましたか? –

+3

'assert(isEmpty());'は、要素を削除しようとすると良い前提条件のようには見えません。 –

答えて

0

あなたはそのコードで正確に何をしようとしていますか?ちょうど最後に除去するtempの全体rigamaroleの代わりにint result = head->data;をちょうどやっていたかもしれません。また、あなたは私があなたがしたいと思うような方法で頭を抜くことは決してないと思います。

私はこれを推測しているが、あなたがについて尋ねたものです。

int IntStack::pop(){ 
    int result = -1;    //Assume there is no element to pop 
    if(!this->isEmpty()){  //if the stack is not empty, 
     result = head->data; //save the head's data in result 
     Node *temp;    //make a temp node 
     temp = head->next;  //point it to the node after the head 
     delete head;   //remove the head 
     head = temp;   //make the one after it the new head 
    } 
    return result;    //return the result 
} 

EDIT:

あなたは、ヘッドノードと、あなたのコード内のテール・ノードを逃しています。あなたがテール・ノードを持っている場合は は、プッシュは次のようになります。私はあなたを見つけると思う

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

void IntStack::push(char data){ 
    if(!this->isFull()){ 
     Node *temp = new Node; 
     temp->data = data; 
     tail->next = temp; 
     temp->next = NULL; 
    } 
} 

ArePairあなたはこのように規制するべき最後の緩いリターンを持っていますこれはコードの残りの部分を自分で知っているので便利です。ここにはいくつかの基本的な構造があります。 https://www.codementor.io/codementorteam/a-comprehensive-guide-to-implementation-of-singly-linked-list-using-c_plus_plus-ondlm5azr

+0

私はあなたが提供したコードを実装しようとしましたが、まだ関数がプログラムをクラッシュさせています。また、ファイルを読み込むプログラムを作成しようとすると、括弧がスタックに格納され、一致する閉じ括弧が読み取られるたびにポップされます。 – user8891853

+0

もっとコードを投稿する必要があります。 –

+0

さらにコードを編集しました – user8891853

関連する問題