User Tools

Site Tools


haas:spring2014:data:projects:nodes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
haas:spring2014:data:projects:nodes [2014/01/27 10:07] – [Pointers] wedgehaas: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://www.youtube.com/watch?v=5VnDaHBi8dM
 =====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:
  
 +{{ :haas:spring2014:data:projects:initalnode.jpg |initial node diagram}}
 +
 +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()**'ed and are storing our struct in.
 +
 +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;
 +</code>
 +
 +And don't forget to update your diagram:
 +
 +{{ :haas:spring2014:data:projects:nextnode.jpg |}}
 +
 +====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's a way to keep it simple (but ambiguous, at least without an updated diagram)... and that is to just use another variable, whose job is to be more of a temporary placeholder. We shall call it **tmp**.
 +
 +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):
 +
 +<code>
 +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;
 +</code>
 +
 +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".... build and update your diagram as changes are happening):
 +
 +{{ :haas:spring2014:data:projects:thirdnodeandtmp.jpg |}}
 +
 +=====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:~/src/data$ ./node
 +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:~/src/data$ 
 +</cli>
 +
 +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 **<nowiki>//</nowiki>**-style descriptive comments
 +  * Track/version the source code in a repository
 +  * 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://lab46.corning-cc.edu/opus/SEMESTER/user/start#week_2
 +Submitting data project "nodes":
 +    -> node.c(OK)
 +    -> http://lab46.corning-cc.edu/opus/SEMESTER/user/start#week_2(OK)
 +
 +SUCCESSFULLY SUBMITTED
 +</cli>
 +
 +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.
haas/spring2014/data/projects/nodes.1390817279.txt.gz · Last modified: 2014/01/27 10:07 by wedge