 |
CoSc2030: Computer Science II
Fall Semester 2007
Instructor: Dr. Thomas Bailey
|
Lab 06: Lists
The Linked List data type is one of several general
ways to organize sequential data and is used frequently in many different
branches of computer science. In this lab we will work with one
implementation of the Linked List data type, specifically the one presented in class. Remember that this structure stores one
pointer to the front of the list. Download the following
files for today's lab node.h, LinkedListOne.cpp,
LinkedListOne.h, OneDriver.cpp
Lab Assignment
- The given list implementation stores its data as doubles. Your
task will be to write a double findSum()
method inside of the LinkedList
class that finds the sum of data stored in the list. In order to perform this task you are going to need to use some form of
iteration to traverse the list. (Note: for this first
problem you should use an iterative scheme and not
recursion.) For hints on how to write this function look at the
method print in the LinkedList
class. NOTE: be sure to test the find_sum method
as well as all of the other methods that you write for this lab thoroughly.
- For your second task we are going to have you create another method in
the LinkedList class.
This method will be responsible for inserting the given parameter into
the second position in the list. The signature for this method
will be void insertAsSecond(double x).
(Note: Again use an iterative approach as
opposed to recursion) There are three special cases you should consider when writing this method.
- An empty list - Should correctly insert the element in the
front of the list.
- A one element list - Should correctly insert the element as
the last element in the list.
- An n element list - Should correctly insert the element as the
second element in the list.
- For part three you will be creating a new LinkedList
class called HTLinkedList.
The list is named HT for head and tail. This list
maintains two pointers, one to the head (front) of the list and one to
the tail (back) of the list. Create new files and use the
provided LinkedList class as a
starting point for building your new class. In other words
all of the methods should work properly for the HTLinkedList
that worked for the LinkedList.
Use the Node class provided for the LinkedList
implementation.
- Your final task is to implement an insertAsLast method for the
HTLinkedList. The signature for this method will be void
insertAsLast(double x). The method should properly handle
the empty case as well as the other cases. Consider the
difficulty of inserting at the end of the LinkedList
implementation versus the HTLinkedList
implementation. Which one is faster? How much faster?
Why? Place your answers to these questions inside the header file
for the HTLinkedList.
Notes:
You are beginning your exploration of pointers and the strengths and
weaknesses associated with them. The code should be without
runtime errors and memory leaks.
Use the write methods provided for you to test your code. If you
are unsure of your code be sure to ask you lab instructor for help. To aid you in
determining problems with your implementations we are going to have a
overview of the Microsoft Visual C++ debugger. Here is the code we
are going to debug.
Turn In:
You should have 4 files to upload. Write your name in comments at the top of each file. Make sure your files are named
Lab6LinkedList.cpp, Lab6LinkedList.h, Lab6HTLinkedList.cpp, and
Lab6HTLinkedList.h .
As always labs are due 1 week past their assigned date.
<< CoSc2030 Home
Copyright © 2005 Thomas Bailey