Linked List Demonstration

Dusty Phillips
Athabasca University
June 16, 2004
Last updated Jul 2, 2004

This applet visually demonstrates the use of Linked Lists in programming. View the applet in the Applet section below. The usage of the applet is described in the following Usage section. For those interested, the Display and design of the applet are also described.

Applet

Usage

Usage of the Linked List demonstration is quite simple. For visual purposes the list is limited to 36 items.

You can choose to Insert, Append, or Delete an item in the list. You can also manipulate the position of the current pointer using Next, and Previous.

Once you have provided the parameters for the method in question, you can manually or automatically step through the pseudocode using the step and autostep buttons. As you step through the code, a visual of the list will be displayed.

Linked Lists have the trait of being unlimited in size. However an artificial maximum is placed on this one to ensure all elements fit in the window.

Display

Standard by now, the display will be divided into three sections, one for input, one for visualization, and one for pseudocode. The pseudocode area will be divided into a class overview and a area to display the specific method being steppend through.

The visualization area will show the links of the list. If an item is being inserted or removed it will be elevated above the rest of the list. The head, tail, and current items will be displayed in red, green, and blue respectively, other items will be white.

Design

The design is straightforward. A ProgramPanel will be used to display the pseudocodes. Each method will have a separate stepper that manipulates a linked list, the head of which is tracked by a LinkedListPanel. Each link in the list will have drawing hints defining whether the link to the next item should be displayed or not, whether the item should be elevated above the list, whether the item is current, etc. This will be tracked by a custom Link class.

This is the main pseudocode: class LinkedList { int max = 36 Link head = new Link(null) Link tail = head Link current = head next(); previous(); insert(Object obj); append(Object obj); remove(); }

The pseudocode for the next() method (changed from Shaffer's code): void next() { if (curr != tail) curr = curr.next() }

The pseudocode for the previous() method (changed from Shaffer's code): void previous() { if (curr == head) return Link temp = head while (temp != null && temp.next() != curr) temp = temp.next() curr = temp }

The pseudocode for the insert() method: void insert(Object obj) { if (curr == null) throw NoCurrentException if (size() == max) throw ListFullException Link newLink = new Link(obj, curr.next()) curr.setNext(newLink) if (tail == curr) tail = newLink }

The pseudocode for the append() method: void append(Object obj) { if (size() == max) { throw ListFullException } Link newLink = new Link(obj, null) tail.setNext(newLink) tail = newLink }

The pseudocode for the remove() method: Object remove() { if (curr == null || curr.next() == null) throw NoCurrentException Object obj = curr.next().element() if (tail == curr.next()) tail = curr newNext = curr.next().next() curr.setNext(newNext) return obj }