2011-07-27 7 views
0

リンクされたリストを作成し、それを動的スタックに変換して、それを動的キューに変換する必要がありました。まあ、すべての "デキュー"を除いて動作するように、終了する予定のプログラムとして、それは私にエラーが表示されます: "未処理のwin32例外がLinkedList_Stack_BNS11.exe [4972]で発生しました。LinkedList/Stack/Queue - デキューに関するヘルプ

デキューすると仮定しているのは、私がステップスルーしたりプログラムを実行したりすると、その部分までスムーズに実行されるため、ポインタが間違っているか何かが送信される可能性があるからです。

出力:5つのアイテムをEnquing

.... // Finsihes

キューの値が(デキュー)であった:

//正しい数字ですが...

//ここでエラーが発生します。完了して閉じるべき時。

私はあまりにも多くのコードが含まれている場合、私が知っていると私は(下のすべてのものの非常に真ん中にある)だけで「デキュー」に助けを事前に

おかげでそれを切り倒しますよ! !私は何が間違っているのか見ていないだけです。おそらく頭がどこを指しているかとは何か関係がありますか? Idk。

ヘッダーファイル:

class NumberList 
{ 
private: 
    // 
    struct ListNode 
    { 
     int value; // Value in this node 
     struct ListNode *next; // Pointer to the next node 
    }; 

    ListNode *head; // List head pointer 
    ListNode *rear; 

public: 
    //Constructor 
    NumberList() 
    { head = NULL; rear = NULL; } 

    //Destructor 
    ~NumberList(); 

    //Stack operations 
    bool isEmpty(); 

    //Queue operations 
    void enqueue(int); 
    void dequeue(int &); 
}; 
#endif 

List_Stack_Queue.cpp:

bool NumberList::isEmpty() 
{ 
    bool status; 

    if(!head) 
     status = true; 
    else 
     status = false; 

    return status; 
} 

    void NumberList::enqueue(int num) 
    { 
     ListNode *newNode; // Point to a new node 

     // Allocate a new node and store num there. 
     newNode = new ListNode; 
     newNode->value = num; 

     //If there are no nodes in the list 
     // make newNode the first node. 
     if(isEmpty()) 
     { 
      head = newNode; 
      rear = head; 
      //newNode->next = NULL; 
     } 
     else 
     { 
      rear->next = newNode; 
      rear = rear->next; 
      //newNode->next = head; 
      //head = newNode; 
     } 
    } 

    void NumberList::dequeue(int &num) 
    { 
     ListNode *temp; 

     if(isEmpty()) 
      cout << "The queue is empty.\n"; 
     else 
     { 
      num = head->value; 
      temp = head; 
      head = head->next; 
      delete temp; 
     } 
    } 

MAIN:

const int MAX_VALUES = 3; 

// Create a DynIntQueue object. 
NumberList iQueue; 

// Enqueue a series of numbers. 
cout << "Enqueuing " << MAX_VALUES << " items...\n"; 
for (int x = 0; x < MAX_VALUES; x++) 
    iQueue.enqueue(x); 

cout << endl; 

//Dequeue and retrieve all numbers in the queue 
cout << "The values in the queue were (Dequeuing):\n"; 
while(!iQueue.isEmpty()) 
{ 
    int value; 
    iQueue.dequeue(value); 
    cout << value << endl; 
} 
return 0; 

答えて

2

最終節の次の要素は、リンクリストのNULLに設定する必要があります。だから、

void NumberList::enqueue(int num) 
{ 
    // ... 
    if(isEmpty()) 
    { 
     head = newNode; 
     head->next = NULL; 
     rear = head; 
    } 
    else 
    { 
     rear->next = newNode; 
     rear = rear->next; 
     rear->next = NULL;  // Pointing the next node element to null. 
    } 
} 

に、私はいくつかのことがNumberlist::isEmpty();メンバ関数と間違っているようです。リストが空であるかどうかをどのように決定していますか?その定義を表示します。

+0

ありがとうございます!あなたのコードはそれをしました、今はエラーなしで動作します! :) cppファイルにNumberList :: isEmpty()を投稿しました(最上部)。 もう一度私の割り当てのカップルポイントを保存しました:)。 質問:私は今、それを一時的に変更しようとしています。新しい質問を投稿するのではなく、この質問を編集するだけですか? – Riotson

関連する問題