c++链表操作
C++ Linked List Operations
我正在尝试为课堂做一个实验,尽管我的教授的讲座和伴随的阅读,我有问题弄清楚语法。如果有人能帮我解决其中一个功能,我觉得我能解决剩下的。
这是标题
#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!
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 使用 XOR 操作仅使用 2 个指针反转链表
- 在 "CodePad" 中执行链表操作时转储的核心(这是一个在线C++编译器)
- 链表回推操作中需要'back pointer'
- 操作链表的函数出现逻辑错误
- 链表操作C++
- 为什么单向链表的 Next() 操作应该用关键部分来保护
- 无锁双链表的原子操作
- 链表操作,检索数据 c++ 的问题
- 尝试仅通过操作指针对链表进行排序
- 使用链表进行堆栈弹出操作
- 使用c++在链表中搜索操作
- 链表操作的段错误
- 仅通过操作指针交换链表中的相邻节点
- c++链表操作