This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:spring2014:data:projects:nodes [2014/01/27 10:07] – [Pointers] wedge | haas:spring2014:data:projects:nodes [2014/01/27 10:58] (current) – [Submission] wedge | ||
---|---|---|---|
Line 11: | Line 11: | ||
To review structs and pointers, and see how these two concepts, when combined, produces the core element of our class explorations. | To review structs and pointers, and see how these two concepts, when combined, produces the core element of our class explorations. | ||
+ | =====Reference===== | ||
+ | You absolutely, positively, MUST watch this video: http:// | ||
=====Structures===== | =====Structures===== | ||
As we learned in C, there are two main composite data types available to us: | As we learned in C, there are two main composite data types available to us: | ||
Line 195: | Line 197: | ||
At present, we have a node called first that we've allocated memory to and altered its contents, that diagram would look as follows: | At present, we have a node called first that we've allocated memory to and altered its contents, that diagram would look as follows: | ||
+ | {{ : | ||
+ | |||
+ | Notice how all the elements of the node are dealt with (both value and the next pointer). And first, being a mere pointer to a struct, is a name that points to (because it merely contains the address of) the memory region we **malloc()**' | ||
+ | |||
+ | Now,to link to another node (and put in, say, a 37 for its value) we'd do something along these lines: | ||
+ | |||
+ | <code c> | ||
+ | first -> next = (Node *) malloc (sizeof(Node)); | ||
+ | first -> next -> value = 37; | ||
+ | first -> next -> next = NULL; | ||
+ | </ | ||
+ | |||
+ | And don't forget to update your diagram: | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ====Use variables to make it easier to traverse the list==== | ||
+ | Since we need to keep a placeholder on our allocated memory, **first** is more or less immovable in the current context (it is our link to everything). | ||
+ | |||
+ | You may be noticing the potential for some very long code about to happen (what if we wanted to add a third nod... those next's would become next -> next, and so on). But there' | ||
+ | |||
+ | Here is that same node construction logic, redone using an additional **tmp** node pointer, and also adding in a third node (containing the value 8): | ||
+ | |||
+ | < | ||
+ | Node *first, *tmp = NULL; | ||
+ | |||
+ | first = (Node *) malloc (sizeof(Node)); | ||
+ | tmp = first; | ||
+ | |||
+ | tmp -> value = 12; | ||
+ | tmp -> next = NULL; | ||
+ | |||
+ | tmp -> next = (Node *) malloc (sizeof(Node)); | ||
+ | tmp = tmp -> next; | ||
+ | |||
+ | tmp -> value = 37; | ||
+ | tmp -> next = NULL; | ||
+ | |||
+ | tmp -> next = (Node *) malloc (sizeof(Node)); | ||
+ | tmp = tmp -> next; | ||
+ | |||
+ | tmp -> value = 8; | ||
+ | tmp -> next = NULL; | ||
+ | </ | ||
+ | |||
+ | Our node diagram now looks as follows (but ideally, you'd have been updating it line by line as this program went along-- do not wait "until the end" | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | =====Procedure===== | ||
+ | I would like for you to write a C program utilizing nodes to construct a simple list, joining new values onto the end of the last node. | ||
+ | |||
+ | The program should do the following: | ||
+ | |||
+ | * implement a node containing signed char's (for **value**) | ||
+ | * prompt the user to enter a value (-1 to exit) | ||
+ | * allocate a node (or the next node-- you'll have to deal with first vs. remaining case logic) | ||
+ | * obtain input from the user and assign it to the current node | ||
+ | * once a -1 has been provided, display the list from beginning to end and then exit | ||
+ | |||
+ | Sample output would look as follows: | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | Enter a value (-1 to quit): 12 | ||
+ | Enter a value (-1 to quit): 37 | ||
+ | Enter a value (-1 to quit): 8 | ||
+ | Enter a value (-1 to quit): 59 | ||
+ | Enter a value (-1 to quit): 86 | ||
+ | Enter a value (-1 to quit): 4 | ||
+ | Enter a value (-1 to quit): -1 | ||
+ | |||
+ | List: 12 -> 37 -> 8 -> 59 -> 86 -> 4 -> NULL | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | NOTE: This is just example input. Not only should your program work with this, but lists of any length, containing any arrangement of valid values. | ||
+ | |||
+ | =====Submission===== | ||
+ | To successfully complete this project, the following criteria must be met: | ||
+ | |||
+ | * Handwritten diagram fully detailed for an 8 node list uploaded to your Opus | ||
+ | * insert the image into your journal, and have related commentary on what is going on | ||
+ | * submit the URL (to that week's journal entry) along with your program | ||
+ | * Code must compile cleanly (no warnings or errors) | ||
+ | * Executed program must display in a manner similar to provided output. | ||
+ | * Output must be correct | ||
+ | * Code must be nicely and consistently indented (you may use the **indent** tool) | ||
+ | * Code must be commented | ||
+ | * have a properly filled-out comment banner at the top | ||
+ | * have at least 20% of your program consist of **< | ||
+ | * Track/ | ||
+ | * Submit a copy of your source code to me using the **submit** tool. | ||
+ | |||
+ | To submit this program to me using the **submit** tool, run the following command at your lab46 prompt: | ||
+ | |||
+ | <cli> | ||
+ | $ submit data nodes node.c http:// | ||
+ | Submitting data project " | ||
+ | -> node.c(OK) | ||
+ | -> http:// | ||
+ | |||
+ | SUCCESSFULLY SUBMITTED | ||
+ | </ | ||
+ | |||
+ | You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches. |