C ++链接列表操作

时间:2014-03-10 19:23:36

标签: c++ linked-list

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

这是标题

#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";
}

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

  

如果有人可以帮忙完成其中一项功能,我觉得我可以理解其余部分。

您应该将列表头作为参考传递(并确保在客户端代码中将其初始化为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!