GreenSaucer42
GreenSaucer42

Reputation: 49

Changing a linked list

Need help printing out the linked list. I have the linked list working, just need to be able to print it. Also, I'm looking to delete the list once it is printed.

Thanks to all who helped.

Appreciate it.

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

}

Upvotes: 1

Views: 116

Answers (3)

Yousaf
Yousaf

Reputation: 29282

Function to Print the Link-List

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

Function to delete the entire Link-List.

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 Deleted\n";
    }
        else
        cout<<"\nLink-List is Already Empty\n";
    }

}

Upvotes: 1

Raindrop7
Raindrop7

Reputation: 3911

you can iterate over all nodes until the last node printing each time the value:

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

and in main before calling delete add:

printList(pList);

Delete Entire List

Simply walk the list, deleting each node until the final null. When finished, the head pointer passed by address will contain null.

void deleteList(node **pHead)
{
    while (*pHead)
    {
        node *tmp = *pHead;
        *pHead = tmp->pNext;
        delete tmp;
    }
}

here is the code to delete any node depending on an input value:

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

Upvotes: 2

Mr. Developerdude
Mr. Developerdude

Reputation: 9668

Something like this? PS: Not tested, probably fulll of bugs, but you get the point.

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

Upvotes: 1

Related Questions