How do I grab the first Node?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goldenteeplanet
    New Member
    • Nov 2009
    • 2

    How do I grab the first Node?

    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,*secondT oLast,*secondEl em;
    guest=secondToL ast=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=gu est;
    guest=guest->next;
    }

    // Set the second to last node to now point to the original first node
    secondToLast->next=startNode ;
    guest->next=secondEle m;
    return true;
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    What is this function supposed to do?
    In what way is it not doing what you want it to do?

    Comment

    • goldenteeplanet
      New Member
      • Nov 2009
      • 2

      #3
      Thanks for the quick reply!

      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


      Code:
      #include <iostream>
      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
      
        [B]  // Grab the first node1	`
      	int startNode;[/B]  
        // 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;
      }
      Last edited by Banfa; Nov 11 '09, 09:49 AM. Reason: Added [Code] ... [/code] tags

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Please read our forum posting guidelines located at this link:


        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.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by goldenteeplanet
          Code:
          struct node {
              int data;
              node * p;
          };
          
          void reverse(node * & );
          
          void reverse( node * & s)                              
          {
              // 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;
          }
          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?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            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.

            Comment

            Working...