need help with singly linked-list

This is a discussion on need help with singly linked-list within the C++ Programming forums, part of the General Programming Boards category; daved, whats wrong with my code ? i get the output that i need. i just facing with 2 more ...

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    daved,
    whats wrong with my code ?
    i get the output that i need.

    i just facing with 2 more problems.

    for prob no 1.....

    temp5->department != "SALES" is comparing pointers.
    what i need to do is like this

    strcmp(s,"exit")!=0

    how to change the pointer back to char ?

    i did like this :

    Code:
    char *dp;
    temp5->department = *dp;
    if(temp5->salary < 100  && strcmp(dp,"SALES")==0)
    but it gives me compilation error.

  2. #17
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    1. Mostly because of the way you attempt to compare. You understand how to apply your filter, but character arrays compare their memory locations with !=, not their contents. The fix is to use std::strings, where the != operator does what you need.

    2. Try using a simpler format like "yyyy/mm/dd" and using a string comparison:
    Code:
    #include <string> 
    
    struct node {
      std::string date;
      // other members ...
    };
    
    // ... in the filter:
    if( node.date > "2008/01/01" ) 
      // then ...
    It just so happens that later dates, when stored as text, can be ordered chronologically this way.
    Last edited by whiteflags; 04-08-2008 at 01:42 PM.

  3. #18
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    Quote Originally Posted by citizen View Post
    1. Mostly because of the way you attempt to compare. You understand how to apply your filter, but character arrays compare their memory locations with !=, not their contents. The fix is to use std::strings, where the != operator does what you need.
    can u show me some examples of std::strings. ?

    is it like this ?

    Code:
    if(temp5->salary < 100  && std::string temp5->department != "SALES")
    but i got compilation error
    Last edited by vearns; 04-08-2008 at 01:51 PM.

  4. #19
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Well I suppose I could come up with an example you could use. Not much of this will look familiar but you will get the idea. You should probably use a more C++ like approach to this anyway.

    You need to build a class for your linked list. First, concentrate on creating and then deleting nodes. After you get that working, then you have a solid foundation to build upon, because you can properly make and destroy a list, and it will be time to move on to writing the algorithms you need.

    You will copy your data into a list.

    Then you will apply your filter like this, in pseudocode:
    Code:
    procedure apply_filter ( this_filter_func f ) :
      i = 0;
      while i < list.size() :
        if f( list( i ) ) :
          display list( i )
        endif
      endwhile
    done
    Hopefully that's not too bad.

    For a more concrete example, look at the C++ STL:
    Code:
    #include <list>
    #include <algorithm>
    #include <string>
    
    struct node
    {
        std::string name;
        std::string dept;
        float salary;
        // other members ...
    };
    
    bool filter ( const node & n )
    {
        return n.salary > 100.0f || n.dept != "SALES"; 
    }
    
    int main()
    {
        // copy your data ubto a linked list:
        node data[] = { 
            { "Joe", "SALES", 99.0f }, { "Sam", "MGMT", 200.0f }, 
            { "Bob", "ACCNT", 80.0f }, { "Oliver", "MGMT", 399.0f } 
        };
        std::list<node> full_list( data, data+4 );
        std::list<node> poor_list( full_list.size() );
        
        // apply your filter:
        std::list<node>::iterator last_copied = std::remove_copy_if( 
            full_list.begin(), full_list.end(), poor_list.begin(), filter );
    
        // fixing the list size:
        poor_list.erase( last_copied, poor_list.end() );
    }
    And now, if you look, the poor_list only contains the poor salesman named Joe.

    I dunno if I expressed myself very well, but this is the sort of approach a more experienced person would take. If you want to build the linked list data structure yourself (always a fun, learning exercise), go ahead. Just make sure that you build appropriate functions to make a list, apply your filter to items in that list, and display the result.

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by vearns View Post
    i removed old codes.
    and i put my new codes.
    Do you not understand that you should not do that?!
    You make it very hard for others to help you, and for anyone else to learn from your mistakes, and is very selfish.
    I genuinely did have helpful advice for the problems you were facing, and really did simply choose not to post it. Obviously my humourous hint did not express the "back-at ya" annoyance it was meant to convey, or you would have broken the habbit very quickly.

    Next time you post, I'm going to quote your entire post in my own; You can't REMOVE that!
    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"

  6. #21
    coder
    Join Date
    Feb 2008
    Posts
    127
    True
    please do not remove your codes the next time.
    This post has been useful for you but it's useless for others which would have learn from your mistake instead.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  2. Replies: 6
    Last Post: 03-02-2005, 01:45 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. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 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