In Class: Week 13
Create a week13 subdirectory in your cs21/class directory and
copy over some starting point files:
$ cd
$ cd cs21/class
$ pwd
/home/your_user_name/cs21/class
$ mkdir week13
$ cd week13
$ pwd
/home/your_user_name/cs21/class/week13
$ cp ~newhall/public/cs21/week13/* .
$ ls
linkedlist.py mainprog.py node.py
This week and next week we are going to cover linked lists.
We will talk about operations on linked lists and look at an implementation
of Node and LinkedList classes to which we will add functionality.
- We will start by looking at the Node class defined in node.py
Linked list nodes have a special data field, next, that refers to the
next node in the linked list.
- Next, we will look at the linked list class. A linked list object
has three data members: head points to the first Node in the list; tail
points to the last Node in the list; and size, is the number of values
(Nodes) in the list. Minimally, a linked list needs one data field,
head, to point to the head of the list, but often it has these other
two additional fields to make some operations faster
- We are going to talk about several operations on a linked list,
and implement some of the following missing LinkedList
method functions:
- insertAtTail(val): add a new node to the end of the linked list
- insertAtHead(val): add a new node at the front of the linked list
- findElement(val): find a matching element in the linked list, returns
a reference to the Node with a matching data field or None if no matching
value exists in the linked list
- __str__: complete this method that will create a string version of
the linked list. We will try calling it from main, by calling
print str(list)
- insertSorted(val): add a new node in sorted order into the linked list.
We will use insertion Sort to find the correct insertion spot for the
new element
- removeFromHead(): remove the first element from the linked list
and returns the value of its data field or None if the list is empty
- removeFromTail(): remove the last element from the linked list and
returns the value of its data field or None if the list is empty