0% found this document useful (0 votes)
3 views6 pages

DoubleLinkList

The document contains Java code for a graphical user interface (GUI) application that implements a double linked list. It allows users to add and remove elements from the head or tail of the list and displays the current list in a JList component. The application uses event listeners to handle button actions and updates the displayed list accordingly.

Uploaded by

wijilad205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

DoubleLinkList

The document contains Java code for a graphical user interface (GUI) application that implements a double linked list. It allows users to add and remove elements from the head or tail of the list and displays the current list in a JList component. The application uses event listeners to handle button actions and updates the displayed list accordingly.

Uploaded by

wijilad205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Activity: Double 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 DoubleLinkDemo 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;
//object
listOperation link = new listOperation();

public DoubleLinkDemo(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 DoubleLinkDemo("Double Linked List");
}
}

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;

}
}
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>";

for (tmp = head; tmp != null; tmp = tmp.next) {


output = output + "<br>" + tmp.data + "<br>";
}
output = output + "<html>";
}
}

OUTPUT
ADD HEAD

ADD TAIL

REMOVE HEAD
REMOVE TAIL

You might also like