c++链表操作

C++ Linked List Operations

本文关键字:操作 链表 c++      更新时间:2023-10-16

我正在尝试为课堂做一个实验,尽管我的教授的讲座和伴随的阅读,我有问题弄清楚语法。如果有人能帮我解决其中一个功能,我觉得我能解决剩下的。

这是标题

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

第一个:

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

如果有人能帮我解决其中一个功能,我觉得我能解决剩下的。

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