Thread: Help with a class assignment

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    21

    Help with a class assignment

    I need help on these 2 problems for an in class assignment. They are as follows:

    (1) Given the declaration:

    Code:
    typedef int ComponentType;
    
    struct NodeType;
    typedef NodeType* NodePtr;
    
    struct NodeType
    {
         ComponentType component;
         NodePtr link;
    }
    NodePtr lastPtr;
    NodePtr listPtr;
    NodePtr currPtr;
    NodePtr newNodePtr;
    assume that the list has a large collection of members and that currPtr is somewhere in the middle of the list. Write a code segment to remove the node following the node pointed to by currPtr and reinsert it at the head of the list.

    (2) Given the same declaration, write a void function that sorts the elements of the list in ascending order. The sort algorithm will scan the list, keeping a pointer to the lowest value seen thus far. When the end of the list is reached, the lowest value will be removed from that point and inserted at the end of a new list. After all of the elements have been moved from the original list to the new sorted list, change listPtr and currPtr to point to the first element of the new list. That way, when the function returns, the client code will simply see the list as having been sorted.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    What have you attempted?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    For (1):
    Code:
            NodePtr newNodePtr4 = new NodeType;
    
    	newNodePtr4 = currPtr->link;
    
    	cout << newNodePtr4->component << endl;
    
    	currPtr = currPtr->link;
    
    	cout << currPtr->component << endl;
    
    	newNodePtr4->link = listPtr;
    
    	listPtr = newNodePtr4;
    
    	delete currPtr;
    For (2), I am still trying to figure out a way.

    And yes I know that there is a ';' missing after the '}' at the end of the struct declaration, but the book left that out (don't you just love textbooks with typos).
    Last edited by EonsNearby; 03-19-2011 at 09:33 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>typedef NodeType* NodePtr;
    Remove this obfuscation.

    >>struct NodeType;
    This does not appear to be necessary.

    >> NodePtr newNodePtr4 = new NodeType;
    >> newNodePtr4 = currPtr->link;
    This spells disaster. First you are allocating a node, then you are simply replacing its value with that of another pointer, resulting in a memory leak.

    What is ListPtr?
    Last edited by Elysia; 03-19-2011 at 12:31 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    >>typedef NodeType* NodePtr;
    Remove this obfuscation.

    >>struct NodeType;
    This does not appear to be necessary.
    I can't remove these since the book says I have to have them in my program code even if they don't make sense or are unnecessary (like the struct NodeType you pointed out). In regards to what listPtr is, it is supposed to point to the first item in the linked list. Let me further clarify, there are other questions that came before these 2 that used the same declaration, but I already have those solved. The only thing necessary to know is that I have created 3 other nodes and put them all in the same linked list, and listPtr points to the first element in the linked list.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What the book says is irrelevant. I did not say remove them to break the code. They can be removed perfectly fine.
    The only thing that matters if it an exercise, is that it works and it is right and it is clear what the code does.
    Any book that obfuscates things like that is prone to dumping into the recycle bin. Don't teach yourself obfuscation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    I'm not say it is relevant, I am just saying that if I leave that out, then my professor will most likely subtract points from my assignment if I do leave that out, simply because the book said I had to use that exact code when doing the exercises.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is one thing is an idiotic professor hands you the code, and it is another things entirely if it's just code from the book you've copied.
    If unsure, consult your professor to make it's okay to remove the obfuscating code.
    Here is a picture showing what you've done, and hopefully you'll understand what's going wrong, and what you need to do to fix it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Okay, I got that problem resolved. Thanks for helping me.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Okay, I am having some trouble with (2). Here is the code I have thus far (the reason I have the excessive cout and system("Pause") functions is because I am still debugging it):


    Code:
    void sortList (NodePtr dataPtr, NodePtr currPosition)
    {
    	NodePtr current = dataPtr;
    
    	NodePtr newList = new NodeType;
    
    	int length = 0;
    
    	do
    	{
    		length++;
    
    		current = current->link;
    
    	} while (current->link != NULL);
    
    	length++;
    
    	current = dataPtr;
    
    	int passes = 0;
    
    	NodePtr current2 = newList;
    
    	while (length >= 0)
    	{
    		NodePtr smallest;
    
    		for (int count = 1; count < length; count++)
    		{
    			NodePtr temporary = new NodeType;
    			
    			temporary->component = current->component;
    
    			cout << "The value of the temporary NodePtr is: " << temporary->component << endl;
    
    			current = current->link;
    
    			system("Pause");
    
    			if (count == 1 || temporary->component < smallest->component)
    			{
    				smallest = temporary;
    
    				cout << "The value of the smallest NodePtr, thus far, is: " << smallest->component << endl;
    
    				system("Pause");
    			}
    		}
    
    		if (passes == 0)
    		{
    			newList->link = smallest;
    
    			newList = smallest;
    
    			passes++;
    		}
    
    		else
    		{
    
    			cout << "The value of the first NodePtr in newList is: " << newList->component << endl;
    
    			system("Pause");
    
    			current2->link = smallest;
    
    			cout << "The value of the last NodePtr in newList is, for now: " << current2->link->component << endl;
    
    			system("Pause");
    
    			passes++;
    
    			current2 = current2->link;
    		}
    
    		delete smallest;
    
    		length--;
    	}
    
    	dataPtr = newList;
    }
    I know I still need to set currPtr, but I am holding off on that until I get this working.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You still haven't gotten rid of NodePtr.
    And instead of adding lots of cour and pause statements, I'd recommend you learn a debugger. Visual Studio is an IDE with integrated debugger which is a snap to use and learn.
    Then there is GDB which mostly works with Code::Blocks. Haven't used it myself, though, but it's worth a shot if Visual Studio seems too big and bloated.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Which NodePtr are you referring too? If you are referring to what we discussed earlier, I won't be able to talk to my teacher about that until Monday, which is when the assignment is due.
    Last edited by EonsNearby; 03-19-2011 at 03:49 PM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    typedef NodeType* NodePtr;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    I am going to have to have that in my program code since I can't discuss that with my teacher until it is too late.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Elysia seems to be mandating that you follow what is merely an opinion here, which is disappointing.

    There is nothing wrong with having a typedef for a pointer type, especially when it's nicely named with a 'Ptr' at the end so it is perfectly clear. If you're aware of exactly how the typedef works and how to use the resulting type, then use it. Otherwise you shouldn't.

    Of course all of that is irrelevent if you have no choice but to use it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. i need help with errors in my assignment for class
    By azrael13302473 in forum C++ Programming
    Replies: 1
    Last Post: 07-04-2002, 06:02 PM