473,670 Members | 2,673 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 1593
"JohanS" <mu***********@ hotmail.com> wrote in message
news:b1******** *************** ***@posting.goo gle.com...
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::iter ator 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 CircularIterato r {
ListType& list;
typename ListType::itera tor it;

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

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

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

--it;
}
return *this;
}

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

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

typedef std::list<int> List;
typedef CircularIterato r<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******* *************@c omcast.com>...
"JohanS" <mu***********@ hotmail.com> wrote in message
news:b1******** *************** ***@posting.goo gle.com...
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::iter ator 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 CircularIterato r {
ListType& list;
typename ListType::itera tor it;

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

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

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

--it;
}
return *this;
}

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

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

typedef std::list<int> List;
typedef CircularIterato r<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
24100
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 convert n = "" # init new string for i in str: if i in chr: # if special character in str n+='\\' # append it with a backslash n+=i
7
3723
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 purports to be a comprehensive checklist when considering special-case inputs for functions. I thought the best way to puncture my ego would be to post that list here and see how well it survives :-) Here goes:
3
14669
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 (like Ç). I have written a code wherein i am specifying the list of characters that are not allowed. It would be better if i can give a list of characters that are allowed instead of list of characters that are not allowed. And check whether...
4
2855
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 __<special-methods>__ are used by each native Python data type? Thanks! Malcolm
5
2195
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 I'm having trouble determining what special methods I have to override in my class for the above two operations. From my testing, it seems to be __setslice__ for both, but the docs say __setslice__ and brethren are deprecated. I would have...
5
1470
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 words, I can do this: .... pass .... 33
3
3172
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 what i do. the user has 3 forms he will go through before he gets his background pdf. this is what happens. first, he uploads the pdf which is put in the upload map then the user specifies a name and some other modifications. then, my php...
17
2060
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 textarea and I have list of 90 ro 100 words and special chracter that I do not want user to enter. If they enter the item from the list of blocked word or character I want to give them error saying "this character or word in not allowed, please...
0
8468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7415
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
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 we have to send another system
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.