Skip to content

Instantly share code, notes, and snippets.

@mmandersheid
Created March 11, 2010 14:38
Show Gist options
  • Save mmandersheid/329168 to your computer and use it in GitHub Desktop.
Save mmandersheid/329168 to your computer and use it in GitHub Desktop.
Simple LinkedList java prog
import java.awt.*;
public class AList
{
private Node head;
public AList()
{
head = new Node ("<fake>", -99, null);
}
public void add(String n, int s)
{
head.addInOrder(n, s);
}
public void remove(String s)
{
head.remove(s);
}
public void paint(Graphics g)
{
g.setFont(new Font("Serif", Font.BOLD, 14));
head.paint(g, 20, 25);
}
public int getHowMany()
{
return head.getHowMany();
}
public int greatestAge()
{
return head.greatestAge();
}
public double getAverage()
{
return (head.getTotal()/head.getHowMany());
}
}
import java.awt.*;
public class ListCanvas extends Canvas
{
private AList list;
public ListCanvas(AList al)
{
list = al;
}
public void paint(Graphics g)
{
list.paint(g);
}
}
import java.awt.*;
import java.awt.event.*;
public class MyPanel extends Panel implements ActionListener
{
private AList list;
private ListCanvas canvas;
private StatsCanvas sCanvas;
private TextField nameField, ageField;
private Label nameLabel, ageLabel;
private Button addButton, removeButton;
public MyPanel()
{
list = new AList();
setLayout(null);
setBackground(new Color(200, 225, 255));
canvas = new ListCanvas(list);
canvas.setSize(200, 280);
canvas.setLocation(10, 10);
canvas.setBackground(Color.white);
add(canvas);
sCanvas = new StatsCanvas(list);
sCanvas.setSize(125, 125);
sCanvas.setLocation(250, 165);
sCanvas.setBackground(Color.white);
add(sCanvas);
nameField = new TextField();
nameField.setSize(80,20);
nameField.setLocation(310, 20);
nameField.setBackground(Color.white);
add(nameField);
nameLabel = new Label("Name: ");
nameLabel.setSize(80, 20);
nameLabel.setLocation(215, 20);
nameLabel.setAlignment(Label.RIGHT);
add(nameLabel);
ageField = new TextField();
ageField.setSize(80,20);
ageField.setLocation(310, 50);
ageField.setBackground(Color.white);
add(ageField);
ageLabel = new Label ("Age: ");
ageLabel.setSize(80,20);
ageLabel.setLocation(215, 50);
ageLabel.setAlignment(Label.RIGHT);
add(ageLabel);
addButton = new Button("Add: ");
addButton.setSize(125,20);
addButton.setLocation(250, 100);
add(addButton);
removeButton = new Button("Remove: ");
removeButton.setSize(125,20);
removeButton.setLocation(250, 130);
add(removeButton);
addButton.addActionListener(this);
removeButton.addActionListener(this);
ageField.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == addButton || source == ageField)
{
list.add(nameField.getText(), convert(ageField.getText()));
}
else if (source == removeButton)
{
list.remove(nameField.getText());
}
canvas.repaint();
sCanvas.repaint();
}
public int convert(String s)
{
s = s.trim();
try
{
return (new Integer(s)).intValue();
}
catch (NumberFormatException e)
{
return -9876;
}
}
}
import java.awt.*;
public class Node
{
private int age;
public static int count;
private Node link;
private String name;
private double total;
public Node (String a, int s, Node n)
{
age = s;
link = n;
name = a;
count = 0;
total = 0;
}
public void paint(Graphics g,int x, int y)
{
if (!name.equals("<fake>"))
{
g.drawString(name, x, y);
BL.drawStringLeft(g, age + "", x+80, y);
}
if (link != null)
{
link.paint(g, x, y+20);
}
}
public void addInOrder (String n, int s)
{
if (link == null || n.toUpperCase().compareTo(link.name.toUpperCase())<0) //create new node
{
link = new Node (n, s, link);
}
else
link.addInOrder(n, s);
}
public void add(String s, int a)
{
if(link!=null)
{
link.add(s,a);
}
else
{
link = new Node(s,a,null);
}
}
public void remove (String s)
{
if (link == null)
return;
if (s.equalsIgnoreCase(link.name))
link = link.link;
else
link.remove(s);
}
public int getHowMany()
{
int howMany = 1;
if (age < 0)
howMany = 0;
if (link != null)
howMany += link.getHowMany();
return howMany;
}
public int greatestAge()
{
count++;
if (link == null)
return age;
int after = link.greatestAge();
if (age > after)
return age;
return after;
}
public double getTotal()
{
total = age;
if (link == null)
total = age + 99;
else
total += link.getTotal();
return total;
}
public String toString()
{
String s= age + "";
if (link != null)
{
s += " " + link.toString();
}
return s;
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class PanelApplet extends Applet
{
Panel panel;
public void init()
{
setLayout(null);
panel = new MyPanel();
add(panel);
panel.setSize(getSize().width, getSize().height);
panel.setLocation(0,0);
panel.setVisible(true);
panel.requestFocus();
if (panel instanceof KeyListener)
addKeyListener((KeyListener)panel);
}
}
import java.awt.*;
public class StatsCanvas extends Canvas
{
private AList list;
public StatsCanvas(AList al)
{
list = al;
}
public void paint(Graphics g)
{
int count = list.getHowMany();
g.drawString("count: " + count, 20, 20);
int greatest = list.greatestAge();
double average = list.getAverage();
if (greatest != -99)
{
g.drawString("Greatest Age: " + greatest, 20, 50);
g.drawString("Average Age: " + average, 20, 75);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment