LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-20-2004, 05:37 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
c++ list class, type of node?


I have been asked to use objects of type list, what I use them for is upt o me, I am supposed to use an iterator to scan through and print out everythign in at least one list.

my problem is I am trying ot make my iterator and I realise I do not have this required info:

what "Node" type is used in a list type.

as in

Type *interator

as well so far we have been making our own nodes and giving them a pointer called Link to point to the next. is Link the usual name? would the Node type use it? as well I noticed lists can also go backwards, if Link is next what is previous?

I have the oreilly c++ and STl references, a class book, and the C++ STL hardback and I cannot seem to find this info, maybe I do not know where to look, or maybe I have missed the concept here somewhere.. can anyone help me?
 
Old 05-20-2004, 05:55 PM   #2
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
code I need help with

here is some code: I have placed comments everywhere I need the info for.

Code:
#include <iostream>
#include <string>
#include <cstddef>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <list> //is this correct?

using namespace std;

int main()
{
	int i = 0;
	/*whatever node type the builtin list type uses?*/ *Iterator;
	list<int> FirstList;

	while (i < 6)
	{
		FirstList.push_back(i);
		i++;
	}

	Iterator = FirstList;
	while (Iterator != NULL)
	{
		cout << Iterator.begin();
		Iterator = Iterator->Link/*or whatever the object used as a node by the list type calls it.*/;
	}

	FirstList.reverse();

	Iterator = FirstList;
	while (Iterator != NULL) //should now print in reverse order
	{
		cout << Iterator.begin();
		Iterator = Iterator->Link/*or whatever the object used as a node by the list type calls it.*/;
	}

	FirstList.sort();

	Iterator = FirstList;
	while (Iterator != NULL) //should now print in sorted order
	{
		cout << Iterator.begin();
		Iterator = Iterator->Link/*or whatever the object used as a node by the list type calls it.*/;
	}
}
 
Old 05-20-2004, 06:27 PM   #3
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
well, I do not think it is what he wanted, but looking up the iterator type I got this final product that works:
#include <iostream>
#include <string>
#include <cstddef>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <list>

using namespace std;

int main()
{
int i = 0;
list<int> FirstList;
list<int>::iterator MyIterator;

while (i < 6)
{
FirstList.push_back(i);
i++;
}

cout << endl << "Initial:";
MyIterator = FirstList.begin();
while (MyIterator != FirstList.end())
{
cout << *MyIterator;
MyIterator++;
}

cout << endl << "Reversed:";
FirstList.reverse();
MyIterator = FirstList.begin();
while (MyIterator != FirstList.end())
{
cout << *MyIterator;
MyIterator++;
}

cout << endl << "Sorted:";
FirstList.sort();
MyIterator = FirstList.begin();
while (MyIterator != FirstList.end())
{
cout << *MyIterator;
MyIterator++;
}
cout << endl;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ generic list class? true_atlantis Programming 1 09-03-2005 10:17 PM
list<type> how can I make type be a pointer? exodist Programming 2 06-06-2005 08:40 AM
Implementing a vector class from a list class purefan Programming 9 04-14-2005 10:48 PM
total newbie type question-where do i find the good links for /etc/apt/sources.list? petkov Debian 2 03-28-2004 12:44 AM
node class help... leroy27336 Programming 1 02-25-2004 01:35 PM


All times are GMT -5. The time now is 02:06 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration