459,636 Members | 1,814 Online
Need help? Post your question and get tips & solutions from a community of 459,636 IT Pros & Developers. It's quick & easy.

# How do I grab the first Node?

 P: 2 void reverse( node * & s) { // NOTE: YOU CAN NOT MOVE DATA FROM ONE NODE TO ANOTHER // YOU CAN ONLY MOVE POINTERS FROM ONE NODE TO ANOTHER // Grab the first node` // Create startNode = s; node *guest,*secondToLast,*secondElem; guest=secondToLast=s; // Return true if list is only 1 element long if (s->next == NULL) return true; // Grab the second element in the list secondElem = s->next; // Grab the second to last element in the list while(guest->next!=NULL) { secondToLast=guest; guest=guest->next; } // Set the second to last node to now point to the original first node secondToLast->next=startNode; guest->next=secondElem; return true; Nov 10 '09 #1
5 Replies

 Expert 100+ P: 2,418 What is this function supposed to do? In what way is it not doing what you want it to do? Nov 10 '09 #2

 P: 2 Donbock, I am just trying to change the syntax from psudo to actual code, I have included the rest of the code in this email The fuction is supposed to switch the first and last node in a linked list by just changing the pointers The code is shooting errors not the least of which cannot convert from node * to int Thanks again for any help, Goldenteeplanet Expand|Select|Wrap|Line Numbers #include  using std::cout; using std::cin; using std::endl; using std::ostream;   struct node {     int data;     node * p; };   void reverse(node * & );                        //  Student Written   void addFront( node * & start, int);   void print( ostream & ,  node *);   void cleanUp(  node *);   ostream & operator<<( ostream &, const node *);   void build( node * & );   void main() {       node * a = NULL;     build(a);     cout << "Test of general case" << endl;     cout << "a is " << a << '\n';     reverse(a);     cout << "after reverse, a is " << a << endl << endl;       cleanUp(a);     a = NULL;     cout << "Test of empty list" << endl;     cout << "a is " << a << '\n';     reverse(a);     cout << "after reverse, a is " << a << endl << endl;       cout << "Test of 1 element list" << endl;     addFront(a,100);     cout << "a is " << a << '\n';     reverse(a);     cout << "after reverse, a is " << a << endl << endl;         addFront(a,200);     cout << "Test of 2 element list" << endl;     cout << "a is " << a << '\n';     reverse(a);     cout << "after reverse, a is " << a << endl;     cleanUp(a); }           void reverse( node * & s)                               {     //   NOTE:   YOU CAN NOT MOVE DATA FROM ONE NODE TO ANOTHER     //           YOU CAN ONLY MOVE POINTERS FROM ONE NODE TO ANOTHER       // Grab the first node1    `     int startNode;     // Create     startNode = s;     node *guest,*secondToLast,*secondElem;     guest=secondToLast=s;       // Return true if list is only 1 element long     if (s->next == NULL)         return true;       // Grab the second element in the list     secondElem = s->next;       // Grab the second to last element in the list     while(guest->next!=NULL)     {         secondToLast=guest;         guest=guest->next;     }       // Set the second to last node to now point to the original first node     secondToLast->next=startNode;     guest->next=secondElem;     return true; } Nov 11 '09 #3

 Expert 100+ P: 2,418 Please read our forum posting guidelines located at this link: http://bytes.com/faq.php?faq=posting_guidelines You will find that you will receive much more high quality help by adhering to these simple rules and suggestions. For example using [code] ... [ \code] tags. Code tags put line numbers in your code snippet -- making it much easier for helpers to point at specific problems in the code. Nov 11 '09 #4

 Expert 100+ P: 2,418 @goldenteeplanet The compiler error messages typically provide all kinds of detail to help you find and fix the errors. You will get quicker and more helpful assistance if you clearly state what is going wrong with your code. This includes providing the full text of any error messages. Most compiler error messages specify a line number. Frequently the code snippet you post is a subset of your original program. It is especially helpful if you edit the error message so that it specifies the line number in your posted snippet that corresponds to that same line in your original program. Here is a quick list of problems I see with your code. No doubt there are others.Compare lines 6 and 8 against 20 and 35. The function is defined to not have a return value, but you try to return a boolean value. Compare lines 6 and 8 against 11 and 14. Argument s is of type node * &, but you assign it to int variable startNode. In line 33 you do the reverse. I don't understand C++ reference syntax, so I don't know if it is ok to assign node * &s to node *guest (line 16). Compare lines 3 and 19. The node structure does not have a field called next. On line 19 you should have verified that s isn't NULL before you dereferenced the pointer. (No compiler error for this one.) Did you cut and paste this code from somewhere else? Nov 11 '09 #5

 Expert Mod 5K+ P: 9,197 Another problem is that there is no list. All I see is a struct for the nodes. Where is the struct for the list? You know, the struct where you keep the address of the first and last node of the list plus the address of the current node used by functions that traverse the list. This missing struct is causing a lot of trouble. Nov 11 '09 #6