簡體   English   中英

我在顯示鏈接列表時遇到問題

[英]I'm having trouble displaying a linked 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM