Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Assignment 4

Task 1: Declare a structure with specified members.


Declare a structure with the following attributes:

Tag: part
Member 1: name (a character array of 20 elements)
Member 2: id (an integer)
Member 3: cost (an 8 byte floating point number)
Member 4: num_available (an integer)

NOTE 1: This declaration should be done in 'tasks.h' not in ‘tasks.c’!!

NOTE 2: When you make a new project with given skeleton code, it WILL NOT compile! You have
to declare this structure in tasks.h for it to compile successfully!

Task 2: Initialize a structure with specified data.


For this task you have to complete a function load_data(). This function takes as arguments
the following:

 A character string 'name' (if the input string is larger, the member 'name' should be set
as an empty string).
 An integer 'id'
 A floating point number 'cost'
 An integer 'num'

Your function should return a structure variable (of type struct part) initialized with this data.
The function prototype is given below:

struct part load_data(char * name, int id, double cost, int num);

Task 3: Compare two structures


For this task you have to complete a function compare_structures() that takes pointers to
the two structures to be compared. It should return true, if the two structure variables contain
exactly the same data, false otherwise.

The function has the following prototype:

bool compare_structures(struct part * p1, struct part * p2);

Task 4: Find the addresses of the nth element in a linked list.


Write a function address_nth_list() that determines the address assigned to the nth
element in a linked list. The two inputs to the function are the pointer to the start of the list and
the index n.

The function should return the address of the nth element (a pointer to the nth element) or NULL
if the list contains less than n elements.

It has the following prototype:

struct node * address_nth_list(struct node * ptr, int n);

The structure used for creating the list (in this task and Task 5) is declared as follows:

struct node
{
int data;
struct node * next;
};

Task 5: Read data of the nth element in a linked list.


Write a function read_nth_list(), that takes as arguments a pointer to the start of a linked
list and returns (by reference) the nth element in the list. It should return 0 if the nth element is
successfully found. It should return -1 if the list contains less than n elements.

The prototype of the function is given below

int read_nth_list(struct node * ptr, int n, int * data);


Task 6: Search for a record in a linked list
Write a function search_record() that searches for a record with a particular ID in a linked
list and returns a pointer to the record with the correct ID. The inputs to the function are a pointer
to the head of the list and an ID. The function should return a pointer to the desired record or
NULL if the record with the given ID is not found in the list.

The function prototype is given below.

struct list_node * search_record(struct list_node * ptr, int ID);

The structure used for creating the list in this task, is declared as follows:

struct list_node
{
struct part data;
struct list_node * next;
};

The data part of this structure is the structure that you have already seen in tasks 1 to 3.

You might also like