更改链接列表
Changing a linked list
需要帮助打印出链接列表。我有链接列表的工作,只需要能够打印它即可。另外,我希望在打印一旦删除列表。
感谢所有帮助的人。
感谢它。
// nodes.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct node
{
int val;
node *pNext;
int list_length = 6;
};
// prototypes
void insertNode(node **pHead, int valToInsert);
void printList(node *pHead);
void deleteNode(node **pHead, int valToDelete);
void deleteList(node **pHead);
// Print the list
void printList(node *pHead)
{
node* pTmp = pHead;
while (pTmp)
{
cout << pTmp->val << endl;
pTmp = pTmp->pNext;
}
}
// Delete the list
void deleteList(node **pHead)
{
while (*pHead)
{
node *tmp = *pHead;
*pHead = tmp->pNext;
delete tmp;
}
}
int main()
{
node *pList = NULL;
insertNode(&pList, 5);
insertNode(&pList, 15);
insertNode(&pList, 50);
insertNode(&pList, 4);
insertNode(&pList, 42);
insertNode(&pList, 34);
printList(pList);
deleteNode(&pList, 15);
deleteNode(&pList, 50);
deleteNode(&pList, 40);
deleteList(&pList);
cin.get();
return 0;
}
void insertNode(node **pHead, int valToInsert)
{
node *pNew;
node *pCurr;
node *pPrev;
// declare and fill node
pNew = new node;
pNew->val = valToInsert;
pNew->pNext = NULL;
// traverse list to find where node goes
pCurr = *pHead;
pPrev = NULL;
while (pCurr != NULL && pCurr->val < pNew->val)
{
pPrev = pCurr;
pCurr = pCurr->pNext;
}
// insert the node
if (pPrev != NULL) // not at the beginning of the list
{
pPrev->pNext = pNew;
pNew->pNext = pCurr;
}
else
{
*pHead = pNew;
pNew->pNext = pCurr;
}
}
void deleteNode(node **pHead, int valToDelete)
{
node *pDeleteNode;
node *pCurr;
node *pPrev;
bool bFound;
// traverse list to find the node
pCurr = *pHead;
pPrev = NULL;
bFound = false;
// stop looking when we find it, get to the end, or get past it
while (!bFound && pCurr != NULL && pCurr->val <= valToDelete)
{
// if we find the node, set bFound to true
if (pCurr->val == valToDelete)
{
bFound = true;
}
else
{
// move to next node
pPrev = pCurr;
pCurr = pCurr->pNext;
}
}
// if we found the node
if (bFound)
{
// if it's the first node in the list
if (pPrev == NULL)
{
// change head of list
*pHead = pCurr->pNext;
}
else
{
// change pointer of the previous node
pPrev->pNext = pCurr->pNext;
}
//delete the node
delete pCurr;
pCurr = NULL;
} // if (bFound)
}
您可以在所有节点上迭代,直到每次值的最后一个节点打印为:
void printList(node *pHead)
{
while(pHead)
{
cout << pHead->val << endl;
pHead = pHead->pNext;
}
}
,在呼叫删除之前,请
printList(pList);
删除整个列表
只需删除列表,将每个节点删除直至最终null。完成后,通过地址传递的头指针将包含null。
void deleteList(node **pHead)
{
while (*pHead)
{
node *tmp = *pHead;
*pHead = tmp->pNext;
delete tmp;
}
}
这是根据输入值删除任何节点的代码:
void deleteNode(node *pHead, int valToDelete)
{
node* pTmp = pHead;
node* pCurrent = pHead;
node* pNext = NULL;
//1 checking for wether valToDelete to be deleted does exist
for(; pTmp; pTmp = pTmp->pNext)
{
if(pTmp->val == valToDelete)
break;
}
//if valToDelete to be deleted doesn't exist pop up a message and return
if(!pTmp)
{
cout << valToDelete << ": Can't be found!n";
return;
}
int NextValue = 0;
for(;;)
{
//if valToDelete to be deleted is on the head node
if(pHead->val == valToDelete)
{
//store the next to head node
pTmp = pHead->pNext;
//delete head node
delete pHead;
//assign the head new node (node after the original head)
pHead = pTmp;
//exiting gracefully
return;
}
//moving to next node
pNext = pCurrent->pNext;
NextValue = pNext->val;
//checking next node value with valToDelete to be deleted. if they
//match delete the next node
if(NextValue == valToDelete)
{
//next node holds the target valToDelete so we want its next node
//and store it in pTmp;
pTmp = pNext->pNext;
//pCurrent doesn't yet hold the target value to delete but it is the
//previous node to target node
//change the next node of pCurrent so that it doesn't
//point to the target node but instead to the node right
//after it
pCurrent->pNext = pTmp;
//delete the target node (holds the target value to delete)
delete pNext;
//exit
return;
}
//if pNext doesn't point to target node move to the next node
//by making pCurrent points to the next node in the list
pCurrent = pNext;
}
}
类似的东西?PS:未经测试,可能是一个错误,但您明白了。
void printList(node **pHead)
{
std::cout << "PRINTING THE LIST";
int i=0;
while(nullptr != *pHead){
std::cout << "ITEM NUMBER "<<i<<" IS "<<*pHead->val;
*pHead=*pHead->pNext;
i++;
}
}
函数以打印链接列表
void print_list(node *ptr)
{
if (ptr!=NULL)
{
cout<<"nElements in the List are : ";
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr = ptr->next;
}
cout<<endl;
}
else
cout<<"nLink-List is Emptyn";
}
}
函数删除整个链接列表。
void delete_list()
{
if (length != 0) //length is a variable that stores the length of linkedlist
{
while(list_length!=0)
{
remove_tail(); // function that removes tail node from the linkedlist
list_length--;
}
head =NULL;
cout<<"nLink-List Deletedn";
}
else
cout<<"nLink-List is Already Emptyn";
}
}
相关文章:
- 从链接列表c++中删除一个项目
- 读取文件的最后一行并输入到链接列表时出错
- 下面是我为检测链接列表中的循环而制作的代码
- 有人能帮我处理这个链接列表吗?C++
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 链接列表运算符重载没有打印出我想要的内容
- 链接列表在 cpp 中包含不同的对象类
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 如何在构建链接列表时调整头、尾指针
- Shared_ptr双链接列表内存泄漏
- 为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?
- 在链接列表中查找元素 - C++
- 删除链接列表中剩余的最后一个节点
- 只有我的"链接列表"中的第一个节点正在打印
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 如何在C / C++中正确实现链接列表而不会使程序崩溃
- 将元素插入链接列表