Thread: Best way to manage list of users?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    847

    Best way to manage list of users?

    I want to store information about users. When a new user is added my program needs to look thru each user record to see if that name is allready in use. Should I do this using classes? How do I search thru each class?
    I thought of having the class like this
    Code:
    class User
    {
       char Name[20];
       class *Next;
    }
    And then I would use the next pointer to look thru all classes but what would be the best way to fill in the next pointer?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Once you declare the class User, you need to declare instances of the User class (that is individual objects) to do anything useful. You can store the objects in any container you wish to simplify your task. Then search container by whatever mechanism is appropriate for the contaier. Containers include basic arrays and lists, containers from the Standard Template Libraray like vectors, queues, stacks, etc, non standard third party containers, and user declared/defined containers.
    You're only born perfect.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Classes default to private access. What you'd want is one of the following (equivalent) forms:
    Code:
    struct User
    {
       char Name[20];
       User* Next;
    }
    
    ... or ...
    
    class User
    {
    public:
       char Name[20];
       User* Next;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I just noticed that I didn't answer your question.

    The data structure that you have described is a singly-linked list. (There is a brief tutorial at http://www.cprogramming.com/tutorial/lesson15.html and Google would be an invaluable reference to get more information.)

    This will work for your purposes. You could just put the new nodes at the end and then search through the entire list, looking for the item, and then adding it if it isn't there. (You could order your list using an insertion sort algorithm, but this will not be of much help.)

    A slightly more complicated data structure that you could use is an ordered binary tree, and this would improve efficiency (worst case, it would be no worse than a linked list). Getting more advanced there are algorithms to balance binary trees as well.

    Starting off, however, a linked list is good.

    The search method that you would use with linked lists is sequential search. The algorithm you'd use for binary trees is called binary search.

    If you are not trying to do an exercise in data structure programming (a good exercise, though), you may want to look into using the std::list class.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM