Activity: Stack Linked List
Name : Joaquim Muhongo
CODE
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class StackLinkDemo extends JFrame implements ActionListener {
private JTextField num = new JTextField(50);
private JLabel label = new JLabel("Item: ");
private JButton push = new JButton("Push");
private JButton undo = new JButton("Undo");
private JButton redo = new JButton("Redo");
private JList list;
private DefaultListModel listModel;
ListSelectionModel listmodel;
//object
listOperation stack = new listOperation();
listOperation stackundo = new listOperation();
public StackLinkDemo(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");
[Link](new Font("Century Gothic", [Link],20));
[Link](new Color(32, 50, 57));
[Link](new Font("Century Gothic", [Link],15));
[Link](new Color(32, 50, 57));
//SETBOUNDS
[Link](170,10,200,30);
[Link](50,50,50,30);
[Link](110,50,320,30);
[Link](280,110,150,30);
[Link](280,150,150,30);
[Link](280,190,150,30);
[Link](this);
[Link](this);
[Link](this);
JScrollPane listScrollPane = new JScrollPane(list);
[Link](50,90,200,350);
[Link](new Dimension(200,100));
//ADD
add(headline);
add(label);
add(num);
add(listScrollPane);
add(push);
add(undo);
add(redo);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(500,500);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if(push == [Link]()) {
[Link]([Link]([Link]()));
[Link]("");
} else if (undo == [Link]()) {
if([Link] == null) {
return;
} else {
[Link]([Link]);
[Link]();
}
} else if(redo == [Link]()) {
if([Link] == null) {
return;
} else {
[Link]([Link]);
[Link]();
}
}
[Link]();
[Link]();
[Link]();
[Link]([Link]);
}
public static void main(String[] args) {
Container list = new StackLinkDemo("Stack Linked List");
}
}
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 = [Link] = new Doublenode(null,item,head);
}
}
public void addTail(int item) {
if (isEmpty()) {
head = tail = new Doublenode(item);
} else {
tail = [Link] = new Doublenode(tail,item,null);
//[Link] = new node(item);
//tail = [Link];
}
}
public void deleteHead() {
Doublenode tempHead;
if (!isEmpty()) {
if (head == tail) {
head = tail = null;
} else {
tempHead = [Link];
[Link] = [Link] = null;
head = tempHead;
}
}
}
public void deleteTail() {
Doublenode tmp;
if (!isEmpty()) {
if (head == tail) {
head = tail = null;
} else {
tmp = [Link];
[Link] = [Link] = null;//cut the connection
tail = tmp;
}
}
}
public void displayThis() {
Doublenode tmp = head;
output = "<html>";
for (tmp = head; tmp != null; tmp = [Link]) {
output = output + "<br>" + [Link] + "<br>";
}
output = output + "<html>";
}
}
public class Doublenode {
public int data;
public Doublenode next;
public Doublenode previous;
public Doublenode(int d) {
this(null,d,null);
}
public Doublenode(Doublenode p,int d,Doublenode n) {
previous = p;
data = d;
next = n;
}
}
OUTPUT
PUSH
UNDO
REDO