我在显示链接列表时遇到问题

时间:2017-11-15 20:01:17

标签: c++ list

当我运行程序时,它按预期工作,但是当调用displayList()函数时,列表中的项目之间没有空格。我有3个文件。主cpp文件是ShoppingList.cpp。然后我有两个其他文件用于链表分类和实现。

//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 个答案:

答案 0 :(得分:1)

在您的值之间添加分隔符。

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