linked list faqs?
i was wondering if this site has any faq's on linked lists...i tried searching for them and i found nothing of use...anyways i was wondering if someone could give me a link to a page that had some information on them
thanks
lance
[247 byte] By [
lance743] at [2007-11-19 7:13:43]

# 1 Re: linked list faqs?
There are no FAQs for linked lists because maintiaining a FAQ for each type of container would be too much effort. (vectors, sets, hash_maps, multisets, etc...).
Really, the best thing for you to do is go get a book on data structures from Amazon. SGI's STL web site on STL lists (http://www.sgi.com/tech/stl/List.html) has some good information, but certainly not as much as a book on data structures would have.
# 2 Re: linked list faqs?
well im not buying a book for a homework problem...
but i do have a question...how do i add a new data item to my node...this is a copy of my teachers code..just trying to add a new (int) data type and then adding it to the list and then outputting it seems to be a hassle..
#include <iostream>
#include <ctime>
using namespace std;
class Node
{
private:
int ID;
char NodeName;
Node *Next;
public:
Node(){
ID = 0;
NodeName = 'A';
Next = '\0';
}
Node(int z) {ID = z;}
void SetID(int x) {ID = x;}
int GetID(){return ID;}
void SetNodeName(char x) { NodeName = x;}
char GetNodeName() {return NodeName;}
void SetPtr(Node *ptr) {Next = ptr;}
Node *GetPtr(){return Next;}
Node *AddANode(Node *Here, int ID) {
Node *NewNode;
NewNode = new(Node);
NewNode->SetID(ID);
//Sets NodeName to letter after NodeName of last node in the list
// so the nodes are named in sequence
NewNode->SetNodeName(Here->GetNodeName()+1);
Here->SetPtr(NewNode);
return(NewNode);
}
}; //End of class definition
void main()
{
Node *Start;
Node *PresentLocation;
srand(static_cast<unsigned>(time(0))); //sets seed for random number generator
//create first node
Start = new(Node);
PresentLocation = Start;
PresentLocation->SetID(1000 + int(1000.0 * rand()/(RAND_MAX+1.0)));
//add 40 more nodes
for(int x=0; x<20;x++)
PresentLocation = PresentLocation->
AddANode(PresentLocation, 1000 + int(1000.0 * rand()/(RAND_MAX+1.0)));
//Write code to find and display the largest ID in the list and the NodeName of the node where that
//largest value occurs
} //End of main
he is using random numbers to fill the id field...
how would i add a new variable to the node? like a int
this is what i have added to the code so far
private:
int ID;
int limit; //new var i want to add
char NodeName;
Node *Next;
public:
Node(){
ID = 0;
limit = 0; //new var i want to add
NodeName = 'A';
Next = '\0';
}
this declairs and initlizes the new variable "limit"
then,
void SetLim(int x) {limit = x}
int GetLim(){return limit;}
sets and shows the limit
now what im stuck on is how do i actually add this to the node? im pretty sure it is in here:
Node *AddANode(Node *Here, int ID) {
Node *NewNode;
NewNode = new(Node);
NewNode->SetID(ID);
//Sets NodeName to letter after NodeName of last node in the list
// so the nodes are named in sequence
NewNode->SetNodeName(Here->GetNodeName()+1);
Here->SetPtr(NewNode);
return(NewNode);
}
but im lost on what to put where, do i make a new pointer, should i add more varibles in the declaration of *AddANode, and so on... i could use some help
# 3 Re: linked list faqs?
If you want the user to determine the value of the new variable, then add a new argument to the function. Then, set the value in the node using your SetLim() function.
Node *AddANode(Node *Here, int ID, int limit) {
Node *NewNode;
NewNode = new(Node);
NewNode->SetID(ID);
NewNode->SetLim(limit);
//Sets NodeName to letter after NodeName of last node in the list
// so the nodes are named in sequence
NewNode->SetNodeName(Here->GetNodeName()+1);
Here->SetPtr(NewNode);
return(NewNode);
}