Thread: Link list

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    22

    Link list

    Hello everyone
    I am working on a link list in c .My goal is to add elements into a single link list to the end. If the element already exist in the link list, it prints out "element present" otherwise adds the element.

    I am new to programming, so I am just working on the logic, however I have not been able to implement it.

    Logic:

    create a ptr node.
    check if( start==NULL)
    if (yes)
    node->info=data

    else if(start!=NULL)
    check ptr->next==NULL and ptr->info!=data
    if (yes)
    add the new node here
    else
    ptr=ptr->next


    I am so confused.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You're always adding to the end, yes? Then it's a simple traversal that breaks if the new value is matched:
    Code:
    if (head == NULL)
    {
        head = new_node(data);
    }
    else
    {
        node *it = head;
    
        while (it->info != data && it->next != NULL)
        {
            it = it->next;
        }
    
        if (it->info != data)
        {
            it->next = new_node(data);
        }
        else
        {
            puts("element present");
        }
    }
    It's early and I haven't had my coffee, so hopefully that logic is sound. ;D
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    22
    Good morning to u

    Thank you..Yes, the link list traverse logic is perfect. I spent all morning looking at it. Have a good day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on link list
    By bg1906 in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2007, 06:06 PM
  2. about link list - - please help
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-26-2006, 08:20 AM
  3. Link List
    By siavoshkc in forum C++ Programming
    Replies: 59
    Last Post: 08-01-2006, 11:48 PM
  4. Left justification using linked list of link list
    By hykyit in forum C Programming
    Replies: 7
    Last Post: 08-29-2005, 10:04 PM
  5. Link List
    By papedum in forum C Programming
    Replies: 3
    Last Post: 01-08-2002, 06:31 PM

Tags for this Thread