Thursday, July 9, 2009

C++ help!?

I made a thread earlier but didnt get any help so can someone tell me what Im suppossed to do this is my assignment and I cant figure out what I am suppossed to make:





Write a program that creates a linked list of points. This linked list should insert the points ordered by the x value. The user is prompted to enter as many points as they desire. Each point is a new node in the linked list. After the user has entered all the points they wish to enter, the program is to output the linked list data as it appears in the linked list.








What should my program be doing??





Thanks!

C++ help!?
Umm.. I think your second paragraph states what your program should be doing pretty clearly. Can you be more clear? ^_^





But just in case, here's a visual example of my interpretation:





Say the user first enters the point (5,6). Your program should store this data in a linked list:





head -%26gt; [(5,6)] -%26gt; NULL





The user then enters the point (7,7). Your program should insert this data into the linked list, sorted by the x value:





head -%26gt; [(5,6)] -%26gt; [(7,7)] -%26gt; NULL





The user then enters the point (6,1). Your program should insert this data into the linked list, sorted by the x value:





head -%26gt; [(5,6)] -%26gt; [(6,1)] -%26gt; [(7,7)] -%26gt; NULL





If the user wants to stop at this point, your program should traverse the linked list and print out the data:





(5,6)


(6,1)


(7,7)





Hope that helps!
Reply:Make a XYPoint class that contains a pointer to a XYPoint. That pointer is your link, initialize it to NULL.





You start with a XYPoint pointer, called head for instance, that is your head of the list. Initialize it to NULL.





When the user enters an X, Y pair, create a XYPoint object, called newXY for instance. Then loop starting from the head pointer until you find a XYPoint object with an X value that's larger than the new point's X value. You set the new objects link pointer to point to the larger X XYPoint object. You set the previous pointer, either the head pointer or the previous object's link pointer, to point to the new object.





Something like this:


XYPoint *head = NULL


begin input loop


.. get X and Y


XYPoint *newXY = new XYPoint( X, Y )


if head is NULL then head = newXY


else if X is less than head-%26gt;X then


newXY-%26gt;next = head


head = newXY


else


XYPoint *pos = head


while pos-%26gt;next isnt NULL and pos-%26gt;next-%26gt;X is less than X then pos = pos-%26gt;next


newXY-%26gt;next = pos-%26gt;next


if pos-%26gt;next isnt NULL then pos-%26gt;next = newXY


endif





To print them, just start at head and follow the next pointers.
Reply:struct Node


{


int x, y;


Node *next;


};





Node *head = NULL;


get input


Node *input = new Node(x, y, NULL);


if head == NULL then


head = input


else


{


Node *link = head, *prev = head;


while (link != NULL)


{


if (link-%26gt;x %26gt; input-%26gt;x)


{


input-%26gt;next = link;


prev-%26gt;next = input;


link = NULL;


}


else


{


prev = link;


link = link -%26gt; next;


}


} // while loop


}


Display list


for (link = head; link != NULL; link = link-%26gt;next)


{


Display link-%26gt;x, link-%26gt;y in appropriate form


}


No comments:

Post a Comment