CS-211 Fall 2015 Lab 8 Data Structures

Background

In the last couple of lectures, we covered the idea of structures, structures which contain fields that point to other instances of the same structure, and dynamic allocation. Once example that uses all three concepts is a linked list; a data structure which consists of a list of nodes, where every node contains a payload (one or more values) and a pointer to a next node. The entire list starts with a pointer to the first node, called the "head" of the list. The first node's next pointer then points to the second node, whose next pointer points to the third node, and so on, until you get to a node whose next pointer is zero or NULL. This last node is the tail of the list.

We can use dynamic allocation (malloc) to create a new instance of a node, and that node can be inserted anywhere in the list by manipulating the "next" pointers.

For this lab, we will be creating a linked list. The payload part of each node in that list will consist of two integers - the first is the "value" of the node, just some arbitrary number; and the second is a "count", which counts the number of occurences of the value. We are also going to impose a new rule on our linked list... namely that the value in any node in the list will be less than the value in the next node in the list. In other words, the first node in the list will have the lowest value, the second will be greater than the first, the third greater than the second, and so on, until you get to the tail which has the greatest value of all.

Our linked list will be created from an arbitrary, unsorted list of integers read in from standard input.

After we have created our list, we will print out the values and counts for each node in that list.

Getting Started

Download the code that has been started for you in lab8.tar.gz. Use the command tar -xvzf lab8.tar.gz. This will unpack the tar file and make a new lab8 subdirectory. The lab8 directory will have a Makefile, and the C code to start from in file llist.c

The llist.c File

The llist.c file starts of with the standard includes for standard library functions, a definition of the lnode structure, and prototypes for the five lower level functions in the file.

What follows is just the normal function definitions that we are used to seeing. I've included a description of each of these functions below.

Using The Makefile

The Makefile file in the lab8 directory contains "targets" which allow you to do several things using the make command. The following targets are supported:

Inserting a Node into an Ordered Linked List

If you thought about it for awhile, you could probably figure out exactly how to insert a node into an ordered linked list without any help, but in order to save time, here is a description of exactly how to insert a node. In this description, we will assume that you have invoked insertValue, passing in the val value to insert, and head points to the ordered linked list that we want to insert the value into.

Checking for Memory Leaks

After you have written and tested your implentation of the insertValue function, test to see if your llist command has any memory leaks. To do this, we will use the valgrind command.

To run valgrind, execute the following command:
  valgrind --leak-check=full ./llist <input.txt

If you have implemented insertValue correctly, you should get no memory leaks, and see the message at the end of the valgrind invocation that includes:

 All heap blocks were freed -- no leaks are possible

Lab Report

Download and edit the following file: lab8_report.txt. Then submit your editted file on Blackboard in the Lab_08 report area.