更改链接列表

Changing a linked list

本文关键字:列表 链接      更新时间:2023-10-16

需要帮助打印出链接列表。我有链接列表的工作,只需要能够打印它即可。另外,我希望在打印一旦删除列表。

感谢所有帮助的人。

感谢它。

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