count how many time the value appear in the link list -- HELP

This is a discussion on count how many time the value appear in the link list -- HELP within the C++ Programming forums, part of the General Programming Boards category; i think you are right.. so what you mean is that the display function is miss coded right? i dont ...

  1. #16
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    i think you are right..
    so what you mean is that the display function is miss coded right?
    i dont know where should i correct it in order to get the output as wat i mention above...

  2. #17
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    anyone please help ....i really need to finsih this up now... h help

  3. #18
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    id rather communicate here because: its permanent (or almost!), its public (so others can help and learn), and you can post your full code here with code tags for nice formatting.


    i dont think anythings wrong with your display function. please reread my previous response to see if it makes any other sense to you... i think you just need ONE node PER ip address. if you have a duplicate ip address, then increase that node's count variable.

    i dont see why you want to have duplicated nodes. (note: i said duplicated NODES not IP addresses)

    back in the morning! good luck

  4. #19
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by nadroj
    im kind of confused about the count. does this list contain duplicate IP addresses? if so, why is there a need to have duplicates?

    can you explain what the class in general is to do and this updateCount function in particular, then i can try and help.

    edit: i read your comments in the code so please dont just repeat those, if you can
    this list will contain duplicate address. it cannot be change cause is a part of work....

    ok here is the explanation of overall work ...

    <this program reads these IP addresses from a file and stores them in a linked list where each node will store the addresses and the number of times that address appears in the data file. list will be ordered in ascending order by IP address...As each address is read the program showuld check if it is already in the list. if it is then its count should be incremented by 1. if it isnt alreadyy int the list . should be inserted into the list so as to maintain the ordering of the list.


    the display will look like

    Please look at the list display
    Attached Images Attached Images  

  5. #20
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i still think from this description it is not requiring duplicate NODES. your not realizing that a NODE DOES NOT EQUAL IP ADDRESS.

    PLEASE read this. i will repeat it from a previous post, this is how your looking at how the output should be:

    Input File
    --------------------------
    192.149.089.061
    100.001.004.031
    034.056.078.012
    192.149.089.061
    100.001.004.031
    192.149.089.061
    111.022.033.004
    192.149.089.061
    111.022.033.004
    111.022.033.004

    IpAddress Count
    --------------------------
    192.149.089.061 4
    192.149.089.061 4
    192.149.089.061 4
    192.149.089.061 4
    100.001.004.031 2
    100.001.004.031 2
    034.056.078.012 1
    111.022.033.004 3
    111.022.033.004 3
    111.022.033.004 3

    this IS how it would look if you had duplicate NODES. you dont want duplicate nodes, you want duplicate IP ADDRESSES. a duplicate ip address is simply a node whose ip address has appeared 1+ times (which is stored in it's count variable).
    this is how the output would appear if you dont have duplicate nodes (note you still have duplicate ip addresses):

    Input File
    --------------------------
    192.149.089.061
    100.001.004.031
    034.056.078.012
    192.149.089.061
    100.001.004.031
    192.149.089.061
    111.022.033.004
    192.149.089.061
    111.022.033.004
    111.022.033.004

    IpAddress Count
    --------------------------
    192.149.089.061 4
    100.001.004.031 2
    034.056.078.012 1
    111.022.033.004 3


    are we on the same page yet?

  6. #21
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You state in the preconditions to that function that a node with that address has to exist, so why do you create a new one ?.
    If you really want to create a new node then you would have to remove the old one from the list and replace it with the new one.
    Kurt

  7. #22
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    hi Zuk, well is not has to exist.... is <if exist, increment the count .....>

  8. #23
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'm not talking about the overall logic. I'm talking about the precondition to the function updateCount().
    If exitance of the address is a precondition then updateCount() must not be called if the address doesn't exist. So according to the stated predcondition updateCount() has to find the node containing that address and simply increment its count member. No need to create a new node.
    BTW: the way your function works now you would create a memory leak because you don't free the old node and you don't link the new node into the list.
    Kurt

  9. #24
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I was thinking about something like this
    Code:
    void IPlist::updateCount(string address )
    // precondition : address is in list
    // postcondition : the count of node with address has been incremented
    {
       // no checks for 0 necessary, we know that it must be somewhere
        node *tmp = list;
        while( tmp->IPaddress != address)
            tmp = tmp->link;
        tmp->count++;
    }
    Kurt

  10. #25
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    thnx, the code is fine.. but the output is like this

    IP address count
    034.056.078.012 3998072
    100.001.004.031 3998073
    111.022.033.004 3998074
    192.149.089.061 3998100

    which is not right .. DAMN... :'(

    i was thinking the same too but that isnt work for me as well ...
    Last edited by peter_hii; 10-27-2006 at 09:06 AM.

  11. #26
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    How do you create new nodes ? Looks like count is not initialized to 1 when a new node is created.
    Kurt

  12. #27
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    where should i initialize count to 0 ???in the private of the class??

  13. #28
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    I think that the main code has a logical error also - else is missing see my comment inside your code
    Code:
    while(myFile>> filename)
     {
            if(ip.empty())                         // store IP address into link list
            {
                ip.insertInOrder(filename);
            }	
            //else is missing
    	//because we inserted the new node IP address is already present
    	//we will update the count for the first node - it is wrong
           if (!ip.isPresent(filename))       // if no duplicate ip address
           {
                               ip.insertInOrder(filename);    // we insert the ip address into link list
           }
           else
           {
                    ip.updateCount(filename);      // otherwise we add one to the count
           }
     }
     ip.display();

  14. #29
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    where should i initialize count to 0 ???in the private of the class??
    in the fuction insertInOrder you probably create new node.
    you should initialize all its members, including count

    Logically - at the beggining count should be set to 1 because it is the first occurence of this ip

  15. #30
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by vart
    in the fuction insertInOrder you probably create new node.
    you should initialize all its members, including count

    Logically - at the beggining count should be set to 1 because it is the first occurence of this ip
    so wat you mean is
    initialize list in the insertinorder???

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  3. Undefined Structure in Link List
    By _Cl0wn_ in forum C Programming
    Replies: 1
    Last Post: 03-22-2003, 04:57 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