Linked Lists

This is a discussion on Linked Lists within the C++ Programming forums, part of the General Programming Boards category; Can someone please check and see if im on the right track with this linked list? If i am completely ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Linked Lists

    Can someone please check and see if im on the right track with this linked list? If i am completely wrong, can you please put me on the right track? :-D

    PHP Code:
    #include <iostream>
    using namespace std;

    typedef struct _Node {
        
    char *name;
        
    int age;

        
    struct _Node *next;
    NODE;

    NODE *root;

    class 
    LinkedList
    {
    public:
        
    LinkedList();
        ~
    LinkedList();

        
    bool AddNode(char *tNameint tAge);
        
    bool DelNodeName(char *tName);
        
    bool DelNodeAge(int tAge);
    };

    bool LinkedList::AddNode(char *tNameint tAge)
    {
        
    NODE *temp;

        while(
    temp->next)
        {
            
    temp temp->next;
        }

        
    NODE *NewNode;
        
    NewNode = new NODE;
        
    NewNode temp->next;
        
    temp->next NewNode;
        
    NewNode->name tName;
        
    NewNode->age tAge;

        return 
    true;
    }

    bool LinkedList::DelNodeAge(int tAge)
    {
        
    NODE *temp;

        
    temp root;

        while(
    temp->next->age != tAge)
        {
            
    temp temp->next;
        }
        
    temp->next temp->next->next;

        
    delete temp->next;

        return 
    true;
    }

    int main(int argcchar *argv[])
    {
        return 
    0;


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > NODE *root;
    This should be a class member

    > bool LinkedList::AddNode(char *tName, int tAge)
    {
    NODE *temp;
    temp is uninitialised
    And I see no point in always returning true

    PHP Tags do not make for good source code posting
    >
    bool LinkedList:<img src="images/smilies/biggrin.gif" border="0" alt="">elNodeAge
    (whatever that is)
    You need to be especially carefull when
    - there is only one node in the list
    - when the node being deleted is at the head/tail of the list

    The usual loop for linked lists is
    Code:
        NODE *temp = root;
        while ( temp != NULL ) {
            // do stuff with node
            temp = temp->next;
        }
    Which easily copes with empty lists, lists with one element etc

  3. #3
    Unregistered
    Guest
    I don't understand why you are using a C style struct in the same program as a C++ type class. Use a C++ type struct instead. Or, if you prefer, create a class to implement the node instead, since the only difference between a struct and a class in C++ is what default access the members have.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21