I am Susan C. I am a C Homework Help Expert at programminghomeworkhelp.com. I hold a PhD. in Programming, Leeds University UK. I have been helping students with their homework for the past 12 years. I solve assignments related to C Programming.
Visit programminghomeworkhelp.com or email support@programminghomeworkhelp.com.You can also call on +1 678 648 4277 for any assistance with C Programming assignments.
Z Score,T Score, Percential Rank and Box Plot Graph
C Programming Homework Help
1. For any help regarding C Programming Assignment Help
Visit :- https://www.programminghomeworkhelp.com/ ,
Email :- support@programminghomeworkhelp.com or
call us at :- +1 678 648 4277
programminghomeworkhelp.com
2. 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.
programminghomeworkhelp.com
C Programming Assignment Help
Problem 1
3. Answer: Here’s one possible implementation:
#include< s t d i o . h>
#include< s t d l i b . h>
struct node
{
int d a t a ;
struct node ∗ n ext ;
};
/∗
@fu n ct ion
@desc
@r et u r n s
n a l l o c
a l l o c a t e s a new node elem en t s
po i n t e r to the new element on success , NULL on f a i l u r e
@param d a t a [ IN ] payload of the new element
∗/
struct node ∗ n a l l o c ( int d a t a )
{
struct node ∗ p=( struct node ∗) malloc ( sizeof ( struct node ) ) ;
i f ( p!= NULL)
{
p−>n e xt=NULL; p−>d a t a= d a t a ;
}
return p ;
}
/∗
a d d f r o n t
adds node
head [ IN ]
d a t a [ IN ]
t o t h e f r o n t o f t h e
c u r r e n t head of the d a t a
t o be i n s e r t e d
@fu n ct ion
@desc
@param
@param
@r et u r n
l i s t
l i s t
updated head of the l i s t
∗/
struct node ∗ a d d f r o n t ( struct node ∗ head , int d a t a )
{
struct node ∗ p= n a l l o c ( d a t a ) ;
i f ( p==NULL) return head ; /∗ no change ∗/ p−>ne xt=head ;
return p ;
}
/∗
@fu n ct ion
@desc
@param
d i s p l a y
d i s p l a y s t h e n od es in t h e l i s t
head [ IN ] po 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 )
{
struct node ∗ p=NULL; p r i n t f (
" l i s t : " ) ;
for ( p= head ; p!= NULL; p= p−>n e xt ) p r i n t f (
" %d " , p−>d a t a ) ;
p r i n t f ( " n" ) ;
}
/∗
@fu n ct ion addba ck
programminghomeworkhelp.com
4. adds node
head [ IN ]
d a t a [ IN ]
to the back of the l i s t c u r r e n t
head of the l i s t d a t a t o be i n s
e r t e d
@desc
@param
@param
@r et u r n updated head node
∗/
struct node ∗ addback ( struct node ∗ head , int d a t a )
{
struct node ∗ p= n a l l o c ( d a t a ) ;
struct node ∗ cu r r=NULL;
i f ( p==NULL) return head ;
/ ∗s p e c i a l ca se : empt y l i s t ∗/
i f ( head==NULL)
{
head= p ;
return p ;
}
else
{
/ ∗f i n d l a s t elem en t ∗/
for ( curr=head ; curr −>ne xt!=NULL; c u r r = c u r r −>ne xt )
;
cu r r −>n e xt= p ;
return head ;
}
}
/∗
@fu n ct ion
@desc
@param
f r e e l i s t
f r e e s t h e elem en t o f t h e l i s t
head [ IN ] po i n t e r to the head node
∗/
void f r e e l i s t ( struct node ∗ head )
{
struct node ∗ p=NULL;
while ( head )
{
p=head ; head= head−>n e xt ;
f r e e ( p ) ;
}
}
/∗
f i n d
f i n d s t h e
head [ IN ]
d a t a [ IN ]
elements t h a t c o n t a i n s the given p o i n t e r
t o t h e head node
payload to match
@fu n ct ion
@desc
@param
@param
@r et u r n
d a t a
NULL i f n ot found , p o i n t e r t o t h e elem en t i f fou n d
∗/
struct node ∗ f i nd ( struct node ∗ head , int d a t a )
{
struct node ∗ cu r r=NULL;
for ( curr=head ; curr −>ne xt!=NULL; c u r r = c u r r −>ne xt )
{
i f ( cu r r −>d a t a== d a t a ) return cu r r ;
}
return NULL;
}
programminghomeworkhelp.com
5. / ∗
@fu n ct ion
@desc
@param
@param
@r et u r n
delnode
d e l e t e s a node
head [ IN ] po i n t e r to the head node
pnode [ IN ] p o i n t e r t o t h e elem en t t o be rem oved updated
head node
∗/
struct node ∗ delnode ( struct node ∗ head , struct node ∗ pnode )
{
struct node ∗ p=NULL;
struct node ∗ q=NULL;
for ( p= head ; p!= NULL && p!= pnode ; p= p−>n e xt ) q= p ; / ∗f o l l o w s p ∗/
i f ( p==NULL) / ∗n ot fou n d ∗/
return head ;
i f ( q==NULL) / ∗head elem en t ∗/
{
head= head−>n e xt ; f r e e ( p ) ;
}
else
{
q−>n e xt= p−>n e xt ; / ∗sk ip p ∗/ f r e e ( p ) ;
}
return head ;
}
/ ∗ @fu n ct ion main
@desc t e s t s l inked − l i s t i m p l e m e n t a t i o n
∗/
int main ( )
{
/ ∗t e s t a d d fr on t ∗/
struct node ∗head=NULL; /∗ head node ∗/ struct node ∗ np=NULL; / ∗node p o i n t e r ∗/
p u t s ( " s houl d di s pl a y e mpt y " ) ;
d i s p l a y ( head ) ; / ∗sh ou ld p r i n t empt y ∗/
/ ∗t e s t add f r o n t ∗/ head= a d d fr on t ( head , 1 0 ) ; head= a d d fr on t ( head , 2 0
) ;
p u t s ( " s houl d di s pl a y 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;
p u t s ( " s houl d di s pl a y e mpt y " ) ; 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 ) ;
p u t s ( " s houl d di s pl a y 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 ) ;
programminghomeworkhelp.com
6. p u t s ( " s houl d di s pl ay empt y " ) ; d i s p l a y ( np ) ;
np= f i n d ( head , 2 0 ) ;
p u t s ( " s houl d di s pl ay 20 , 30 " ) ; d i s p l a y ( np ) ;
/ ∗t e s t d eln od e ∗/ head= d eln od e ( head , np ) ;
p u t s ( " s houl d di s pl ay 10 , 30 " ) ; d i s p l a y ( head ) ;
np= f i n d ( head , 1 0 ) ; head= d eln od e ( head , np ) ;
p u t s ( " s houl d di s pl ay 30 " ) ; d i s p l a y ( head ) ;
/ ∗c l ea n up∗/
f r e e l i s t ( head ) ;
return 0 ;
}
programminghomeworkhelp.com
7. Problem 2
In this problem, we continue our study of binary trees. Let the nodes in the tree have the following
structure
struct tnode
{
int data ;
struct tnode ∗ l e f t ;
struct 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.
programminghomeworkhelp.com
8. Answer: Here’s one possible implementation:
#include< s t d i o . h>
#include< s t d l i b . h>
struct tnode
{
int d a t a ;
struct t n od e ∗ l e f t ;
struct t n od e ∗ r i g h t ;
};
/∗
@fu n ct ion
@desc
@param
@r et u r n
t a l l o c
a l l o c a t e s a new node d a t a [ IN ]
p a yloa d
po i n t e r to the new node or NULL on f a i l u r e
∗/
struct t n od e ∗ t a l l o c ( int d a t a )
{
struct tnode ∗ p=( struct tnode ∗) malloc ( sizeof ( struct tnode ) ) ;
i f ( p!= NULL)
{
p−>data=data ;
p−> l e f t = p−> r i g h t =NULL;
}
return p ;
}
/∗
@fu n ct ion
@desc
@param
@r et u r n s
addnode
i n s e r t s node i n t o t h e t r e e
d a t a [ IN ] d a t a t o be i n s e r t e d
u p d a t ed r oot t o t h e t r e e
∗/
struct tnode ∗ addnode ( struct tnode ∗ root , int d a t a )
{
i f ( r oot==NULL)
{
struct t n od e ∗ node= t a l l o c ( d a t a ) ;
return ( r oot= node ) ;
}
else
{
i f ( d at a<r oot −>d a t a )
r oot −> l e f t = addnode ( r oot −> l e f t , d a t a ) ;
}
else
{
r oot −> r i g h t = addn ode ( r oot −>r igh t , d a t a ) ;
}
return root ;
}
/∗
p r e o r d e r
p r i n t s elem en t s in p re−or d er
root [ IN ] po i n t e r to the root of the t r e e
@fu n ct ion
@desc
@param
@r et u r n s
nothin g
∗/
programminghomeworkhelp.com
9. void p r eor d er ( struct t node ∗ r oot )
{
i f ( r oot==NULL) return ;
p r i n t f ( " %d " , r oot −>dat a ) ;
p r eor d er ( r oot −> l e f t ) ;
p r eor d er ( r oot −>r i g h t ) ;
}
/∗
i n o r d e r
p r i n t s elem en t s in in−or d er
root [ IN ] po i nt e r to the root of the t r e e
@fu n ct ion
@desc
@param
@r et u r n s
nothing
∗/
void i n o r d e r ( struct t node ∗ r oot )
{
i f ( r oot==NULL) return ; i n o
r d e r ( r oot −> l e f t ) ;
p r i n t f ( " %d " , r oot −>dat a ) ; i n o
r d e r ( r oot −>r i g h t ) ;
}
/∗
@fu n ct ion
@desc
@param
d e l t r e e
d e l e t e nodes of t h e t r e e
r oot [ IN ] p o i n t e r t o t h e r oot
of t h e t r e e
∗/
int d e l t r e e ( struct t node ∗ r oot )
{
int count = 0;
i f ( r oot==NULL) return ;
count+= d e l t r e e ( r oot −> l e f t ) ; count+= d e l t r e e ( r oot −>r i g h t ) ; f r e e ( r oot ) ;
return ++count ;
}
/∗
@fu n ct ion main
@desc t e s t s b in a r y t r e e f u n c t i o n s
∗/
int main ( )
{
struct t node ∗ r oot=NULL;
int count = 0;
/ ∗add ing elem en t s ∗/
r oot= addnode ( r oot , 3 ) ; r oot= addnode ( r oot , 1 ) ; r oot= addnode ( root , 0
) ; r oot= addnode ( r oot , 2 ) ; r oot= addnode ( r oot , 8 ) ; r oot= addnode ( root
, 6 ) ; r oot= addnode ( r oot , 5 ) ; r oot= addnode ( r oot , 9 ) ;
programminghomeworkhelp.com
10. / ∗t e s t p r eor d er ∗/
p u t s ( " s houl d pr i nt 3 , 1 , 0 , 2 , 8 , 6 , 5 , 9 " ) ; p r eor d er ( r oot ) ; p u t s ( " " ) ;
/ ∗t e s t i n o r d e r ∗/
p u t s ( " s houl d pr i nt 0 , 1 , 2 , 3 , 5 , 6 , 8 , 9 " ) ;
i n o r d e r ( r oot ) ; p u t s ( " " ) ;
/ ∗t e s t d e l t r e e ∗/
count= d e l t r e e ( r oot ) ; r oot=NULL;
p u t s ( " s houl d expect 8 nodes del et ed " ) ; p r i n t f ( " %d nodes del et ed n" , count ) ; return 0 ;
}
programminghomeworkhelp.com