|
-
March 3rd, 2006, 04:31 PM
#1
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
-
March 3rd, 2006, 04:54 PM
#2
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()!
-
March 3rd, 2006, 05:05 PM
#3
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.
-
March 3rd, 2006, 05:29 PM
#4
Re: List size
 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()!
-
March 3rd, 2006, 05:46 PM
#5
Re: List size
Thanks I'll give it a go.
-
March 3rd, 2006, 06:42 PM
#6
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;
}
};
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|