Linked List Questions

Wanna understand E=mc2, English grammer, or maybe just build a computer? We can help!

Moderator: Moderators

Post Reply
LoSTimberWolf
Inmate
Posts: 128
Joined: Sun Jan 27, 2002 4:17 pm

Linked List Questions

Post by LoSTimberWolf »

My Program 7 for class is dealing with linked lists. Each node contians an object and a pointer. I must be able to output the numbers in the object in each node, but I currently can not. I also am getting an error that does not make sense, " 'cout' : ambiguous symbol". The next problem is how do you reverse a linked list? I know it takes a temp list to copy into but I still can't figure out the proper configuration of loops and limits for those loops. Any information would be helpful. Thanks in advance. <p><!--EZCODE BOLD START--><strong>-LoS-</strong><!--EZCODE BOLD END--><!--EZCODE ITALIC START--><em>TimberWolf</em><!--EZCODE ITALIC END-->{FF}<br>Vehicle Specilist</p><i></i>
Scorch
Inmate
Posts: 215
Joined: Thu Mar 16, 2000 12:02 am

Re: Linked List Questions

Post by Scorch »

Assuming you have...<br><br>There are many ways of setting up a link list, from the sound of it here is what you have (or somthing similar, perhaps a LL class itself, but I don't wantto have to write everything now <!--EZCODE EMOTICON START :) --><img src=http://www.ezboard.com/images/emoticons/smile.gif ALT=":)"><!--EZCODE EMOTICON END--> )<br><br>Anyways<br><br><!--EZCODE CODE START--><pre> class Node {Node *next;int number;};Node::Node(){next=NULL;}in main you would havemain(){int x;Node *top, *curr, *rev, *revPrev;I can't remember if you need *Node or just Node for the new, sorry <!--EZCODE EMOTICON START :( --><img src=http://www.ezboard.com/images/emoticons/frown.gif ALT=":("><!--EZCODE EMOTICON END--> top = new Node; // Top is now the top of your linked listrev = new Node;revPrev = NULL;curr = top; // I do it this way (use a seperate pointer) that way I reduce the risk of breaking the list.while (condition != met){cin>> curr->number; // or curr.number forget which try one if it don't work try other.curr->next = new Node;curr = curr->next; // moves to new thing, although in this// case you would alwayshave an empty node at the end of //your list, no biggie, but you might want to play around with //what I have to get rid of it.}Now to reverse it, assuming you always have the last one pointing at NULL.curr = top;while (curr != null){rev->next = revPrev;revPrev = rev;rev.number = curr.number;rev= new Node;curr = curr->next;}</pre><!--EZCODE CODE END--><br><br>*edited for line length.* <p></p><i>Edited by: <A HREF=http://pub141.ezboard.com/bxmenclan.sho ... ambit>XMEN Gambit</A>&nbsp; <IMG SRC="http://www.xmenclan.org/images/x.gif" BORDER=0> at: 4/2/03 10:04:25 pm<br></i>
User avatar
XMEN Gambit
Site Admin
Posts: 4122
Joined: Thu Nov 18, 1999 12:00 am

Re: Linked List Questions

Post by XMEN Gambit »

Scorch's answer is pretty good - a few syntax errors but that what the compiler's first pass is for, right?<br><br>This might be "cheating" a bit, but you COULD build yourself a doubly-linked list, where each node has a pointer to the previous node AND to the next node. Then you'd only need a pointer to the tail node to traverse the list in reverse order. <br><br><br>I love linked lists. Learning about them was one of those moments in computer science when the lightbulb clicked on and I went "WOW!". <!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i></i>
Image
LoSTimberWolf
Inmate
Posts: 128
Joined: Sun Jan 27, 2002 4:17 pm

Re: Linked List Questions

Post by LoSTimberWolf »

Ok, my only listed compiling error is an INTERNAL COMPILER ERROR, I so am trecking along to my thursday deadline. <p><!--EZCODE BOLD START--><strong>-LoS-</strong><!--EZCODE BOLD END--><!--EZCODE ITALIC START--><em>TimberWolf</em><!--EZCODE ITALIC END-->{FF}<br>Vehicle Specilist</p><i>Edited by: <A HREF=http://pub141.ezboard.com/bxmenclan.sho ... berWolf</A> at: 4/2/03 8:14:41 pm<br></i>
User avatar
XMEN Gambit
Site Admin
Posts: 4122
Joined: Thu Nov 18, 1999 12:00 am

Re: Linked List Questions

Post by XMEN Gambit »

Um, are you trying to compile Scorch's code, or did you write some yourself? <br>Do you have a line number? <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i></i>
Image
LoSTimberWolf
Inmate
Posts: 128
Joined: Sun Jan 27, 2002 4:17 pm

Re: Linked List Questions

Post by LoSTimberWolf »

I have written my own code based off of the functions that already exist in the object I am modifing. It is having a problem witht he cout statement still. This function came with the object. Here is the function:<br><br>void RectangleList::displayList(void)<br>{<br>&nbsp &nbsp &nbsp &nbsp ListNode *nodePtr;<br><br>&nbsp &nbsp &nbsp &nbsp nodePtr = head;<br>&nbsp &nbsp &nbsp &nbsp while (nodePtr)<br>&nbsp &nbsp &nbsp &nbsp {<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp cout << "Width: " << nodePtr->value.getWidth << ", ";<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp cout << "Length: " << nodePtr->value.getLength << ", ";<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp cout << "Area: " << nodePtr->value.getArea << endl;<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp nodePtr = nodePtr->next;<br>&nbsp &nbsp &nbsp &nbsp }<br>} <p><!--EZCODE BOLD START--><strong>-LoS-</strong><!--EZCODE BOLD END--><!--EZCODE ITALIC START--><em>TimberWolf</em><!--EZCODE ITALIC END-->{FF}<br>Vehicle Specilist</p><i></i>
XMEN Ashaman DTM
Inmate
Posts: 2369
Joined: Mon Oct 02, 2000 12:09 am
Location: Silverdale, WA

Re: Linked List Questions

Post by XMEN Ashaman DTM »

This might sound dumb, but aren't your "<<" supposed to point the other way in an assignment? (">>" instead of "<<")<br> <p><br><img src=http://www.xmenclan.org/avatars/ashaman2.jpg width=150 align=left><br><left><a href=http://www.dragontalonmercs.com>Dragon Talon Mercenary</a><br><a href=http://www.xmenclan.org>Member of the XMEN</a><br>Have a Nice Day!<br><br></left></p><i></i>
XMEN Ashaman DTM
Inmate
Posts: 2369
Joined: Mon Oct 02, 2000 12:09 am
Location: Silverdale, WA

Re: Linked List Questions

Post by XMEN Ashaman DTM »

For the 'cout' function, I mean.<br><br>cout>>blahblah <p><br><img src=http://www.xmenclan.org/avatars/ashaman2.jpg width=150 align=left><br><left><a href=http://www.dragontalonmercs.com>Dragon Talon Mercenary</a><br><a href=http://www.xmenclan.org>Member of the XMEN</a><br>Have a Nice Day!<br><br></left></p><i></i>
User avatar
XMEN Gambit
Site Admin
Posts: 4122
Joined: Thu Nov 18, 1999 12:00 am

Re: Linked List Questions

Post by XMEN Gambit »

Well, in this particular case I think I see your problem.<br><br>What is "value.getWidth" ? Is that a public member variable? Or is it a method? If it is the latter, which it most likely is, then you need to indicate that it is a method by adding a set of "()". The compiler chokes on it because cout doesn't have a clue what to do with a function pointer.<br><br>Learning to debug is the single most useful universal programming skill.<br><br>(Asha, cin and cout are kind of weird beasties. Hardly ever used in a production app, they're more for debugging. The data flows in the direction of the arrows, and you want the data going out to the console.) <p><!--EZCODE IMAGE START--><img src="http://www.xmenclan.org/xmengambit.gif"/><!--EZCODE IMAGE END--><br>XMEN member<br>Card-carrying DTM<br>OKL Fish-napper<br><br>Though a program be but three lines long, someday it will have to be maintained.<br><!--EZCODE ITALIC START--><em> The Tao of Programming</em><!--EZCODE ITALIC END--></p><i>Edited by: <A HREF=http://pub141.ezboard.com/bxmenclan.sho ... ambit>XMEN Gambit</A>&nbsp; <IMG SRC="http://www.xmenclan.org/images/x.gif" BORDER=0> at: 4/2/03 10:10:38 pm<br></i>
Image
XMEN Ashaman DTM
Inmate
Posts: 2369
Joined: Mon Oct 02, 2000 12:09 am
Location: Silverdale, WA

Re: Linked List Questions

Post by XMEN Ashaman DTM »

Ah! That's odd. Though I don't think I've used either function more than once. Heh.<br> <p><br><img src=http://www.xmenclan.org/avatars/ashaman2.jpg width=150 align=left><br><left><a href=http://www.dragontalonmercs.com>Dragon Talon Mercenary</a><br><a href=http://www.xmenclan.org>Member of the XMEN</a><br>Have a Nice Day!<br><br></left></p><i></i>
Post Reply