assignment5

assignment5 - Massachusetts Institute of Technology...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.087: Practical Programming in C IAP 2010 Problem Set 5 Solutions Pointers. Arrays. Strings. Searching and sorting algorithms. Out: January 19, 2010. Due: January 20, 2010. Problem 5.1 In this problem, we continue our study of linked list. Let the nodes in the list have the following structure struct node { int data ; next ; } ; Use the template in Lec06 (slides 35,36) to add elements to the list. (a) Write the function void display( struct node head) that displays all the elements of the list. (b) Write the function struct node addback( struct node head, int data) that adds an element to the end of the list. The function should return the new head node to the list. (c) Write the function struct node fnd( struct node head, int data) that returns a pointer to the element in the list having the given data. The function should return NULL if the item does not exist. (d) Write the function struct node delnode( struct node head, struct node pelement) that deletes the element pointed to by pelement (obtained using Fnd). The function should return the up- dated head node. Make sure you consider the case when pelement points to the head node. (e) Write the function void Freelist ( struct node head) that deletes all the element of the list. Make sure you do not use any pointer after it is freed. (f) Write test code to illustrate the working of each of the above functions. All the code and sample outputs should be submitted. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Answer: Here’s one possible implementation: #include < stdio . h > < stdlib . h > struct node { int data ; next ; } ; / @function nalloc @desc a l l o c a t e s a new elements @returns pointer to the new element on success , NULL on f a i l u r e @param [ IN ] payload of new / ( ) { p=( ) malloc ( sizeof ( ) ) ; if ( p!=NULL) { p > next=NULL; p > data=data ; } return p ; } / addfront adds front l i s t @param head [ IN ] current l i s t @param [ IN ] be inserted @return updated l i s t / ( , ) { p=nalloc ( data ) ; ( p==NULL) head ; / no change / p > next=head ; p ; } / display displays nodes in l i s t
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 9

assignment5 - Massachusetts Institute of Technology...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online