473,513 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Special linke list

Hi

I have created my own linked list where last always points to first so
the list is like a circle that goes round and round.

Now i wanna use the list from the STL instead but i still want the
last element to point to the first. Is there some good way of doing
this with the STL list?
Jul 22 '05 #1
2 1584
"JohanS" <mu***********@hotmail.com> wrote in message
news:b1**************************@posting.google.c om...
Hi

I have created my own linked list where last always points to first so
the list is like a circle that goes round and round.

Now i wanna use the list from the STL instead but i still want the
last element to point to the first. Is there some good way of doing
this with the STL list?


You could write a wrapper around a std::list::iterator so that incrementing
an iterator wrapper that was at the end of the std::list would cause it to
return to the first element, and decrementing an iterator wrapper that was
at the beginning of the std::list would cause it to jump to the last
element. Simple example:

template <class ListType>
class CircularIterator {
ListType& list;
typename ListType::iterator it;

public:
CircularIterator(ListType& theList) : list(theList), it(list.begin()){}
CircularIterator& operator++() {
// do nothing on empty list
if ( it != list.end() ) {
++it;

if ( it == list.end() ) {
it = list.begin();
}
}
return *this;
}

CircularIterator& operator--() {
// do nothing on empty list
if ( it != list.end() ) {
if ( it == list.begin() ) {
it = list.end();
}

--it;
}
return *this;
}

typename ListType::reference operator*() {
return *it;
}

typename ListType::iterator operator->(){
return it;
}
};

typedef std::list<int> List;
typedef CircularIterator<List> Iter;
int main()
{
List list;
for ( int i = 0; i < 9; ++i ) {
list.push_back(i);
}

Iter it(list);

std::cout << "Forward" << std::endl;
do {
std::cout << *it << std::endl;
++it;
} while ( *it != 0 );

std::cout << "Reverse" << std::endl;
do {
std::cout << *it << std::endl;
--it;
} while ( *it != 0 );

return 0;
}

--
David Hilsee
Jul 22 '05 #2
"David Hilsee" <da*************@yahoo.com> wrote in message news:<nK********************@comcast.com>...
"JohanS" <mu***********@hotmail.com> wrote in message
news:b1**************************@posting.google.c om...
Hi

I have created my own linked list where last always points to first so
the list is like a circle that goes round and round.

Now i wanna use the list from the STL instead but i still want the
last element to point to the first. Is there some good way of doing
this with the STL list?


You could write a wrapper around a std::list::iterator so that incrementing
an iterator wrapper that was at the end of the std::list would cause it to
return to the first element, and decrementing an iterator wrapper that was
at the beginning of the std::list would cause it to jump to the last
element. Simple example:

template <class ListType>
class CircularIterator {
ListType& list;
typename ListType::iterator it;

public:
CircularIterator(ListType& theList) : list(theList), it(list.begin()){}
CircularIterator& operator++() {
// do nothing on empty list
if ( it != list.end() ) {
++it;

if ( it == list.end() ) {
it = list.begin();
}
}
return *this;
}

CircularIterator& operator--() {
// do nothing on empty list
if ( it != list.end() ) {
if ( it == list.begin() ) {
it = list.end();
}

--it;
}
return *this;
}

typename ListType::reference operator*() {
return *it;
}

typename ListType::iterator operator->(){
return it;
}
};

typedef std::list<int> List;
typedef CircularIterator<List> Iter;
int main()
{
List list;
for ( int i = 0; i < 9; ++i ) {
list.push_back(i);
}

Iter it(list);

std::cout << "Forward" << std::endl;
do {
std::cout << *it << std::endl;
++it;
} while ( *it != 0 );

std::cout << "Reverse" << std::endl;
do {
std::cout << *it << std::endl;
--it;
} while ( *it != 0 );

return 0;
}

I will try that. Thanks alot. Really appreciate it.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
24061
by: tertius | last post by:
Is there a better way to append certain chars in a string with a backslash that the example below? chr = "#$%^&_{}" # special chars to look out for str = "123 45^ & 00 0_" # string to...
7
3715
by: Ben Finney | last post by:
Howdy all, Ned Batchelder blogged about a debate over checking function parameters, and to what extent dynamic typing should be relied upon. I was one of many who commented, but I wrote what...
3
14662
by: jeyabarani | last post by:
Hi guys, I want to check whether the user has entered any special characters in a text box. If the user enters, i want to display an alert message stating that he cant enter a special character...
4
2847
by: python | last post by:
Is there an official list of all Python's __<special-methods>__? I'm learning Python and trying to get an idea of what __<special-methods>__ are available and specifically, what...
5
2177
by: Mike Kent | last post by:
For Python 2.5 and new-style classes, what special method is called for mylist = seq and for del mylist (given that mylist is a list, and seq is some sequence)? I'm trying to subclass list, and...
5
1463
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
3
3167
Ciary
by: Ciary | last post by:
hi all, i'm stuck with a rather rare situation. like most some people will know, i've been busy trying to set an uploaded pdf as background image. now, i encountered a new problem. let me explain...
17
2052
by: newcoder10 | last post by:
Hi All, I would like to know what's the best way to write function(global) in asp.net c# Framework 2.0 to check for textbox in a form (i have about 80 textbox on one form and I have many forms and...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.