assignment5 - Massachusetts Institute of Technology...

Info icon This 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 ; struct node 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 find( 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 find). 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
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 < s t d i o . h > #include < s t d l i b . h > struct node { int data ; struct node next ; } ; / @function n a l l o c @desc a l l o c a t e s a new node elements @returns pointer to the new element on success , NULL on f a i l u r e @param data [ IN ] payload of the new element / struct node n a l l o c ( int data ) { struct node p=( struct node ) malloc ( sizeof ( struct node ) ) ; i f ( p!=NULL) { p > next=NULL; p > data=data ; } return p ; } / @function addfront @desc adds node to the front of the l i s t @param head [ IN ] current head of the l i s t @param data [ IN ] data to be i n s e r t e d @return
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern