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

标签 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();在文件末尾运行。

最佳答案

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

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

关于c++ - 我在显示链接列表时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47316262/

相关文章:

c++ - 如何从 C++ 中的字符串中去除所有非字母数字字符?

Python 有序列表搜索与对象列表搜索集

c# - 在 KeyValuePair 键上相交列表?

list - 删除项目后刷新列表 - Flutter

c++ - SetClipboardData() 期间的堆损坏

c++ - 为什么基类中的 copy-and-swap 会导致在派生类中隐式删除复制分配运算符?

c++ - 超出 DirectX11 视频内存

c++ - 如何在 qt 网络中使用 https?

Python:使用列表中的运算符进行数学运算

vb.net - 如何从文本文件中返回满足条件的字符串?