2017-03-28 3 views
-2

私は学校のための実験室で作業しています。私は二重リンクリストの機能に固執しています。 removeFront()関数を使用してリストからフロントノードを削除しようとすると、NULLが返され、すでにリストが作成されていてもリストが作成されます。私は私のcppファイルと下のメインを投稿し、うまくいけば、誰かが私が間違っていることを理解するのを助けることができます。二重リンクリストのC++ RemoveFront関数

//DLinkedList.h 

#pragma once 

#include <string> 
using namespace std; 

typedef string Elem; 

struct DNode 
{ 
    Elem value; 
    DNode* next; 
    DNode* prev; 
}; 

class DLinkedList 
{ 
public: 
    DLinkedList() { header_ = NULL; } 
    ~DLinkedList() { }; 
    bool empty() const; 
    const Elem& front() const; 
    const Elem& back() const; 
    void addFront(const Elem& e); 
    void addBack(const Elem& e); 
    void removeFront(); 
    void removeBack(); 

private: 
    DNode* header_; 
    DNode* trailer_; 

protected: 
    void add(DNode* v, const DNode& e); 
    void remove(DNode* v); 
}; 


//DLinkedList.cpp 

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

using namespace std; 



const Elem& DLinkedList::front() const 
{ 
    if (header_ == NULL) 
    { 
     cout << "Please create a doubly linked list first.\n\n"; 
    } 
    else 
    { 
     return header_->value; 
    } 

} 

void DLinkedList::addFront(const Elem& e) //DONE 
{ 
    // If there is no header 
    // create a temporary node and set 
    // the header to it 
    if (header_ == NULL) 
    { 
     DNode *temp; 
     temp = new(struct DNode); 
     temp->prev = NULL; 
     temp->value = e; 
     temp->next = NULL; 

     header_ = temp; 
     trailer_ = temp; 
    } 
    else 
    { 
     //Create current node to point to header 
     // and temp node to be the new front node 
     DNode *current; 
     DNode *temp; 
     current = header_; 
     temp = new(struct DNode); 
     temp->prev = NULL; 
     temp->value = e; 
     temp->next = current->next; 

     header_->prev = temp->next; 
     header_ = temp; 
    } 


    cout << "Element Inserted at the front." << endl; 

} 

void DLinkedList::removeFront() 
{ 
    // Check to see if there is anything 
    // in the list first 
    if (header_ == NULL) 
    { 
     cout << "Create a doubly linked list first."; 
    } 

    // Check to see if the list has more than one item. 
    // If it only has one node erase it. 
    DNode *current; 
    current = header_; 

    if (current->next == NULL) 
    { 
     header_->next = NULL; 
     header_->value = ""; 
     header_->prev = NULL; 
     header_ = NULL; 
    } 
    else 
    { 
     current = current->next; 
     //header_->next = NULL; 
     //header_->value = ""; 
     //header_->prev = NULL; 
     header_ = current; 
     header_->prev = NULL; 
    } 
} 

//Main.cpp 

#include <iostream> 
#include <string> 
#include <cassert> 
#include "DLinkedList.h" 


using namespace std; 

int main() 
{ 
    DLinkedList album; 
    album.addFront("Word"); 
    album.addFront("Turn"); 
    album.addFront("Bird"); 
    album.addFront("Weird"); 

    cout << album.front() << endl; 

    album.removeFront(); 

    cout << album.front() << endl; 


    system("pause"); 

    return 0; 
} 
+1

あなたは*少なくともある[MCVE]にこれを削減する必要がある*あなたの実証コードの量問題/質問;すなわち、**コードダンプではない**。 [小さなプログラムをデバッグする方法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)も参照してください。 –

+0

それは良いですか? –

答えて

-1

これはどのように行うのですか。

私はそのようにそれを行うだろう、あなたのaddFrontも間違っていると思う
void DLinkedList::removeFront() 
    { 
     // Check to see if there is anything 
     // in the list first 
     if (header_ == NULL) 
     { 
      cout << "Create a doubly linked list first."; 
     } 

     // Check to see if the list has more than one item. 
     // If it only has one node erase it. 
     if (header_->next == NULL) 
     { 
      delete header_; 
     } 
     else 
     { 
      DNode* next = header_->next; 
      delete header_; 
      header_ = next; 
      header_->prev = NULL; 
     } 
    } 
+0

removeFrontを使用した後にフロントノードを出力しようとすると、プログラムでブレークポイントが発生しました。ヘッダーは<エラーの文字列を読み取る>の値を返し、prevとnextは???に設定されました。 –

+0

DLinkeList.hを教えてください。 –

+0

もちろん、投稿を編集してそれを含むようにします。 –

-1

void DLinkedList::addFront(const Elem& content) //DONE 
{ 
    // If there is no header 
    // create a temporary node and set 
    // the header to it 
    if (header_ == NULL) 
    { 
     header_ = new(struct DNode); 
     header_->prev = NULL; 
     header_->value = content; 
     header_->next = NULL; 

     trailer_ = header_; 
    } 
    else 
    { 
     //Create current node to point to header 
     // and temp node to be the new front node 
     DNode *tmp; 
     tmp = new(struct DNode); 
     tmp->prev = NULL; 
     tmp->value = content; 
     tmp->next = header_; 

     header_->prev = tmp; 
     header_ = tmp; 
    } 


    cout << "Element Inserted at the front." << endl; 

} 
関連する問題