Thread: Linked List Help

  1. #1
    kerrywolfe
    Guest

    Post Linked List Help

    Hey all the Gurus out there...need a little assistance with adding a node and deleting a node...I have the code below...hopefully somebody can make something out of the mess!!!

    THIS TESTS THE ADDING:

    void TestAddNode (NodePtr List)
    {
    char NewChar;

    cout << "\n\n---------------- Testing AddNode -------------------\n\n";

    cout << "Character to be added? ";
    NewChar = getch();

    if (NewChar == '\r') // User pressed just enter key.
    {
    cout << "Aborting AddNode...";
    return;
    }

    cout << NewChar;
    cout << " -- Adding \'" << NewChar << "\'";

    AddNode (NewChar, List);

    cout << "\n\nThe new list: ";
    ShowList(List);
    }

    THIS IS TRYING TO ADD THE NODE:

    void AddNode (char NewChar,
    NodePtr List)
    {
    NodePtr NewNode;

    NewNode = List;

    if (NewChar != '\n')
    {
    NewNode->Link = new Node;

    if (NewNode->Link == NULL)
    AbortProgram();

    else
    {
    NewNode = NewNode->Link;
    NewNode->Ch = NewChar;
    NewNode->Link = NULL;
    }
    }
    }

    THIS IS TO TEST DELETE NODE:

    void TestDeleteNode (NodePtr List)

    {
    int CharFound;
    char CharToBeDeleted;


    cout << "\n\n***************** Testing DeleteNode *******************";

    cout << "\n\nCharacter to be deleted? ";
    CharToBeDeleted = getch();
    cout << CharToBeDeleted;

    DeleteNode (CharToBeDeleted, List, CharFound);

    if ( CharFound )
    cout << "\n\n'" << CharToBeDeleted << "' has been deleted,";
    else
    cout << "\n\n'" << CharToBeDeleted << "' was not in the list,";

    cout << "\n\nList = ";
    ShowList(List);
    }


    THIS IS FOR DELETING NODE:

    void DeleteNode (char CharToDelete,
    NodePtr List,
    int &CharFound)
    {
    NodePtr NodeToBeDeleted;

    if (List->Ch == CharToDelete)
    {
    NodeToBeDeleted = List;
    List = List->Link;
    delete NodeToBeDeleted;
    List->Link = NULL;
    CharFound = 1;
    }
    else
    CharFound = 0;
    }


    ANY HELP WOULD BE MUCH APPRECIATED!!!

    Thanks!

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You haven't really shown enough of your code or stated what error messages you're getting, but here's a few points that may or may not be applicable -

    1) Does List always point to the last node added to your list? If not you'll either have to store the end of your list in a pointer and pass this into AddNode(), or traverse the list everytime you call AddNode(). Otherwise you'll be adding a character in the same position and losing pointers to all the other characters added.

    2) Add node will only work if the list already exists.

    3) When deleteing a character you'll probably want to traverse the list to find it, at present you're only checking one node.

    4) If you are deleting the last node then List = List->Link; will make List=NULL, and then doing List->Link = NULL; will probably crash your program, as you cannot access members of objects that don't exist. You'll want to check to see if you're deleting the head or tail of the list and take the appropriate action.
    zen

  3. #3
    kerrywolfe
    Guest

    Smile REply to Zen

    Okay,

    i understand what you are saying....I got the Add NOde to work now....the LIST DOES EXIST already. Its a lot of code and I was trying to save the time of going through alot of what doesn't matter....

    For the Delete Node....the user is asked to enter a char to delete from the list that they have built already. That is the NewChartoDelete variable.

    I dont understand how to go node to node to find the match and then delete that node....this only has to work for the first occurence of the character even if there are multiple ones present

    As Always, Any Help is Appreciated!

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    dont understand how to go node to node to find the match and then delete that node
    You can loop through the list -


    NodePtr It = List;

    while(It != NULL)
    {
    if(It->Ch== CharToDelete)
    {
    //Delete node
    //Re-link list
    //Set flag to indicate that char has been deleted
    //Break from loop
    }
    It=It->Link;
    }


    The only bit that needs some work is the re-linking.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM