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