SlideShare a Scribd company logo
1 of 7
For Any Homework Related Queries,
Text/ WhatsApp Us At : - +1(315) 557-6473
You Can Mail Us At : - support@programminghomeworkhelp.com or
Reach Us At : - https://www.programminghomeworkhelp.com/c-assignment/
https://www.programminghomeworkhelp.com/c-assignment/
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).
https://www.programminghomeworkhelp.com/c-assignment/
The function should return the updated 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.
Solution
Answer: Here’s one possible implementation:
#include<stdio .h> #include<stdlib .h>
struct node
{ int data ; struct node∗ next ;
} ;
/∗ @function nalloc @desc allocates a new node elements @returns pointer to
the new element on success , NULL on failure @param data [IN] payload of the
new element
∗/
struct node∗ nalloc ( int data )
{ struct node∗ p=(struct node ∗) malloc ( sizeof ( struct node )); if
(p!=NULL)
{
p−>next=NULL;p−>data=data ;
}
https://www.programminghomeworkhelp.com/c-assignment/
return p;
}
/∗ @function addfront @desc adds node to the front of the list @param head
[IN] current head of the list @param data [IN] data to be inserted @return
updated head of the list
∗/
struct node∗ addfront ( struct node∗ head , int data )
{ struct node∗ p=nalloc (data ); if (p==NULL) return head; /∗no change ∗/
p−>next=head ; return p;
}
/∗ @function display @desc displays the nodes in the list @param head [IN]
pointer to the head node of the list
∗/
void display ( struct node∗ head)
{ struct node∗ p=NULL; printf ( "list:" ); for (p=head ;p!=NULL;p=p−>next )
printf ( "%d " ,p−>data );printf ( "n" );
}
/∗@function addback
@desc adds node to the back of the list@param head [IN] current head of the
list@param data [IN] data to be inserted@return updated head node
https://www.programminghomeworkhelp.com/c-assignment/
struct node∗ addback( struct node∗ head , int data )
{ struct node∗ p=nalloc (data ); struct node∗ curr=NULL; if (p==NULL) return head
;
/∗ special case: empty list ∗/
if ( head==NULL)
{ head=p ;return p;
}else
{ /∗ find last element ∗/for (curr=head; curr−>next!=NULL; curr=curr−>next )
;curr−>next=p ;return head ;
}}
/∗ @function freelist @desc frees the element of the list @param head [IN] pointer
to the head node
∗/
void freelist ( struct node∗ head)
{ struct node∗ p=NULL; while (head)
{ p=head ;head=head−>next ;free (p);
}}
/∗ @function find @desc finds the elements that contains the given data @param
head [IN] pointer to the head node @param data [IN] payload to match @return
NULL if not found , pointer to the element if found
∗/
https://www.cpphomeworkhelp.com/
struct node∗ find( struct node∗ head , int data )
{ struct node∗ curr=NULL; for (curr=head; curr−>next!=NULL; curr=curr−>next )
{
if ( curr−>data==data ) return curr ;
}return NULL;
}
/∗ @function delnode @desc deletes a node @param head [IN] pointer to the
head node @param pnode [IN] pointer to the element to be removed @return
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−>next )
q=p; /∗ follows p∗/if (p==NULL) /∗ not found ∗/return head ;if ( q==NULL)
/∗head element ∗/
{
head=head−>next ;free (p);
}else
{
q−>next=p−>next; /∗ skip p∗/free (p);
}return head ;
}/∗ @function main @desc tests linked−l i s t implementation
∗/
https://www.programminghomeworkhelp.com/c-assignment/
int main ( )
{ /∗ test addfront ∗/ struct node∗ head=NULL; /∗head node ∗/ struct node∗
np=NULL; /∗node pointer ∗/ puts ( "should display empty" ); display (head
); /∗ should print empty∗/
/∗ test add front ∗/
head=addfront(head ,10);head=addfront(head ,20);puts ( "should display
20,10" );display (head );
/∗ test free list ∗/
freelist (head);head=NULL;puts ( "should display empty" );display (head );
/∗ test add back ∗/
head=addback(head ,10);head=addback(head ,20);head=addback(head
,30);puts ( "should display 10,20,30" );display (head );
/∗ test find ∗/
np=find (head, −20);
puts ( "should display empty" ); display (np);
np=find (head ,20); puts ( "should display 20,30" ); display (np);
/∗ test delnode ∗/
head=delnode(head ,np); puts ( "should display 10,30" ); display (head );
np=find (head ,10); head=delnode(head ,np); puts ( "should display 30" );
display (head );
/∗ clean up∗/
freelist (head);return 0;
}

More Related Content

Similar to C Assignment Help

Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdfsudhirchourasia86
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfwasemanivytreenrco51
 
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
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfsales98
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracingSamsil Arefin
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docx10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docxtodd991
 
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docxhoney725342
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfajayadinathcomputers
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhvasavim9
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3ecomputernotes
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxMatthPYNashd
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfarchgeetsenterprises
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfhadpadrrajeshh
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 

Similar to C Assignment Help (20)

Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdf
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
137 Lab-2.2.pdf
137 Lab-2.2.pdf137 Lab-2.2.pdf
137 Lab-2.2.pdf
 
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
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docx10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docx
 
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
03.DS_Store__MACOSX03._.DS_Store03A2.DS_Store__.docx
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.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

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
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
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
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
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
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
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
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
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

C Assignment Help

  • 1. For Any Homework Related Queries, Text/ WhatsApp Us At : - +1(315) 557-6473 You Can Mail Us At : - support@programminghomeworkhelp.com or Reach Us At : - https://www.programminghomeworkhelp.com/c-assignment/
  • 2. https://www.programminghomeworkhelp.com/c-assignment/ 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).
  • 3. https://www.programminghomeworkhelp.com/c-assignment/ The function should return the updated 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. Solution Answer: Here’s one possible implementation: #include<stdio .h> #include<stdlib .h> struct node { int data ; struct node∗ next ; } ; /∗ @function nalloc @desc allocates a new node elements @returns pointer to the new element on success , NULL on failure @param data [IN] payload of the new element ∗/ struct node∗ nalloc ( int data ) { struct node∗ p=(struct node ∗) malloc ( sizeof ( struct node )); if (p!=NULL) { p−>next=NULL;p−>data=data ; }
  • 4. https://www.programminghomeworkhelp.com/c-assignment/ return p; } /∗ @function addfront @desc adds node to the front of the list @param head [IN] current head of the list @param data [IN] data to be inserted @return updated head of the list ∗/ struct node∗ addfront ( struct node∗ head , int data ) { struct node∗ p=nalloc (data ); if (p==NULL) return head; /∗no change ∗/ p−>next=head ; return p; } /∗ @function display @desc displays the nodes in the list @param head [IN] pointer to the head node of the list ∗/ void display ( struct node∗ head) { struct node∗ p=NULL; printf ( "list:" ); for (p=head ;p!=NULL;p=p−>next ) printf ( "%d " ,p−>data );printf ( "n" ); } /∗@function addback @desc adds node to the back of the list@param head [IN] current head of the list@param data [IN] data to be inserted@return updated head node
  • 5. https://www.programminghomeworkhelp.com/c-assignment/ struct node∗ addback( struct node∗ head , int data ) { struct node∗ p=nalloc (data ); struct node∗ curr=NULL; if (p==NULL) return head ; /∗ special case: empty list ∗/ if ( head==NULL) { head=p ;return p; }else { /∗ find last element ∗/for (curr=head; curr−>next!=NULL; curr=curr−>next ) ;curr−>next=p ;return head ; }} /∗ @function freelist @desc frees the element of the list @param head [IN] pointer to the head node ∗/ void freelist ( struct node∗ head) { struct node∗ p=NULL; while (head) { p=head ;head=head−>next ;free (p); }} /∗ @function find @desc finds the elements that contains the given data @param head [IN] pointer to the head node @param data [IN] payload to match @return NULL if not found , pointer to the element if found ∗/
  • 6. https://www.cpphomeworkhelp.com/ struct node∗ find( struct node∗ head , int data ) { struct node∗ curr=NULL; for (curr=head; curr−>next!=NULL; curr=curr−>next ) { if ( curr−>data==data ) return curr ; }return NULL; } /∗ @function delnode @desc deletes a node @param head [IN] pointer to the head node @param pnode [IN] pointer to the element to be removed @return 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−>next ) q=p; /∗ follows p∗/if (p==NULL) /∗ not found ∗/return head ;if ( q==NULL) /∗head element ∗/ { head=head−>next ;free (p); }else { q−>next=p−>next; /∗ skip p∗/free (p); }return head ; }/∗ @function main @desc tests linked−l i s t implementation ∗/
  • 7. https://www.programminghomeworkhelp.com/c-assignment/ int main ( ) { /∗ test addfront ∗/ struct node∗ head=NULL; /∗head node ∗/ struct node∗ np=NULL; /∗node pointer ∗/ puts ( "should display empty" ); display (head ); /∗ should print empty∗/ /∗ test add front ∗/ head=addfront(head ,10);head=addfront(head ,20);puts ( "should display 20,10" );display (head ); /∗ test free list ∗/ freelist (head);head=NULL;puts ( "should display empty" );display (head ); /∗ test add back ∗/ head=addback(head ,10);head=addback(head ,20);head=addback(head ,30);puts ( "should display 10,20,30" );display (head ); /∗ test find ∗/ np=find (head, −20); puts ( "should display empty" ); display (np); np=find (head ,20); puts ( "should display 20,30" ); display (np); /∗ test delnode ∗/ head=delnode(head ,np); puts ( "should display 10,30" ); display (head ); np=find (head ,10); head=delnode(head ,np); puts ( "should display 30" ); display (head ); /∗ clean up∗/ freelist (head);return 0; }