【问题标题】:C++ Linked List OperationsC++ 链表操作
【发布时间】:2014-03-10 19:23:35
【问题描述】:

我正在尝试为课堂做一个实验室,尽管有教授的讲座和随附的阅读材料,但我在搞清楚语法时遇到了问题。如果有人可以帮助其中一项功能,我觉得我可以弄清楚其余的。

这是标题

#pragma once

#include <string>

using namespace std;

struct ListNode
{
public:
    ListNode( const string& theString, ListNode* pNext = NULL )
    {
        word = theString;
        pNextNode = pNext;
    }

    string word;
    ListNode* pNextNode;
};

//add a node (pNode) at the head of the list.  
//return a pointer to the head node
ListNode* addFront( ListNode* pHead, ListNode* pNode );

//add a node (pNode) at the tail of the list.  
//return a pointer to the head node
ListNode* addEnd( ListNode* pHead, ListNode* pNode );

//remove (and cleanup after) the node at the head of the LinkedList (pHead)
//return a pointer to the head node
ListNode* removeFront( ListNode* pHead );

//remove (and cleanup after) the node at the tail of the LinkedList (pHead)
//return a pointer to the head node
ListNode* removeEnd( ListNode* pHead );

//traverse the LinkedList (pHead) and delete all nodes
//return a pointer to the head node
ListNode* removeAllNodes( ListNode* pHead );

//traverse the LinkedList (pHead) and write out all the words in the list
//separated by a space
void printNodeReport( ListNode* pHead, ostream& out );

这是我需要在其中实现存根的 cpp

#include "LinkedList.h"
#include <string>
#include <sstream>

//add a node (pNode) at the head of the list.  
//return a pointer to the head node
ListNode* addFront( ListNode* pHead, ListNode* pNode )
{
    //stub

    return pHead;
}

//add a node (pNode) at the tail of the list.  
//return a pointer to the head node
ListNode* addEnd( ListNode* pHead, ListNode* pNode )
{
    //stub
    return pHead;
}


//remove (and cleanup after) the node at the head of the LinkedList (pHead)
//return a pointer to the head node
ListNode* removeFront( ListNode* pHead )
{
    //stub
    return pHead;
}

//remove (and cleanup after) the node at the tail of the LinkedList (pHead)
//return a pointer to the head node
ListNode* removeEnd( ListNode* pHead )
{
    //stub
    return pHead;
}



//traverse the LinkedList (pHead) and delete all nodes
//return a pointer to the head node
ListNode* removeAllNodes( ListNode* pHead )
{
    //stub
    return pHead;
}


//traverse the LinkedList (pHead) and write out all the words in the list
//separated by a space
void printNodeReport( ListNode* pHead, ostream& out )
{
    out << "Here's the list: ";
    ListNode* pCurr = pHead;
    while( pCurr != NULL )
    {
        out << pCurr->word << " ";
        pCurr = pCurr->pNextNode;
    }
    out << "EOL \n";
}

【问题讨论】:

  • 哪个函数?什么语法?具体...
  • 哇,按照这个速度,他们可能会在 20 年内向学生教授当前的 C++ 标准。
  • 查看stackoverflow.com/questions/397895/…,Johannes Schaub 的答案

标签: c++ linked-list


【解决方案1】:

第一个:

ListNode* addFront( ListNode* pHead, ListNode* pNode )
{
    pNode->pNextNode = pHead;
    return pNode;
}

一个也可以更具防御性:

ListNode* addFront( ListNode* pHead, ListNode* pNode )
{
    if(pNode == NULL)
    {
        //Exception handling.
    }
    pNode->pNextNode = pHead;
    return pNode;
}

【讨论】:

  • Hmmmmmmm ... :/ 这不是我所期望的名为 addFront() 的函数的行为
  • 怎么样?它将前面的元素附加到要添加的元素上,然后将前面的元素设置为新的前面元素,对吧?
  • 你使用的签名不对,pHead应该是参考。
  • 我不这么认为?我从问题描述中复制了签名。
  • 可能是,但显然不是错误的。虽然我不会那样写addFront(),但我认为这不是一个有效的实现。
【解决方案2】:

如果有人可以帮助其中一项功能,我觉得我可以解决其余的问题。

您应该将列表头作为参考传递(并确保在您的客户端代码中将其初始化为 nullptr):

ListNode* addFront( ListNode*& pHead, ListNode* pNode )
{
    if(!pHead) {
        pHead = pNode;
    }
    else if(pNode) {
        pNode->pNextNode = pHead;
        pHead = pNode;
    }
    return pHead;
}

或者使用这个函数签名(如果这有助于您更好地理解参考):

ListNode* addFront( ListNode** pHead, ListNode* pNode )
{
    assert(pHead);
    if(!(*pHead)) {
        *pHead = pNode;
    }
    else if(pNode) {
        pNode->pNextNode = *pHead;
        *pHead = pNode;
    }
    return *pHead;
}

使用第一个样本:

ListNode* theList = nullptr;
addFront(theList,new ListNode("World!"));
addFront(theList,new ListNode("Hello "));

使用第二个样本:

ListNode* theList = nullptr;
addFront(&theList,new ListNode("World!"));
addFront(&theList,new ListNode("Hello "));

以上语句后面的代码

for(ListNode* curNode = theList; curNode; curNode = curNode->pNextNode) {
    std::cout << curNode->word;
}

输出

Hello World!

【讨论】:

  • 虽然我完全同意您的实现更好,但它实际上并没有回答提出的问题。问题是填写提供的功能,而不是更改签名。我相信提供签名的目的是通过发出list = addFront(list, nodeToAdd)来调用。
  • @riklund 我什至考虑过创建函数void。但是由于您提出的声明仍然适用于我的提议,因此我谴责了这一点,以减少混乱......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
相关资源
最近更新 更多