DoubleLinkList
DoubleLinkList
CODE
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
btnaddH.addActionListener(this);
btnaddT.addActionListener(this);
btnremoveH.addActionListener(this);
btnremoveT.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(500,500);
setVisible(true);
}
num.requestFocusInWindow();
link.displayThis();
listModel.clear();
listModel.addElement(link.output);
}
public Doublenode(int d) {
this(null,d,null);
}
}
}
class listOperation {
Doublenode head;
Doublenode 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 Doublenode(item);
} else {
head = head.previous = new Doublenode(null,item,head);
}
}
public void addTail(int item) {
if (isEmpty()) {
head = tail = new Doublenode(item);
} else {
tail = tail.next = new Doublenode(tail,item,null);
//tail.next = new node(item);
//tail = tail.next;
}
}
public void deleteHead() {
Doublenode tempHead;
if (!isEmpty()) {
if (head == tail) {
head = tail = null;
} else {
tempHead = head.next;
head.next = tempHead.previous = null;
head = tempHead;
}
}
}
public void deleteTail() {
Doublenode tmp;
if (!isEmpty()) {
if (head == tail) {
head = tail = null;
} else {
tmp = tail.previous;
tail.previous = tmp.next = null;//cut the connection
tail = tmp;
}
}
}
public void displayThis() {
Doublenode tmp = head;
output = "<html>";
OUTPUT
ADD HEAD
ADD TAIL
REMOVE HEAD
REMOVE TAIL