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; hi everyone, i have an error while i compile this code. i tried two diff code but same error...as i ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

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

    hi everyone, i have an error while i compile this code. i tried two diff code but same error...as i know that void function canot return value.. so anyone can help me to fix and return the count number please///

    Code:
    void IPlist::updateCount(string address)
    // precondition : address is in list
    // postcondition : the count of node with address has been incremented
    {
    
            node* currPtr = list;
    	int TotalCount = 0;
    
    	do
    	{
    		TotalCount++;
    		currPtr = currPtr->link;
    	}while (currPtr != list);
    
    	return TotalCount;
    }
    Code:
    void IPlist::updateCount(string address)
    // precondition : address is in list
    // postcondition : the count of node with address has been incremented
    {
        int count = 0;
        node *tmp = list;
        while(tmp != NULL && tmp->IPaddress == address)
        {
            count++;
            tmp = tmp->link;
        }
        return count;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void IPlist::updateCount(string address)
    To return a value, change void to the type you want to return:
    Code:
    int IPlist::updateCount(string address)

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    do you have any other optioon instead of changing the type as if it could not be change


  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you must keep the return type void can you change the parameter list to include a reference to int, ie:
    Code:
    void IPlist::updateCount(string address, int &count)
    // precondition : address is in list
    // postcondition : the count of node with address has been incremented
    {
        count = 0;
        node *tmp = list;
        while(tmp != NULL && tmp->IPaddress == address)
        {
            count++;
            tmp = tmp->link;
        }
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    The only other way to return a value from a function by adding a parameter in the function's declaration.
    Code:
    void IPlist::updateCount(string address, int &count)
    The above would allow you to return an int in count.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    how should i call from main

    just to make sure cause got errors popping out

    ip.updateCount(data[i]);

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    how many parameters does the updateCount function now require? and what types? take a minute to figure it out

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    ok forget to let you know that the struct type

    Code:
    struc node(
    {
        string IPaddress;
        int count;
        node * link;
    }
    which means that we count the number of occurences and display it ,,,

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    here is the class file

    Code:
    #ifndef IPLIST
    #define IPLIST
    
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    struct node
    {
       string IPaddress;
       int count;
       node *link;
    };
    
    class IPlist
    {
       // contains an ordered list of IP addresses and a count of each
       // occurrence
       public:
    
        IPlist();  // default constructor
                   // creates a new empty list
    
        ~IPlist(); // destructor - reallocates all dynamic memory of list
    
        IPlist (const IPlist & other);
                   // copy constructor to create a deep copy of other IPlist
                   // object
    
        bool empty() const; 
                   // precondition : none
                   // postcondition : returns true if list is empty 
                   //                 and otherwise false
    
        void insertInOrder(string address);
                   // precondition : list is ordered and address is not in list
                   // postcondition : list is ordered and contains address
    
        bool isPresent(string address) const;
                   // precondition : none
                   // postcondition : returns true if a node containing address
                   //                is present in list and otherwise false
    
        void updateCount(string address);
                   // precondition : address is in list
                   // postcondition : the count of node with address has been incremented
    
        node *find (string address) const;
                   // precondition : none
                   // postcondition : returns a pointer to node containing address or
                   //                 NULL if not present
    
        void display() const; 
                   // precondition : none
                   // postcondition : the list has been displayed 
                   // one record per line, tab-separated
                   // with heading IPaddress Count
        private:
        
           node *list;
    };
    #endif

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    i am sorry..

    i try to explain...

    the updatecount is to update how many duplicate ip address in the list...
    tats all it does....

    better explanation

    u just
    increase count by one (if found duplicate) otherwise you 'put in' the new address......

    hope it helps u

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    as i asked in previous post, do you WANT to allow duplicates? if this is an ordered list, how do you determine what duplicate is 'first' before another duplicate?

    if you have a duplicate then you will have 2 identical nodes, correct? ie, 2 nodes with same ip address and same count (2), right?

    i think you should either get rid of the count or get rid of the duplicates.. or is there a better reason you need duplicates and count that i dont see? or is this some project which REQUIRES it?

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    yes, it is requirement. and yes this list will containt duplicate and
    for example
    input file is

    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

    and the output will be

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

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    from the looks of the output there are only 4 NODES that you create (or at least print).

    i dont understand if you want duplicate NODES (notice i said nodes not ip addresses), AND you want to count how many times each node occurs, even with duplicates..

    the output you shown is not containing duplicate NODES but duplicate ipaddresses, if it had duplicate nodes and you incremented each count appropriatly, the output would be:

    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

    i hope im making sense. i think the solution is to get rid of duplicate nodes. when you add another ip address, then ,as you have in the updateCount function, search your list for the new ipaddress and increment that node's count.. you dont need to create another node.

    or is what im saying still not satisfy the requirement of needing duplicate NODES. realize in this case duplicate nodes are just that.. 2 nodes with EXACTLY the same data, it doesnt make sense to me.

    im off to bed, ill check back in the morning.

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    thanks and i will try to explain it clearly ... please check error when u have time alright .. appreciate tat... can you send me ur emaill addresss as i want to send you whole the cpp and it might easier for you to understand

Page 1 of 3 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, 04:09 AM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  3. Undefined Structure in Link List
    By _Cl0wn_ in forum C Programming
    Replies: 1
    Last Post: 03-22-2003, 05:57 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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