SlideShare a Scribd company logo
1 of 10
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
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
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
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
/ ∗
@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
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
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
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
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
/ ∗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

More Related Content

Similar to C Programming Homework Help

Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfcontact32
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxtienlivick
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfsivakumar19831
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3ecomputernotes
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdfankitmobileshop235
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdfdeepua8
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdfanujmkt
 
public class SLLT { protected SLLNodeT head, tail; pub.pdf
public class SLLT {     protected SLLNodeT head, tail;     pub.pdfpublic class SLLT {     protected SLLNodeT head, tail;     pub.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdfclarityvision
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdfexcellentmobilesabc
 

Similar to C Programming Homework Help (20)

Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
 
C program
C programC program
C program
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Arrays
ArraysArrays
Arrays
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
137 Lab-2.2.pdf
137 Lab-2.2.pdf137 Lab-2.2.pdf
137 Lab-2.2.pdf
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
public class SLLT { protected SLLNodeT head, tail; pub.pdf
public class SLLT {     protected SLLNodeT head, tail;     pub.pdfpublic class SLLT {     protected SLLNodeT head, tail;     pub.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdf
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdf
 

More from Programming Homework Help

Design and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment HelpDesign and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment HelpProgramming Homework Help
 
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptxprogramminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptxProgramming Homework Help
 

More from Programming Homework Help (20)

Python Question - Python Assignment Help
Python Question - Python Assignment HelpPython Question - Python Assignment Help
Python Question - Python Assignment Help
 
Best Algorithms Assignment Help
Best Algorithms Assignment Help Best Algorithms Assignment Help
Best Algorithms Assignment Help
 
Design and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment HelpDesign and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment Help
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptxprogramminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
Algorithms Design Assignment Help
Algorithms Design Assignment HelpAlgorithms Design Assignment Help
Algorithms Design Assignment Help
 
Algorithms Design Homework Help
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework Help
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
Computer Science Assignment Help
Computer Science Assignment Help Computer Science Assignment Help
Computer Science Assignment Help
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
Software Construction Assignment Help
Software Construction Assignment HelpSoftware Construction Assignment Help
Software Construction Assignment Help
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
Programming Homework Help
Programming Homework Help Programming Homework Help
Programming Homework Help
 

Recently uploaded

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
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