Linked List Node Problem

This is a discussion on Linked List Node Problem within the C Programming forums, part of the General Programming Boards category; Part of my program is to count all the elements on my list. I have no trouble doing this, but ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Linked List Node Problem

    Part of my program is to count all the elements on my list. I have no trouble doing this, but it only counts each int once. If there are duplicate elements it skips them. EX. 70 70 50 30 20 20 10 . My program would say there are 5 elements on my list when it should say 7. Here is a copy of this part of my program. Any suggestions would be appreciated. I tried to use a debug statement in my while loop, but i'm a little novice with C. Thank you.

    Code:
    #include "ll.h"
    
    int countAll(lnode * h) {
    int count = 0;
    
    while ( (h != NULL )) {
    count++;
    h=h->next;
    }
    return count;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    INDENT!

    You don't need two sets of brackets around the while condition.

    Otherwise, that looks like it should work; the function does not involve the values at the nodes, so your diagnosis cannot be correct -- it's based on coincidence. There is something more fundamental wrong with your list.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >I tried to use a debug statement in my while loop
    So if you see all the elements getting printed?

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    Yes. The list prints. Also wrote another count just to count the nodes which is similar to this and works great. I'm just having trouble counting all the duplicate elements. Thanks for your time.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What MK27 says. I would like to know what your struct actually looks like here. At first glance you are only checking if you have a struct that has not been initialized to NULL, not that you are checking for any data element within the struct. In other words what element in the struct are you setting these values for?

  6. #6
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Your count function is fine. You're problem is probably in your Add function where you are probably adding a new node only if the node doesn't already exist in the list. Check your Add code or post some more if you still cant find it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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