For this exercise, you will need to download the template
ex3.c, and enter your code in the
methods (functions) provided.
You are welcome to create your own methods if you like.
You will also need to download two additional
files - the test program ex3test.c
and the file uniform.c.
You may change
or add test code to ex3test.c if you like.
Both files need to be in the same directory as your ex2.c.
Before you write your first line of code, you ought to try compiling
the template as follows:
gcc -o ex3 ex3test.c ex3.c uniform.c -lm
This will create an executable called ex3 that you can
execute as follows:
./ex3
In this exercise you will get practice with linked lists and
pointers in C. You will implement a doubly-linked list and
manipulate it in certain ways. Let's look at some of the methods:
- The method initializelist() should, as the name
indicates, start a new list that is initially empty.
- The method clearList() should return all the memory
that was allocated. This will be called by test code when the list
is no longer needed. You will need to call the free() method
with each pointer to dispose of the memory allocated to that pointer.
- The method insertAtEnd (int data) should add a new item
at the end of the current list and place the given integer in that item.
Thus, the list is a linked list of integers.
- The method findPositionAndMove (int data, int
moveDistance) is the most interesting. Here, the data
is an integer that you will search the list to see where it occurs
in the list, i.e., the search depth. If the integer is not in the list you will return
-1, otherwise you will return the position, counting from
the front, where the data was found. Thus, if the data to search for
is 5 and 5 happens to be in the 9-th position
starting from the front, then you will return 9. If
the data is found at the front itself, return 1.
We'll next explain the moveDistance parameter.
Once you have found the data, you will move that item up towards
the front of the list. The idea is, if an item is frequently accessed,
it's better to have it closer up near the front of the list.
But since we have no way of knowing what's frequently accessed, we'll
simply move each accessed item some distance towards the front.
This way, over time, the frequently accessed items will tend to
cluster near the front of the list.
How much do we move an item up towards the front? If
moveDistance=1 that corresponds to moving the item up
by one, i.e., by swapping it with the item that's next to it
(in the direction towards the front).
Similarly, if moveDistance is large, the move will
probably move it right to the front of the list.
The goal here is to experiment with different values of
moveDistance.
The test code provided experiments with a few such distances.
To evaluate a particular strategy, we need to simulate many
lists and compute the average performance. The performance is
defined as the average search depth required over a large
number of accesses (say, 10,000 accesses).
Deliverables and submission:
- The source code for your program. Please write all your code in the
same file (ex3.c).
- A screenshot from using the test code in ex3test.c.
- For test 1, 2 and 3 in
ex3test.c,
include PDF drawings of the list at each stage of insertion and list adjustment.
These drawings (done by hand in PDF) should contain the actually memory
addresses of the list nodes as well as the contents of the list nodes.
You can use Word, PPT, or any way of drawing as long as the final
result is in PDF.
- For a list of 10 elements, report the average search depth,
using various values of moveDistance in the range of
1 to 10.
- Note: you will need to implement the move "honestly", which
means actually moving the list item itself (as opposed to just moving
the data).
- NOTE: Your code will be graded on both correctness and documentation (comments).
- As usual, you will need to follow
submission instructions carefully.