linkedlists - Linked Lists Day 1, Day 2 Background Before...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Linked Lists Day 1 , Day 2 Background Before we begin our discussion of linked lists, let us review how variables, references, and objects work in Java. For example, suppose we have the following series of statements: ChessBoard chessboard; chessboard = new ChessBoard(8); The first statement is a reference variable declaration. It creates a variable of type ChessBoard , which will be capable of storing (more accurately, "referring to") an object of type ChessBoard . Because we haven't assigned any value to chessboard , it initially refers to null . This situation is shown below: The next statement then creates a new ChessBoard object, and sets the chessboard to refer to (or "point to") the newly created ChessBoard object, as below: In all honesty, the above picture is not completely accurate: the ChessBoard object actually contains a two dimensional array of char , which is itself an object. That is, there should be a reference coming out of our ChessBoard object (I don't draw that object, however): ListNodes Bearing the above in mind, we define a linked list: a sequence of elements containing data arranged one after the other, such that each element is connected by a link. The elements in the list are commonly referred to as a node . The node contains data and a link (reference to) the next node: Chaining a whole bunch of these together forms our linked list:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The list contains three items. Therefore, the node which contains a points to the next node in the sequence (b), which points to the final node in the sequence (c). Because there are no items following c, there is no next reference; instead this node's reference will point to null . We use a ListNode data type to define the nodes in our linked list. A ListNode then must contain to data members: the piece of data the node is keeping track of, and the next ListNode . In Java, the ListNode class is typically constructed with its data members having package wide access. That is, the data members will only be accessible to other classes within the same package. To specify the package access modifier, we simply declare our members with no access modifier. Therefore, we would likely define the class as follows: class ListNode { Object data; ListNode next; public ListNode(Object d, ListNode n) { data = d; next = n; } } The Linked List Data structure We are now ready to define the LinkedList data structure. It should be pointed out here that there are many variations on linked lists and the operations that can be performed on them. This is but one of several. We need to define the operations we will be able to use on a data structure. We have the four basic methods we want to be able to perform on any structure: add, remove, get, and contains: add(x) Input: the element to be added to our list Postcondition: the element has been added to the front of the list remove(x) Input: the item to be removed front the list Returns: true if the list contains x, false otherwise Postcondition: Removes the first occurrence of x from the list
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/17/2011 for the course CS 367 taught by Professor Marvinsolomon during the Spring '08 term at Wisconsin.

Page1 / 10

linkedlists - Linked Lists Day 1, Day 2 Background Before...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online