Thread: Need help with Singly Linked List C++

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Need help with Singly Linked List C++

    Hi there.
    I have made two classes. Below is Node Class.
    Code:
    class Node
    {
    public:
        int id;
        char name[20];
        Node *Next;
    };
    Below is Link Class.
    Code:
    class Link
    {
    private:
        Node *Head;
        Node *Runner;
        Node *Ptr;
    public:
        Link();
        //~Link();
        int Add(int id, char name[20]);
        void Display();
    };
    Below is my Add Function. If condition will run only when there is no node exist. Head is pointing to NULL at this time. If there is one or more than one NODES exist, then else condition will be executed.
    Code:
    int Link::Add(int id, char name[20])
    {
    
        if (Head == NULL)
        {
            Head = new (Node);
            Head->id = id;
            strcpy(Head->name, name);
            Head->Next = Head;
            return 0;
        }
        else
        {
            Runner = Head;
            do
            {
                Runner = Runner->Next;
                Runner = new (Node);
                Runner->id = id;
                strcpy(Runner->name, name);
                Runner->Next = NULL;
                Head = Runner;
                return -1;
            }
            while(Runner != NULL);
            
        }
    }
    Below is my display Code.
    Code:
    void Link::Display()
    {
        Runner = Head;
    
        if (Runner != NULL)
        {            
            cout << "Id  : " <<Runner->id<<endl;
            cout << "Name: " <<Runner->name<<endl;
            Runner = Runner->Next;
        }
    }
    This code takes the input and prints only the most recent input. All i want to do is to make a singly linked list which can make 5 or 10 nodes and prints the data of each NODE.

    This is my assignment and i am very confused and worried about it. Please help me out.

    Regards

    Assad

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1 Are you restricted to using char arrays for the name field? Can you not use a std::string?

    #2
    Code:
    if (Head == NULL)
    {
        Head = new (Node);
        Head->id = id;
        strcpy(Head->name, name);
        Head->Next = Head;
        return 0;
    }
    This creates a circular list. Are you sure you want to do that? You also do not check that dynamic memory allocation was a success, you simply assume the call to new works.

    #3
    Code:
    do
    {
        Runner = Runner->Next;
        Runner = new (Node);
        Runner->id = id;
        strcpy(Runner->name, name);
        Runner->Next = NULL;
        Head = Runner;
        return -1;
    }
    while(Runner != NULL);
    Why do you have a loop that will only ever execute once due to the return statement. The first line of the loop body has you getting the Next member of the list which, since Runner points to Head and Head->Next points to Head (see comment #2), means that Runner still points to Head. The second line of the loop body throws away what Runner was - so why even have the first line - for a newly allocated Node which you again neglect to check for success. Here is the big one, the line before the return statement destroys the linked list by throwing away what Head used to be for the newly allocated Node. The end result is that this code leaks memory like a sieve and will only ever result in a linked list with a single Node no matter how many times you try and add a new Node.
    Last edited by hk_mp5kpdw; 10-18-2011 at 02:08 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    new throws if it fails anyway, so there is no problem in the first if statement.
    If you want to be picky, you should allocate a new node first, since we don't want to modify the list unless the allocation succeeds. Doing that would leave the list in an inconsistent state if the allocation fails.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Do you care what order the items are put into the list?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked list
    By aextine in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 02:20 PM
  2. singly linked list
    By right2001 in forum C Programming
    Replies: 3
    Last Post: 08-20-2009, 10:21 AM
  3. need help with singly linked-list
    By vearns in forum C++ Programming
    Replies: 20
    Last Post: 04-09-2008, 09:48 AM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. singly linked list
    By Luigi in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2002, 11:19 AM