Activity: Single Linked List
Name : Joaquim Muhongo
CODE
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
public class Linkdemo1 extends JFrame implements ActionListener
{
private JTextField num = new JTextField(50);
private JLabel label = new JLabel("Item: ");
private JButton btnaddH = new JButton("Add Head");
private JButton btnaddT = new JButton("Add Tail");
private JButton btnremoveH = new JButton("Remove Head");
private JButton btnremoveT = new JButton("Remove Tail");
private JList list;
private DefaultListModel listModel;
ListSelectionModel listmodel;
listOperation link = new listOperation();
public Linkdemo1(String title)
{
super(title);
setLayout(new BorderLayout());
setLayout(new FlowLayout());
// Just for refresh :) Not optional!
setSize(399,399);
setSize(300,300);
listModel = new DefaultListModel();
list = new JList(listModel);
JLabel headline = new JLabel("Single Linked List");
headline.setFont(new Font("Century Gothic", Font.BOLD,20));
headline.setForeground(new Color(32, 50, 57));
label.setFont(new Font("Century Gothic", Font.BOLD,15));
label.setForeground(new Color(32, 50, 57));
//SETBOUNDS
headline.setBounds(170,10,200,30);
label.setBounds(50,50,50,30);
num.setBounds(110,50,320,30);
btnaddH.setBounds(280,110,150,30);
btnaddT.setBounds(280,150,150,30);
btnremoveH.setBounds(280,190,150,30);
btnremoveT.setBounds(280,230,150,30);
btnaddH.addActionListener(this);
btnaddT.addActionListener(this);
btnremoveH.addActionListener(this);
btnremoveT.addActionListener(this);
JScrollPane listScrollPane = new JScrollPane(list);
listScrollPane.setBounds(50,90,200,350);
listScrollPane.setPreferredSize(new Dimension(200,100));
//ADD
add(headline);
add(label);
add(num);
add(listScrollPane);
add(btnaddH);
add(btnaddT);
add(btnremoveH);
add(btnremoveT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(500,500);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
if(btnaddH == event.getSource()) {
link.addHead(Integer.parseInt(num.getText()));
num.setText("");
} else if (btnaddT == event.getSource()) {
link.addTail(Integer.parseInt(num.getText()));
num.setText("");
} else if(btnremoveH == event.getSource()) {
link.deleteHead();
} else if(btnremoveT == event.getSource()) {
link.deleteTail();
}
num.requestFocusInWindow();
link.displayThis();
listModel.clear();
listModel.addElement(link.output);
}
public static void main(String[] args) {
Container list = new Linkdemo1("Singled Linked List");
}
}
public class node
{
public int data;
public node next;
public node(int d)
{
this(d,null);
}
public node(int d,node n)
{
data = d;
next = n;
}
}
class listOperation {
node head;
node tail;
String output = "";
public listOperation() {
head = null;
tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void addHead(int item) {
if (isEmpty()) {
head = tail = new node(item);
} else {
head = new node(item,head);
}
}
public void addTail(int item) {
if (isEmpty()) {
head = tail = new node(item);
} else {
tail.next = new node(item);
tail = tail.next;
}
}
public void deleteHead() {
node tempHead;
if (!isEmpty()) {
if (head == tail) {
head = tail = null;
} else {
tempHead = head.next;
head.next = null;
head = tempHead;
}
}
}
public void deleteTail() {
node tmp;
if (!isEmpty()) {
if (head == tail) {
head = tail = null;
} else {
for (tmp = head; tmp != null; tmp = tmp.next) {
if (tmp.next == tail) {
tail = tmp;
tmp.next = null;
}
}
}
}
}
public void displayThis() {
node tmp = head;
output = "<html>";
for (tmp = head; tmp != null; tmp = tmp.next) {
output = output + "<br>" + tmp.data + "<br>";
}
output = output + "<html>";
}
}
OUTPUT
ADD HEAD
REMOVE TAIL
REMOVE HEAD