更改链接列表

时间:2016-12-11 21:18:29

标签: c++

需要帮助打印出链表。 我有链表工作,只需要能够打印它。此外,我打算在打印后删除列表。

感谢所有帮助过的人。

欣赏它。

 // 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)

}

3 个答案:

答案 0 :(得分:2)

您可以迭代所有节点,直到每次打印最后一个节点:

void printList(node *pHead)
{
    while(pHead)
    {
        cout << pHead->val << endl;
        pHead = pHead->pNext;
    }
}

并且在调用delete之前在main中添加:

printList(pList);

删除整个列表

只需遍历列表,删除每个节点,直到最终为空。完成后,通过地址传递的头指针将包含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;
    }
}

答案 1 :(得分:1)

这样的东西? 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++;
    }
}

答案 2 :(得分:1)

打印链接列表的功能

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 Empty\n";
        }

删除整个链接列表的功能。

void delete_list()
    {
        if(list_length!=0)   //list_length is a variable that stores the length of linklist
        {
            while(list_length!=0) 
            {
                remove_tail();   //you will need a function that removes tail node from the linklist
                list_length--;
            }
           head =NULL;
           cout<<"\nLink-List Deleted\n";
        }
        else
        cout<<"\nLink-List is Already Empty\n";
    }