By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,636 Members | 1,814 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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
  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5. using std::ostream;
  6.  
  7. struct node {
  8.     int data;
  9.     node * p;
  10. };
  11.  
  12. void reverse(node * & );                        //  Student Written
  13.  
  14. void addFront( node * & start, int);
  15.  
  16. void print( ostream & ,  node *);
  17.  
  18. void cleanUp(  node *);
  19.  
  20. ostream & operator<<( ostream &, const node *);
  21.  
  22. void build( node * & );
  23.  
  24. void main()
  25. {
  26.  
  27.     node * a = NULL;
  28.     build(a);
  29.     cout << "Test of general case" << endl;
  30.     cout << "a is " << a << '\n';
  31.     reverse(a);
  32.     cout << "after reverse, a is " << a << endl << endl;
  33.  
  34.     cleanUp(a);
  35.     a = NULL;
  36.     cout << "Test of empty list" << endl;
  37.     cout << "a is " << a << '\n';
  38.     reverse(a);
  39.     cout << "after reverse, a is " << a << endl << endl;
  40.  
  41.     cout << "Test of 1 element list" << endl;
  42.     addFront(a,100);
  43.     cout << "a is " << a << '\n';
  44.     reverse(a);
  45.     cout << "after reverse, a is " << a << endl << endl;
  46.  
  47.  
  48.     addFront(a,200);
  49.     cout << "Test of 2 element list" << endl;
  50.     cout << "a is " << a << '\n';
  51.     reverse(a);
  52.     cout << "after reverse, a is " << a << endl;
  53.     cleanUp(a);
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60. void reverse( node * & s)                              
  61. {
  62.     //   NOTE:   YOU CAN NOT MOVE DATA FROM ONE NODE TO ANOTHER
  63.     //           YOU CAN ONLY MOVE POINTERS FROM ONE NODE TO ANOTHER
  64.  
  65.     // Grab the first node1    `
  66.     int startNode;  
  67.   // Create
  68.     startNode = s;
  69.     node *guest,*secondToLast,*secondElem;
  70.     guest=secondToLast=s;
  71.  
  72.     // Return true if list is only 1 element long
  73.     if (s->next == NULL)
  74.         return true;
  75.  
  76.     // Grab the second element in the list
  77.     secondElem = s->next;
  78.  
  79.     // Grab the second to last element in the list
  80.     while(guest->next!=NULL)
  81.     {
  82.         secondToLast=guest;
  83.         guest=guest->next;
  84.     }
  85.  
  86.     // Set the second to last node to now point to the original first node
  87.     secondToLast->next=startNode;
  88.     guest->next=secondElem;
  89.     return true;
  90. }
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

weaknessforcats
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

Post your reply

Sign in to post your reply or Sign up for a free account.