Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

C Programming MIT Problem SET5 - Solutions

Rating
-
Sold
-
Pages
10
Grade
A
Uploaded on
19-10-2023
Written in
2023/2024

"C Programming MIT Problem Set 5 - Solutions" is a valuable educational resource designed for individuals interested in mastering the intricacies of the C programming language, particularly from a problem-solving perspective. Developed by the Massachusetts Institute of Technology (MIT), this problem set offers a series of challenging exercises and tasks aimed at enhancing one's understanding of C programming. Whether you are a student looking to strengthen your programming skills or an enthusiast interested in solving complex problems, this resource provides an excellent platform for honing your C programming abilities. By engaging with these MIT problem sets, you can sharpen your problem-solving skills, gain a deeper understanding of C, and broaden your horizons in the world of computer science.

Show more Read less
Institution
Course

Content preview

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
struct node
{
i n t 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.




1

, Answer: Here’s one possible implementation:
#include<s t d i o . h>
#include<s t d l i b . h>

struct node
{
i n t data ;
struct node ∗ next ;
};

/∗
@function n a l l o c
@desc a l l o c a t e s a new node e l e m e n t s
@ r e t u r n s p o i n t e r t o t h e new e l e m e n t on s u c c e s s , NULL on f a i l u r e
@param data [ IN ] pa y l o a d o f t h e new e l e m e n t
∗/
struct node ∗ n a l l o c ( i n t data )
{
struct node ∗ p=( struct node ∗ ) m a l l o c ( s i z e o f ( struct node ) ) ;
i f ( p!=NULL)
{
p−>next=NULL;
p−>data=data ;
}
return p ;
}

/∗
@function addfront
@desc adds node t o t h e f r o n t o f t h e l i s t
@param head [ IN ] c u r r e n t head o f t h e l i s t
@param data [ IN ] data t o be i n s e r t e d
@return updated head o f t h e l i s t
∗/
struct node ∗ a d d f r o n t ( struct node ∗ head , i n t data )
{
struct node ∗ p=n a l l o c ( data ) ;
i f ( p==NULL) return head ; /∗ no change ∗/
p−>next=head ;
return p ;
}

/∗
@function display
@desc d i s p l a y s t h e nodes i n t h e l i s t
@param head [ IN ] p o i n t e r t o t h e head node o f t h e 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 ( " list :" ) ;
f o r ( p=head ; p!=NULL; p=p−>next )
p r i n t f ( "%d " , p−>data ) ;
p r i n t f ( "\n" ) ;
}

/∗
@ f u n c t i o n addback



2

Written for

Course

Document information

Uploaded on
October 19, 2023
Number of pages
10
Written in
2023/2024
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$49.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
shubhjaiswal

Get to know the seller

Seller avatar
shubhjaiswal Teachme2-tutor
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
48
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions