Problem Set 5 – Solutions Pointers. Arrays. Strings. Searching and sorting algorithms

10 380 0
Problem Set 5 – Solutions Pointers. Arrays. Strings. Searching and sorting algorithms

Đang tải... (xem toàn văn)

Thông tin tài liệu

Problem 5.1 In this problem, we continue our study of linked list. Let the nodes in the list have the following structure

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 struc t node { int data ; st ruc t 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 Answer: Here’s one possible implementation: #include<s t d i o . h> #include<s t d l i b . h> st ruc t node { int data ; st ruc t node ∗ next ; } ; /∗ @funct i o n n a l l o c @desc a l l o c a t e s a new node e le m e n t s @returns p o i n t e r to the new element on s u c c e s s , NULL on f a i l u r e @param data [ IN ] payl o ad o f the new element ∗/ st ruc t node ∗ n a l l o c ( int data ) { st ruc t node ∗ p=( struct node ∗) m all oc ( s i z e o f ( struct node ) ) ; i f ( p!=NULL) { p−>next=NULL; p −>data=data ; } return p ; } /∗ @funct i o n a dd fr o n t @desc adds node to the f r o n t o f the l i s t @param head [ IN ] c u r r e n t head o f t he l i s t @param data [ IN ] data to be i n s e r t e d @return updated head o f the l i s t ∗/ st ruc t node ∗ a d d f ro n t ( s tru ct node ∗ head , i n t data ) { st ruc t node ∗ p=n a l l o c ( data ) ; i f ( p==NULL) return head ; / ∗ no change ∗/ p −>next=head ; return p ; } /∗ @funct i o n d i s p l a y @desc d i s p l a y s the nodes in t h e l i s t @param head [ IN ] p o i n t e r to the head node of the l i s t ∗/ void d i s p l a y ( struct node ∗ head ) { st ruc t node ∗ p=NULL; p r i n t f ( " list :" ) ; for (p=head ; p!=NULL; p=p −>next ) p r i n t f ( "%d " , p−>data ) ; p r i n t f ( "\n" ) ; } /∗ @funct i o n addback 2 @desc adds node to the back o f t h e l i s t @param head [ IN ] c u r r e n t head o f t he l i s t @param data [ IN ] data to be i n s e r t e d @return updated head node ∗/ st ruc t node ∗ addback ( st ruc t node ∗ head , i n t data ) { st ruc t node ∗ p=n a l l o c ( data ) ; st ruc t node ∗ c u r r=NULL; i f ( p==NULL) return head ; /∗ s p e c i a l c a s e : empty l i s t ∗/ i f ( head==NULL) { head=p ; return p ; } e l s e { /∗ f i n d l a s t ele m e n t ∗/ for ( cu r r=head ; cu r r −>next !=NULL; c u r r=curr −>next ) ; curr −>next=p ; return head ; } } /∗ @funct i o n f r e e l i s t @desc f r e e s the element o f the l i s t @param head [ IN ] p o i n t e r to the head node ∗/ void f r e e l i s t ( str uct node ∗ head ) { st ruc t node ∗ p=NULL; while ( head ) { p=head ; head=head −>next ; f r e e ( p ) ; } } /∗ @funct i o n f i n d @desc f i n d s t h e ele m e n ts tha t c o n t a i n s the g i v e n data @param head [ IN ] p o i n t e r to the head node @param data [ IN ] payload t o match @return NULL i f not found , p o i n t e r to the el ement i f found ∗/ st ruc t node ∗ f i n d ( struc t node ∗ head , i n t data ) { st ruc t node ∗ c u r r=NULL; for ( cu r r=head ; cu r r −>next !=NULL; c u r r=curr −>next ) { i f ( c urr −>data==data ) return c u r r ; } return NULL; } 3 /∗ @funct i o n del n od e @desc d e l e t e s a node @param head [ IN ] p o i n t e r to the head node @param pnode [ IN ] p o i n t e r to t h e elem e n t t o be removed @return updated head node ∗/ st ruc t node ∗ del n od e ( str uct node ∗ head , s truct node ∗ pnode ) { st ruc t node ∗ p=NULL; st ruc t node ∗ q=NULL; for (p=head ; p!=NULL && p!= pnode ; p=p −>next ) q=p ; /∗ f o l l o w s p∗/ i f ( p==NULL) / ∗ not found ∗/ return head ; i f ( q==NULL) / ∗ head element ∗/ { head=head−>next ; f r e e ( p ) ; } e l s e { q−>next=p−>next ; /∗ s k i p p∗/ f r e e ( p ) ; } return head ; } /∗ @funct i o n main @desc t e s t s li n ke d − l i s t i m pleme n t ation ∗/ int main ( ) { /∗ t e s t a d df ro n t ∗/ st ruc t node ∗ head=NULL; /∗ head node ∗/ st ruc t node ∗ np=NULL; /∗ node p o i n t e r ∗/ puts ( " should display empty " ) ; d i s p l a y ( head ) ; / ∗ sh o uld p r i n t empty∗/ /∗ t e s t add f r o n t ∗/ head=a dd fr o n t ( head , 1 0 ) ; head=a dd fr o n t ( head , 2 0 ) ; puts ( " should display 20 ,10 " ) ; d i s p l a y ( head ) ; /∗ t e s t f r e e l i s t ∗/ f r e e l i s t ( head ) ; head=NULL; puts ( " should display empty " ) ; d i s p l a y ( head ) ; /∗ t e s t add back ∗/ head=addback ( head , 1 0 ) ; head=addback ( head , 2 0 ) ; head=addback ( head , 3 0 ) ; puts ( " should display 10 ,20 ,30 " ) ; d i s p l a y ( head ) ; /∗ t e s t f i n d ∗/ np=f i n d ( head , −20 ) ; 4 puts ( " should display empty " ) ; d i s p l a y ( np ) ; np=f i n d ( head , 2 0 ) ; puts ( " should display 20 ,30 " ) ; d i s p l a y ( np ) ; /∗ t e s t d e ln o de ∗/ head=del nod e ( head , np ) ; puts ( " should display 10 ,30 " ) ; d i s p l a y ( head ) ; np=f i n d ( head , 1 0 ) ; head=del nod e ( head , np ) ; puts ( " should display 30" ) ; d i s p l a y ( head ) ; /∗ c l e a n up ∗/ f r e e l i s t ( head ) ; return 0 ; } 5 Problem 5.2 In this problem, we continue our study of binary trees. Let the nodes in the tree have the following structure st ruc t tnode { int data ; st ruc t tnode ∗ l e f t ; st ruc t tnode ∗ r i g h t ; } ; Use the template in Lec06 (slides 41) to add elements to the list. (a) Write the function struct tnode∗ talloc(int data) that allocates a new node with the given data. (b) Complete the function addnode() by filling in the missing section. Insert elements 3, 1, 0, 2, 8, 6, 5, 9 in the same order. (c) Write function void preorder(struct tnode∗ root) to display the elements using pre-order traver­ sal. (d) Write function void inorder(struct tnode∗ root) to display the elements using in-order traversal. Note that the elements are sorted. (e) Write function int deltree (struct tnode∗ root) to delete all the elements of the tree. The function must return the number of nodes deleted. Make sure not to use any pointer after it has been freed. (Hint: use post-order traversal). (f) Write test code to illustrate the working of each of the above functions. All the code and sample outputs should be submitted. 6 Answer: Here’s one possible implementation: #include<s t d i o . h> #include<s t d l i b . h> st ruc t tnode { int data ; st ruc t tnode ∗ l e f t ; st ruc t tnode ∗ r i g h t ; } ; /∗ @funct i o n t a l l o c @desc a l l o c a t e s a new node @param data [ IN ] payload @return p o i n t e r to the new node o r NULL on f a i l u r e ∗/ st ruc t tnode ∗ t a l l o c ( i n t data ) { st ruc t tnode ∗ p=( st ruc t tnode ∗ ) m al loc ( s i z e o f ( str uct tnode ) ) ; i f ( p!=NULL) { p−>data=data ; p −>l e f t =p−>r i g h t=NULL; } return p ; } /∗ @funct i o n addnode @desc i n s e r t s node i n t o the t r e e @param data [ IN ] data to be i n s e r t e d @returns updated r o o t to the t r e e ∗/ st ruc t tnode ∗ addnode ( s tru ct tnode ∗ root , i n t data ) { i f ( r o o t==NULL) { st ruc t tnode ∗ node=t a l l o c ( data ) ; return ( r o o t=node ) ; } e l s e i f ( data<roo t −>data ) { root −>l e f t =addnode ( root −>l e f t , data ) ; } e l s e { root −>r i g h t=addnode ( root −>r i g h t , data ) ; } return ro o t ; } /∗ @funct i o n p r e o r d e r @desc p r i n t s el e me nt s i n pre −or d er @param r o o t [ IN ] p o i n t e r to the r o o t o f the t r e e @returns not h ing ∗/ 7 void pr e o r d e r ( struct tnode ∗ r o o t ) { i f ( r o o t==NULL) return ; p r i n t f ( "%d " , root −>data ) ; p r e o r d e r ( ro ot −> l e f t ) ; p r e o r d e r ( ro ot −>r i g h t ) ; } /∗ @funct i o n i n o r d e r @desc p r i n t s el e me nt s i n in −or d er @param r o o t [ IN ] p o i n t e r to the r o o t o f the t r e e @returns not h in g ∗/ void i n o r d e r ( str uct tnode ∗ ro o t ) { i f ( r o o t==NULL) return ; i n o r d e r ( root −>l e f t ) ; p r i n t f ( "%d " , root −>data ) ; i n o r d e r ( root −>r i g h t ) ; } /∗ @funct i o n d e l t r e e @desc d e l e t e nodes of the t r e e @param r o o t [ IN ] p o i n t e r to the r o o t o f the t r e e ∗/ int d e l t r e e ( s tru ct tnode ∗ r o o t ) { int count =0; i f ( r o o t==NULL) return ; count+=d e l t r e e ( ro o t −>l e f t ) ; count+=d e l t r e e ( ro o t −>r i g h t ) ; f r e e ( r o o t ) ; return ++count ; } /∗ @funct i o n main @desc t e s t s b in ar y t r e e f u n c t i o n s ∗/ int main ( ) { st ruc t tnode ∗ ro o t=NULL; int count =0; /∗ adding e le m e n ts ∗/ r o o t=addnode ( ro ot , 3 ) ; r o o t=addnode ( ro ot , 1 ) ; r o o t=addnode ( ro ot , 0 ) ; r o o t=addnode ( ro ot , 2 ) ; r o o t=addnode ( ro ot , 8 ) ; r o o t=addnode ( ro ot , 6 ) ; r o o t=addnode ( ro ot , 5 ) ; r o o t=addnode ( ro ot , 9 ) ; /∗ t e s t p r e o r d e r ∗/ puts ( " should print 3,1,0 ,2 ,8 ,6 ,5 ,9 " ) ; p r e o r d e r ( r o o t ) ; puts ( " " ) ; /∗ t e s t i n o r d e r ∗/ puts ( " should print 0,1,2 ,3 ,5 ,6 ,8 ,9 " ) ; 8 i n o r d e r ( r o o t ) ; pu ts ( "" ) ; /∗ t e s t d e l t r e e ∗/ count=d e l t r e e ( r o o t ) ; r o o t=NULL; puts ( " should expect 8 nodes deleted " ) ; p r i n t f ( "%d nodes deleted \ n" , count ) ; return 0 ; } 9 MIT OpenCourseWare http://ocw.mit.edu 6.087 Practical Programming in C January (IAP) 2010 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms . . Science 6.087: Practical Programming in C IAP 2010 Problem Set 5 – Solutions Pointers. Arrays. Strings. Searching and sorting algorithms.

Ngày đăng: 25/04/2013, 08:07

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan