1. Linked Lists

If you want an ordered sequence of elements and the ability to rapidly insert and remove elements in the middle of the sequence, then you need a linked list. In a linked list a list header contains a pointer to the first link, which stores a data item and a pointer to the next link. That link, likewise, stores a data item and a pointer to the next link, and so on. The last link has a null pointer to denote the end of the list.

Lists are easier to program in Java than in just about any other programming language because every object referencein Java is already a pointer. This makes a basic Link class particularly simple:

class Link {
  Object data; 
  Link   next; // pointer to another object of the class
} 
In addition we need a List class. It holds a pointer to the head link, and (in order to make rapid insertion at the tail end of the list easier) we include a pointer to the tail as well. We also keep a field for the list size, so we won't need to traverse the entire list every time the caller wants to have an element count.

Furthermore, there must be some way of inserting in the middle of the list. We do this by storing a cursor with the list object. The cursor marks a specific element in the linked list. The reset method resets the cursor to the beginning of the list. The nextElement method advances it to the next element. This permits positioning of the cursor anywhere in the linked list. The remove method removes the element under the cursor, and the put method inserts a new object before the cursor. (The cursor functions just like the cursor on a terminal. Characters are inserted before the cursor and deleted from under the cursor).

The advantage of using a cursor is that the individual links are completely hidden from the list user, so the list user can't mess them up.

The program.

The code for the homework:

Script started on Tue Nov 10 17:24:39 1998
tucotuco.cs.indiana.edu% tucotuco.cs.indiana.edu% cd httpd tucotuco.cs.indiana.edu% cd htdocs tucotuco.cs.indiana.edu% cd 1110/linked tucotuco.cs.indiana.edu% ls -l total 28 -rw-r--r-- 1 dgerman students 399 Nov 10 16:58 Link.class -rw-r--r-- 1 dgerman students 1635 Nov 10 16:58 LinkedList.class -rw------- 1 dgerman students 2067 Nov 10 16:51 LinkedList.java -rw-r--r-- 1 dgerman students 647 Nov 10 16:58 ListEnumeration.class -rw-r--r-- 1 dgerman students 2526 Nov 10 16:58 ListTest.class -rw------- 1 dgerman students 2055 Nov 10 16:57 ListTest.java -rw-r--r-- 1 dgerman students 150 Nov 10 11:04 list.html tucotuco.cs.indiana.edu% cat LinkedList.java
public class LinkedList { private Link head; private Link tail; private Link pre; int len; public void reset() { pre = null; } public boolean hasMoreElements() { return cursor() != null; } public Object nextElement() { if (pre == null) { pre = head; } else pre = pre.next; if (pre.next == null) pre = pre.next; if (pre == null && head == null) throw new java.util.NoSuchElementException(); else if (head != null) return head.data; else return pre.data; } public Object currentElement () { Link cur = cursor(); if (cur == null) throw new java.util.NoSuchElementException(); return cur.data; } public void insert (Object n) { Link p = new Link(n, cursor()); if (pre != null) { pre.next = p; if (pre == tail) tail = p; } else { if (head == null) tail = p; head = p; } pre = p; len ++; } public void append(Object n) { // // // this is your homework 7: // implement this method: append // // } public Object remove() { Link cur = cursor(); if (cur == null) throw new java.util.NoSuchElementException(); if (tail == cur) tail = pre; if (pre != null) pre.next = cur.next; else head = cur.next; len--; return cur.data; } int size() { return len; } public java.util.Enumeration elements() { return new ListEnumeration(head); } private Link cursor() { if (pre == null) return head; else return pre.next; } } class Link { Object data; Link next; Link (Object d, Link n) { data = d; next = n; } } class ListEnumeration implements java.util.Enumeration { private Link cursor; public ListEnumeration (Link l) { cursor = l; } public boolean hasMoreElements() { return cursor != null; } public Object nextElement() { if (cursor == null) throw new java.util.NoSuchElementException(); Object r = cursor.data; cursor = cursor.next; return r; } }
tucotuco.cs.indiana.edu% cat ListTest.java
import java.applet.*; import java.util.*; import java.awt.*; public class ListTest extends Applet { private LinkedList a = new LinkedList(); private int m = 1; public void init() { this.add(new Button("Insert")); this.add(new Button("Append")); this.add(new Button("Remove")); this.add(new Button("Reset")); this.add(new Button("Next")); a.append(new Integer(m++)); a.append(new Integer(m++)); a.append(new Integer(m++)); // a.reset(); // a.nextElement(); // a.insert(new Integer(m++)); } public boolean handleEvent (Event evt) { return super.handleEvent(evt); } public boolean action (Event evt, Object arg) { if (arg.equals("Insert")) { a.insert(new Integer(m)); m++; repaint(); } else if (arg.equals("Append")) { a.append(new Integer(m)); m++; repaint(); } else if (arg.equals("Remove")) { a.remove(); repaint(); } else if (arg.equals("Next")) { a.nextElement(); repaint(); } else if (arg.equals("Reset")) { a.reset(); repaint(); } else return super.action(evt, arg); return true; } public void paint (Graphics g) { Enumeration e = a.elements(); int x = 0; int y = 50; int cx = 0; Rectangle r = this.bounds(); g.setColor(this.getBackground()); g.fillRect(r.x, r.y, r.width, r.height); g.setColor(Color.black); while (e.hasMoreElements()) { g.drawRect(x, y, 30, 20); Integer i = (Integer)e.nextElement(); if (a.hasMoreElements() && i.equals(a.currentElement())) cx = x; g.drawString(i.toString(), x + 3, y + 16); g.drawLine(x + 30, y + 10, x + 45, y + 10); g.drawLine(x + 45, y + 10, x + 40, y + 5); g.drawLine(x + 45, y + 10, x + 40, y + 15); x += 45; } if (!a.hasMoreElements()) cx = x - 45; g.drawLine(cx + 15, 25 + y, cx + 15, 40 + y); g.drawLine(cx + 15, 25 + y, cx + 10, 30 + y); g.drawLine(cx + 15, 25 + y, cx + 20, 30 + y); } }
tucotuco.cs.indiana.edu% cat list.html
<html> <head><title>Linked List Test</title></head> <body bgcolor=white> <applet code=ListTest.class width=300 height=200></applet> </body> </html>
tucotuco.cs.indiana.edu% exit script done on Tue Nov 10 17:25:36 1998
2. Threads

The program

The code:

Script started on Tue Nov 10 17:39:38 1998
tucotuco.cs.indiana.edu% cd httpd/htdocs/1110/threads
tucotuco.cs.indiana.edu% ls -l
total 26
-rw-r--r--   1 dgerman  students    1529 Nov 10 13:25 Ball.class
-rw-r--r--   1 dgerman  students    1134 Nov 10 13:25 Bounce.class
-rw-------   1 dgerman  students    1914 Nov 10 13:24 Bounce.java
-rw-r--r--   1 dgerman  students     186 Nov 10 13:13 show.html
tucotuco.cs.indiana.edu% cat Bounce.java

import java.applet.*; import java.awt.*; public class Bounce extends Applet { private int m = 1; public void init() { this.add(new Button("Regular")); this.add(new Button("Express")); } public boolean handleEvent (Event evt) { return super.handleEvent(evt); } public boolean action (Event evt, Object arg) { if (arg.equals("Regular")) { Ball b = new Ball(this, Color.black, m++); b.setPriority(Thread.NORM_PRIORITY); b.start(); } else if (arg.equals("Express")) { Ball b = new Ball(this, Color.red, m++); b.setPriority(Thread.NORM_PRIORITY + 2); b.start(); } else return super.action(evt, arg); return true; } } class Ball extends Thread { Bounce box; Color color; private int id; Graphics g; private static final int XSIZE = 10; private static final int YSIZE = 10; private int x = 0; private int y = 0; private int dx = 2; private int dy = 2; public Ball (Bounce c, Color co, int m) { box = c; color = co; id = m; g = box.getGraphics(); draw(); } public void draw () { g.setColor(color); g.setXORMode(box.getBackground()); g.fillOval(x, y, XSIZE, YSIZE); } public void move () { g.setColor(color); g.setXORMode(box.getBackground()); g.fillOval(x, y, XSIZE, YSIZE); // x += 3; y += 3; x += dx; y += dy; Rectangle r = box.bounds(); // g.drawRect(r.x, r.y, r.width-2, r.height-2); if (x < 0) { x = 0; dx = -dx; } if (y < 0) { y = 0; dy = -dy; } if (x + XSIZE >= r.width) { x = r.width - XSIZE; dx = -dx; } if (y + YSIZE >= r.height) { y = r.height - YSIZE; dy = -dy; } // g.setXORMode(box.getBackground()); g.fillOval(x, y, XSIZE, YSIZE); } public void run () { while (1 == 1) { try { sleep (100); move(); } catch (InterruptedException e) { } } } }
tucotuco.cs.indiana.edu% cat show.html
<html> <head><title>Threads</title></head> <body bgcolor=white><table border> <tr> <td> <applet code=Bounce.class width=300 height=200></applet> </td> </tr> </table> </body> </html>
tucotuco.cs.indiana.edu% exit script done on Tue Nov 10 17:40:17 1998