Worksheet: Structs
The objectives of this practical are to understand how to:
- design and use structs; and
- implement a linked list using structs.
Pre-lab Exercises
Part 1. Creating Structs
Declare (with typedef) suitable structs to hold the following information:
(a) A date.
(b) A set of coordinates in 3D space.
(c) A postal address.
(d) The details of a practical class at Curtin.
(e) The details of all practicals for a unit.
2. Dynamic Allocation and Deallocation
Write C code to dynamically allocate and then deallocate the following:
(a) The date struct from Question 1a.
(b) An array of 25 coordinate structs from Question 1b.
(c) A struct called Info that contains a char* field called name, intended to store names up to 99 characters.
(d) An array of 25 Info structs.
(e) The following struct, so that it keeps track of an array of 25 Info structs:
3. Expressions with Pointers, Arrays and Structs
The following expressions use a combination of pointers, arrays and structs. In each case, describe (i) what "var" is by itself and (ii) what the overall expression is accessing.
(a) var.b
(b) var->b
(c)
(d)
(e)
(f)
(g)
(h)
(i)
var[i].b
var[i]->b
var->b[i] var[i]->b[j]
*(var[i]->b) var->b->c var.b[i][j].c
4. Linked List Structs
Design a set of structs to hold a linked list, where:
- The values to be stored in the list are structs called FruitBat.
- The size of the list must be instantly accessible.
- The last element of the list must be instantly accessible.
How does the last point complicate the insertion of a new element?
5. Miscellaneous Questions
(a) When inserting an element at the start of a linked list, you must (a) point the new node's "next" field to the existing first node, and (b) point "head" to the new node. Why does the order of these operations matter?
(b) Can an element belong to more than one linked list simultaneously?
(c) Can an element appear more than once in the same linked list?
Practical Exercises
1. Files and Structs
Write a C program to read a file and store the contents in a dynamically-allocated array of structs.
The file contains journal entries, each consisting of a date and a text string. Each entry occupies two lines. The first line of each entry contains the date (in "day/month/year" format) and the second contains the text. The text can be up to 100 characters long, not including the new-line character.
The very first line of the file contains a single integer, indicating the number of journal entries in the file.
You should declare a suitable struct (in a header file), allocate an array just large enough to hold all the entries, and then read each entry into the array.
Part 2. Linked Lists
For this question please ensure that you download the linkedlistTest.c file from Blackboard and use that to test your Linked List
Write a set of C functions in a file called 'linkedlist.c' to manipulate a generic linked list.
The test harness is able to support the main kinds of list implementation aswell. Whether it be single or doubly ended or single or doubly linked (Just need to add a #define at the of the testing file )
You will need to create appropriate structs, and write functions to:
(a) Create an empty linked list.
(b) Insert an element at the start. This function should take (i) a pointer to an existing linked list, and (ii) a pointer to an existing journal entry struct, to be inserted at the start of the list.
(c) Remove an element from the start. This function should take a pointer to an existing linked list, remove the first element from the list and return a pointer to it. (Return NULL instead if the list is empty.)
(d) Insert an element at the end. This function should be similar to your insertStart function, however instead of working with the head of the list its now the tail.
(e) Removing an element from the end. This function should be similar to your removeStart function, however instead of working with the head of the list its now the tail.
(f) Print out the contents of the list.
(g) Free the list.
Part 3. Convert To Linked List
For this question you are to redo Question1 however with 2 differences:
- Remove the first line of the file that contains the number of entries
- Implement it using your LinkedList instead of an Array
Taking online assistance from the professionals of UNIX & C Programming Assignment Help service is the best way to get resolved from all your academic troubles and worries in no time.
Tags: UNIX & C Programming Assignment Help, UNIX & C Programming Homework Help, UNIX & C Programming Coursework, UNIX & C Programming Solved Assignments, Linked List Assignment Help, Linked List Homework Help
Attachment:- Worksheet-Unix and C Programming.rar