2017-11-15 10 views
0

プログラムを実行すると、displayList()関数が呼び出されたときにリスト内の項目に空白がない点を除き、意図したとおりに機能します。私は3つのファイルを持っています。メインのcppファイルはShoppingList.cppです。次に、リンクリストの分類と実装のための2つのファイルがあります。リンクされたリストの表示に問題があります

//ShoppingList.cpp 
//Michael Hery 
//COP 2001 
//11/9/17 
//Shopping List 

#include <iostream> 
#include <string> 
#include "strList.h" 
#include "strlist.cpp" 

using namespace std; 

int main() 
{ 
    //Define a NumberList object 
    StrList list; 
    string item; 
    string delItem; 
    int menuSelection; 

    //Ask the user how many items to 
    //put in the list 
    cout << "How many items would you like to put it the list? >> "; 
    cin >> menuSelection; 
    while (menuSelection < 1) 
    { 
     cout << "Please enter a valid number (greater than 0) >> "; 
     cin >> menuSelection; 
    } 

    for (int i = 0; i < menuSelection; i++) 
    { 
     cout << "Please enter item " << i + 1 << ": "; 
     cin >> item; 
     list.appendNode(item); 
    } 

    list.displayList(); 

    cout << "Which item do you wish to delete from the list? >> "; 
    cin >> delItem; 
    while (delItem == "") 
    { 
     cout << "Please enter a valid item >> "; 
     cin >> delItem; 
    } 

    list.deleteNode(delItem); 

    list.displayList(); 

    //Wait for user input to exit program 
    system("PAUSE"); 

    return 0; 
} 



//strList.h 
#ifndef STRLIST_H 
#define STRLIST_H 

#include <iostream> 
#include <string> 

namespace 
{ 
    class StrList 
    { 
    private: 
     struct ListNode 
     { 
      std::string value; 
      struct ListNode *next; 
     }; 

     ListNode *head; 
    public: 
     //Constructor 
     StrList() 
     { 
      head = nullptr; 
     } 

     //Destructor 
     ~StrList(); 

     //Linked List Operations 
     void appendNode(std::string item); 
     void insertNode(std::string item); 
     void deleteNode(std::string item); 
     void displayList() const; 
    }; 
} 
#endif 



//strList.cpp 
#ifndef STRLIST_CPP 
#define STRLIST_CPP 

#include <iostream> 
#include <string> 
#include "strList.h" 

void StrList::appendNode(std::string item) 
{ 
    ListNode *newNode; 
    ListNode *nodePtr; 

    //Allocate a new node and store item there 
    newNode = new ListNode; 
    newNode->value = item; 
    newNode->next = nullptr; 

    //If there are no nodes in the list 
    //make newNode the first node 
    if (!head) 
     head = newNode; 
    else //Otherwise, insert newNode at the end 
    { 
     //Initialize nodePtr to head of the list 
     nodePtr = head; 

     //Find the last node in the list 
     while (nodePtr->next) 
      nodePtr = nodePtr->next; 

     //Insert newNode as the last node 
     nodePtr->next = newNode; 
    } 
} 

void StrList::insertNode(std::string item) 
{ 
    ListNode *newNode; 
    ListNode *nodePtr; 
    ListNode *previousNode = nullptr; 

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

    //If there are no nodes in the list 
    //make newNode the first node 
    if (!head) 
    { 
     head = newNode; 
     newNode->next = nullptr; 
    } 
    else //Otherwise, insert newNode 
    { 
     //Position nodePtr at the head of list 
     nodePtr = head; 

     //Initialize previousNode to nullPtr 
     previousNode = nullptr; 

     //Skip all nodes whose values is less than num 
     while (nodePtr != nullptr && nodePtr->value < item) 
     { 
      previousNode = nodePtr; 
      nodePtr = nodePtr->next; 
     } 

     //If the new node is to be the 1st in the list, 
     //insert it before all the other nodes 
     if (previousNode == nullptr) 
     { 
      head = newNode; 
      newNode->next = nodePtr; 
     } 
     else //Otherwise insert after the previous node 
     { 
      previousNode->next = newNode; 
      newNode->next = nodePtr; 
     } 
    } 
} 

void StrList::deleteNode(std::string item) 
{ 
    ListNode *nodePtr;  //To traverse the list 
    ListNode *previousNode = nullptr; //To point to the previous node 

          //If the list is empty, do nothing 
    if (!head) 
     return; 

    //Determine if the first node is the one 
    if (head->value == item) 
    { 
     nodePtr = head->next; 
     delete head; 
     head = nodePtr; 
    } 
    else 
    { 
     //Initialize nodePtr to head of list 
     nodePtr = head; 

     //Skip all nodes whose value member is 
     //not equal to num 
     while (nodePtr != nullptr && nodePtr->value != item) 
     { 
      previousNode = nodePtr; 
      nodePtr = nodePtr->next; 
     } 

     //If nodePtr is not at the end of the list, 
     //link the previous node to the node after 
     //nodePtr, then delete nodePtr 
     if (nodePtr) 
     { 
      previousNode->next = nodePtr->next; 
      delete nodePtr; 
     } 
    } 
} 

StrList::~StrList() 
{ 
    ListNode *nodePtr; //To traverse the list 
    ListNode *nextNode; //To point to the next node 

        //Position nodePtr at the head of the list 
    nodePtr = head; 

    //While nodePtr is not at the end of the list 
    while (nodePtr != nullptr) 
    { 
     //Save a pointer to the next node 
     nextNode = nodePtr->next; 

     //Delete the current node 
     delete nodePtr; 

     //Position nodePtr at the next node 
     nodePtr = nextNode; 
    } 
} 

void StrList::displayList() const 
{ 
    ListNode *display; 
    display = head; 

    std::cout << "**********************************" << std::endl; 
    std::cout << "**** Your Shopping List ****" << std::endl; 
    std::cout << "**********************************" << std::endl; 

    while (display) 
    { 
     std::cout << display->value; 
     display = display->next; 
    } 
} 

#endif 

問題はdisplayList()にあります。ファイルの最後に向かって機能します。

+1

さて、あなたはそれらの間に空白を出力していないので... – molbdnilo

+0

はあなただけでそれらを分離する単一のスペースたいと仮定 - 'のstd :: coutの<< display->値を<<"「; '。ループした後で改行を印刷することも考えられます。 – Steve

+0

@molbdnilo ...だから、プログラムを書いた人ではないようです:) –

答えて

1

値の間に区切り文字を追加します。

char const* sep = ""; 
while (display) 
{ 
    std::cout << sep << display->value; 
    sep = " "; 
    display = display->next; 
} 
+0

Splendid!あなたの区切り文字を使用するconst char * sep = "";彼は同じ出力を得るでしょう:) –

+0

@VladfromMoscowいいえ、彼は最初の項目が表示された後に 'sep'を変更するためです。そのようにすれば、あなたは素朴なバージョンのように最後に迷いの空間を得ないことが保証されます。 – Steve

+0

@スティーブそれは難読化されたコードの例ですか? –

関連する問題