Docsity

Review for Assignment 1 - Computer Programming II | CSC 220, Assignments of Computer Science

Toggle sidebar
bg1
Lecture 8
CSC220: Programming II (Data Structures) - Fall 2003
Dr. Christian A. Duncan
1 Announcements
•Quiz Thursday (Chapters 1-3)
•Homework #1 graded
•Homework #2 due tomorrow (Wednesday)
•Homework #3 is posted and due next Wednesday
2 Linked Lists
Until now, we have been focusing our data structure development using arrays. It is time to learn the next
generic building block, the linked list. Linked lists are as versatile to use as an array. They are a bit harder
to work with initially. But with practice they will become, maybe not your best friend in programming,
but a nice buddy to have around. I prefer a slightly different structure to the linked list class presented in
the book but to avoid any confusion we shall use the book’s presentation, as the key ideas are still there.
Remember there are always different ways to organize your structure. This could mean different names for
the methods (though some names are very popular) and different layouts to the class entirely. For example,
C is not even object-oriented yet you can still create linked lists using structures.
Alinked list is a sequence of elements connected to each other by “links”. You can imagine it as forming
a chain of elements. The general organization of a linked list is to group the element and link together into
one structure (or class) called a node. Since the book deals with integers, we shall first look at a linked list
of integers. In this case, you would define an IntNode which would look something like:
public class IntNode {
private int data;
private IntNode link;
// methods go here...
}
The biggest thing to notice here is that the link variable is of type IntNode, which is the current class
being defined! How is this possible? Well, Java doesn’t care too much because it is really only interested in
the fact that the link stores a reference to some other object. When you use it, it will then care what type
of object that really is.
Here is a very simple graphical depiction of the linked list structure.
data: ???
link
data: ???
link
data: ???
link
data: ???
link
head
null
That simple class along with many methods gives you a lot of power. There are several approaches to
creating a linked list. We shall now look at one. First, we need a reference to the start of our sequence. The
head of the sequence is usually identified by a variable called:
IntNode head
Remember head is not the actual node. It is a reference to the first node!
It is also often useful to know the end of the sequence. In this case, we have the IntNode tail.
Since every node has a link to another node, how do you identify the last node? What does it store a
link to? For a standard linked list, that link would be simply null, meaning it does not point to anything.
1
pf3
pf4

Partial preview of the text

Download Review for Assignment 1 - Computer Programming II | CSC 220 and more Assignments Computer Science in PDF only on Docsity!

Lecture 8

CSC220: Programming II (Data Structures) - Fall 2003 Dr. Christian A. Duncan [email protected]

1 Announcements

  • Quiz Thursday (Chapters 1-3)
  • Homework #1 graded
  • Homework #2 due tomorrow (Wednesday)
  • Homework #3 is posted and due next Wednesday

2 Linked Lists

Until now, we have been focusing our data structure development using arrays. It is time to learn the next generic building block, the linked list. Linked lists are as versatile to use as an array. They are a bit harder to work with initially. But with practice they will become, maybe not your best friend in programming, but a nice buddy to have around. I prefer a slightly different structure to the linked list class presented in the book but to avoid any confusion we shall use the book’s presentation, as the key ideas are still there. Remember there are always different ways to organize your structure. This could mean different names for the methods (though some names are very popular) and different layouts to the class entirely. For example, C is not even object-oriented yet you can still create linked lists using structures. A linked list is a sequence of elements connected to each other by “links”. You can imagine it as forming a chain of elements. The general organization of a linked list is to group the element and link together into one structure (or class) called a node. Since the book deals with integers, we shall first look at a linked list of integers. In this case, you would define an IntNode which would look something like:

public class IntNode { private int data; private IntNode link; // methods go here... }

The biggest thing to notice here is that the link variable is of type IntNode, which is the current class being defined! How is this possible? Well, Java doesn’t care too much because it is really only interested in the fact that the link stores a reference to some other object. When you use it, it will then care what type of object that really is. Here is a very simple graphical depiction of the linked list structure.

data: ??? link

data: ??? link

data: ??? link

data: ??? link

head

null

That simple class along with many methods gives you a lot of power. There are several approaches to creating a linked list. We shall now look at one. First, we need a reference to the start of our sequence. The head of the sequence is usually identified by a variable called: IntNode head Remember head is not the actual node. It is a reference to the first node! It is also often useful to know the end of the sequence. In this case, we have the IntNode tail. Since every node has a link to another node, how do you identify the last node? What does it store a link to? For a standard linked list, that link would be simply null, meaning it does not point to anything.

2.1 Null References

We have seen nulls before. But just to stress some uses remember the following:

  • When a reference instance variable is first declared (e.g. String text;) it is assigned a reference to something... null.
  • The end node of a linked list has link == null.
  • The empty list consists of a list with no values. It is stored by having head == null; tail == null;.
  • For those used to C++, null references are identified by null and NOT by NULL.

2.2 Reminder: NullPointer Exceptions

Just a friendly reminder as the book also points it out. What happens with the following bit of code?

String text; text = null; System.out.println(text.length());

Always be careful when dereferencing (a.k.a. accessing) a variable of value null. If you try to do that, a NullPointerException will be thrown and if not caught (as is most likely) the program will crash. Another warning, although I haven’t seen it with newer versions, one old java implementation actually core dumped when this happened. Java should always exit cleanly either by displaying the exception that was thrown or by ending normally at the end of the program. If it core dumps, there is a bug in the java implementation but it probably means you also have an error in your program as well. I don’t think this will happen for us but in case it does be aware of it.

3 Methods for Node Class

There are several different methods you should write for a node. Mainly the following standard ones:

  • public IntNode(init initialData, IntNode initialLink) To construct the instance of the node.
  • Accessor methods to access the variables: public int getData() and public IntNode getLink()
  • Mutator methods to change the variables: public void setData(int newData), public void setLink(IntNode newLink) Notice there is a tradeoff here between what is known as information hiding and speed. We make all variables private and then grant full access to these variables, if you notice there are no restrictions on any of the methods. Why make them private then? Standard object-oriented programming practice. Keeping protocol makes it easier for others to use. They do not have to check if variables are private or not, they always are! But, it certainly sacrifices some speed - a function call instead of a simple variable access. However, other languages like C++ do what are called inline functions and there would (most likely) be no distinction between either approach in which case having information hiding is certainly a better choice. As the asymptotic runtime is not greatly affected, we shall adhere to standard practice.

4 Manipulating the Linked List

Once the basics for a Node are established the next thing to do is actually manipulate the linked list so it will have some use. For notational sake, I will often use either head or h to refer to the head of a node and tail or t to refer to the tail. In particular, we shall cover (first graphically then via implementation details), how to:

In nearly every case, always think about the special cases, these are typically when one of the affected nodes is referred to by the head or the tail or when the list was or becomes empty. All of these situations should be thought of before coding the method. In particular, ask questions like, “Could this method cause the list to become empty? And if so, what must I do special?” Also, think about how long each algorithm takes and compare that with the array versions of the Bag and Sequence classes. How long does it take to add a node to the list? In the worst case? How long does it take to delete a node? In the worst case? How about getting the length of a list?