Description
In this exercise we want to simulate the behavior of LISP’s list construction in C, where the
elements are limited to lists and atoms of “char” type only.
typedef enum { ATOM, LIST } eltype;
typedef char atom;
struct _listnode;
typedef struct {
eltype type;
union {
atom a;
struct _listnode* l;
};
} element;
typedef struct _listnode {
element el;
struct _listnode* next;
} * list;
const element NIL = { .type=LIST, .l=NULL };
Using the above definition, implement the following functions:
1. element aasel(atom a); AKA atom as element, returns an element whose content
is set to atom a.
2. element lasel(list l); AKA list as element, returns an element whose content is
set to the list, pointed by l.
3. list cons(element e, list l); that creates a new list whose car and cdr are the
element e and the list l. While the memory for the newly created list is to be allocated
dynamically.
3
4. list append(LIST l1, list l2); that creates a new list whose elements are shallow
copies of elements in l2 and l2, appended.
5. element car(element e); that returns head of the list, represented by e; returns
NIL, if e is not a list.
6. list cdr(element e); that returns tail of the list, represented by e.
7. list cddr(element e); that similarly returns the cddr of the list, represented by e.
8. void print(e); that prints the content of the element e. If e is an atom, it prints the
symbol enclosed in spaces, and if e it is a list, if prints recursively prints the elements
of the list enclosed in parentheses. If e is NIL, the word “NIL” must be printed.
9. void free(LIST l); that frees all the memory previously allocated by the whole list
(including all its elements and its inner lists)
Write a short code to create and display the following list:
Additionally, print the car and the cdr of the above list; also print the car of the car of the
original list.
The output must look like the following:
( a ( b c ) d e )
a
(( b c ) d e )
NIL
Make sure the list is freed before your program terminates.
4
4 What to submit
You must submit a zip file containing the following two files:
1. Your source code.
2. A README.txt file with all names that contributed to the assignment.
Name the zip file after your team e.g. team1.zip and submit it at the Electronic Assignment
Submission portal
(https://fis.encs.concordia.ca/eas)
under Programming Assignment 3.
END OF ASSIGNMENT
5