2016-10-29 14 views
0

コードを挿入するとノードが正しく挿入されますが、 リストを印刷しようとすると、残念ながら作業が中止されるという問題があります。 エラーメッセージ:プロジェクトが機能しなくなりました。 これは私のコードですリンクの追加、削除、印刷imlementation

#include <iostream> 
#include <string> 
using namespace std; 
typedef struct st { 
    string data; 
    int ISBN; 
    string Title; 
    string Author; 
    int publishedyear; 
    bool borrow; 
    st* next; 
} NODE; 

NODE* add(NODE* head, int isbn) 
{ 
    NODE *p1, *p2; 
    NODE* n; 
    n = new NODE; 
    n->ISBN = isbn; 
    if (head == NULL) { 
     head = n; 
     return head; 
    } 
    if (n->ISBN < head->ISBN) { 
     n->next = head; 
     head = n; 
     return head; 
    } 
    p1 = p2 = head; 
    while (p2 != NULL) { 
     if (n->ISBN < p2->ISBN) { 
      n->next = p2; 
      p1->next = n; 
      return head; 
     } 
     else { 
      p1 = p2; 
      p2 = p2->next; 
     } 
    } 
    n->next = p2; 
    p1->next = n; 
    return head; 
} 

void print(NODE* head) 
{ 
    NODE* p; 
    p = head; 
    if (head == NULL) { 
     cout << "empty list" << endl; 
    } 

    while (p != NULL) { 
     cout << "Book ISBN Is : " << p->ISBN << endl; 
     p = p->next; 
    } 
} 
void main() 
{ 

    // cout << "hi"; 
    NODE* head; 
    head = NULL; 
    string op; 
    int isbn; 
    cout << "Enter the opertion in the following format : op , ISBN" << endl; 
    while (1) { 
     cin >> op; 
     if (op == "add") { 
      cin >> isbn; 
      if (op == "add") { 
       head = add(head, isbn); 
       cout << "book with thie ISBN code " << isbn << " is added successfuly." 
        << endl; 
      } 
     } 
     else if (op == "print") { 
      print(head); 
     } 
     else { 
      cout << "Enter vaild operation! ." << endl; 
     } 
    } 
} 

何か提案がありますか?

+0

オラフは軽度で動きます。 – user4581301

+3

これはC++ではなく、Cではなく、C++での悪い習慣であり、特にこのアプリケーションのためのC-コーディング**スタイル**を使用しています。 – Olaf

+0

だから。どのように問題を解決する? –

答えて

3

答えが指摘されましたが... ...あなたのコードの状態には驚いていますので、いくつかのヒントを教えてください。

注:リストを作成することを目的としていない限り、独自のビルドではなく、既存の標準コンテナ(特にvector)とアルゴリズム(sort)を再利用してください。


のは、基本から始めましょうが、これはあなたが今ではC++ 11へのアクセス権を持っている必要がある2016年です。

C++ 11では、宣言のポイントでデータメンバを初期化することができます。組み込み型(積分、ブーリアン、浮動小数点、ポインタ)の場合は、デフォルトで初期化することをお勧めします。困惑しているゴミ。

struct Node { 
    std::string data; 
    int ISBN = 0; 
    std::string title; 
    std::string author; 
    int publishedyear = 0; 
    bool borrow = false; 
    Node* next = nullptr; 
}; 

これだけであなたのバグが解決されることに注意してください。また、次回にはそれを忘れるのを避けます。


第2に、addメソッドは、ノードの作成に責任を持ちません。これは混乱の懸念であり、ノードの大部分はデフォルト値を持ち、ISBNでそれを探すことなくノードにアクセスすることはできません。

addメソッドでは考慮していない点もあります。ISBNが既にリストにある場合はどうなりますか?

// Adds the new node to the list, maintaining the ordering by ISBN. 
// 
// Returns the new head of the list, unless an existing node in the list already 
// has this ISBN in which case returns `nullptr`. 
Node* add(Node* head, Node* node) { 
    assert(node != nullptr && "Null argument provided"); 

    if (head == nullptr) { 
     return node; 
    } 

    if (node->ISBN < head->ISBN) { 
     node->next = head; 
     return node; 
    } 

    if (node->ISBN == head->ISBN) { 
     return nullptr; 
    } 

    // Find "current" such that "current->ISBN" < "node->ISBN" and 
    //       "node->ISBN" <= "current->next->ISBN" 
    Node* current = head; 
    while (current->next != nullptr && node->ISBN > current->next->ISBN) { 
     current = current->next; 
    } 

    if (node->ISBN == current->next->ISBN) { 
     return nullptr; 
    } 

    node->next = current->next; 
    current->next = node; 

    return head; 
} 

注:assert#include <cassert>が必要です。


あなたの印刷方法はすでにかなり良いです、おめでとう!

わずか2 nitpicks:

  • あなたは何をさらに実行されないことを知っていれば、すぐに返すが、
  • endlを使用していないが待っていない、それが行の最後に追加し、フラッシュの両方ない
// Prints the list, in order. 
void print(Node* head) { 
    if (head == nullptr) { 
     std::cout << "empty list\n"; 
     return; 
    } 

    for (Node* p = head; p != nullptr; p = p->next) { 
     std::cout << "Book ISBN: " << p->ISBN << "\n"; 
    } 
} 

よりも頻繁にパフォーマンスの問題につながり、直ちにバッファ、

最後に、修飾されたmain

ヘルプテキストを少し拡張し、(クリーンな)quitオペレーションを提供しました。

しかし、主な変更点は入力エラーのない取引です。出力エラーを処理することは、読者の練習として残されています(ヒント:スローさせる)。

割り当てられたメモリを適切に処理することも適切な練習になります。

int main() { 
    std::cout << "Enter one of the following operations when prompted:\n" 
       " - add <isbn>\n" 
       " - print\n" 
       " - quit\n"; 

    Node* head = nullptr; 

    while (1) { 
     std::cout << "> "; 

     std::string op; 
     if (!(std::cin >> op)) { 
      std::cerr << "An error occurred reading the operation, sorry\n"; 
      break; 
     } 

     if (op == "quit") { 
      std::cout << "See you later!\n"; 
      break; 
     } 

     if (op == "print") { 
      print(head); 
      continue; 
     } 

     if (op == "add") { 
      int isbn = 0; 
      if (!(std::cin >> isbn)) { 
       std::cout << "Please provide a correct ISBN!\n"; 
       continue; 
      } 

      Node* node = new Node(); 
      node->ISBN = isbn; 

      Node* h = add(head, node); 
      if (h == nullptr) { 
       std::cout << "This ISBN was already provided!\n"; 
       delete node; 
       continue; 
      } 

      head = h; 
      continue; 
     } 

     std::cout << "Please enter a valid operation!\n"; 
    } 

    // Deal with allocated memory ;) 
} 
1

st::nextは、NULLに決して設定されません。これにより、のprintに若干問題があります。

ソリューション:NULL nextノードがテールノードの場合。

+0

ありがとうたくさんの男。 –

関連する問題