CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: List size

  1. #1
    Join Date
    Apr 2005
    Posts
    79

    List size

    Hi,

    What would be the best way to keep a list size from being no greater than a specified amount.

    For example, if I update my list and the list size goes over a limit a node is deleted from the end of the list.

    Would it be better implementing a circular list or a doubly linked list?

    I have tried to implement the latter but can't work out how to keep track of the tail.

    Cheers

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: List size

    Why not implement your list in term of std::list ?
    Your class would contain a std::list object and a std::list::size_type object which would contain the number of items in the list.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  3. #3
    Join Date
    Apr 2005
    Posts
    79

    Re: List size

    Thanks for your suggestion but I would like to implement this myself rather than use any of the STL classes. The reason why is so I can learn the detail behind the algorithm and understand it.

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: List size

    Quote Originally Posted by jdm2104
    I have tried to implement the latter but can't work out how to keep track of the tail.
    You need to define two pointers as class's members.
    One pointer to the list head.
    And one pointer to the list queue.
    All operations on your list should be done from non-static member functions of your list, not on context-free iterators, because it would be hard to take trace of which list belong to your iterator.... or, you may include a pointer to the list as instance variable of the iterator.

    If you keep in mind that each member function of your list can assume that, before being called, the tail-pointer really points to the tail of your list (pre-condition), and must ensure that before returning, the tail pointer is also valid (post-condition), then your class will work!

    Now, if you find a more specific problem, you can post the code you have written.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  5. #5
    Join Date
    Apr 2005
    Posts
    79

    Re: List size

    Thanks I'll give it a go.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: List size

    What would be the best way to keep a list size from being no greater than a specified amount.
    A simple example:
    Code:
    #define MAXSIZE  10
    
    class mylist
    {
      int list[MAXSIZE];
      int index;
    public:
      mylist(): index(0) {}
    
      void add(int element) 
      {
        list[index++] = element;
        index %= MAXSIZE;
      }
    };
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured