Results 1 to 4 of 4
Thread: I need help in LinkedList ...
- 03-24-2011, 08:15 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
I need help in LinkedList ...
Hello my bros and my sisters .. I am need in this forum .. I hope u guys can help me .. What I need is , I am facing difficulties to relate Class CircularList with ClientNode ...
what I guess is that in class ClientNode , the Button actions has to be be in CircularList by using LinkedList ... and I have four classes in this project , I hope u guys can be able to finish as soon as possible , and I need to implement an Iterator for the linked list.
These are the classes ..
PHP Code:public interface ListInterface { public boolean isEmpty(); public void clear(); public void display(); public void add(Object n, Object a, Object i); public void remove(Object o); public void search(Object s); }PHP Code:public class Node { Object id; Object name; Object author; Node next; Node() { setNode(null,null,null,null); } Node(Object name, Object author, Object id, Node next) { setNode(name,author,id,next); } void setNode(Object name, Object author, Object id, Node next) { this.name=name; this.author=author; this.id=id; this.next=next; } public void setNext(Node next) { this.next=next; } public Node getNext() { return next; } public void setName(Object name) { this.name=name; } Object getName() { return name; } public void setAuthor(Object author) { this.author=author; } Object getAuthor() { return author; } public void setId(Node id) { this.id=id; } Object getId() { return id; } }PHP Code:import javax.swing.JOptionPane; public class CircularList implements ListInterface { private Node firstNode; private Node lastNode; private Node head; int no_of_nodes; public CircularList() { firstNode = null; lastNode = null; no_of_nodes = 0; } public boolean isEmpty() { if (firstNode == null) return true; else return false; } public void clear() { firstNode = null; lastNode = null; head = firstNode; no_of_nodes = 0; JOptionPane.showMessageDialog(null, "The list have been deleted"); } public void display() { head = firstNode; String out = ""; if (isEmpty()) out = "List is empty"; else { lastNode.setNext(null); while (head != null) { out += head.getName() + ", " + head.getAuthor() + "-" + head.getId() + "\n"; head = head.next; } lastNode.setNext(firstNode); } JOptionPane.showMessageDialog(null, out + ""); } public void add(Object n, Object a, Object i) { head = firstNode; Node newNode = new Node(n, a, i, null); if (isEmpty()) { firstNode = newNode; lastNode = firstNode; } else { if (no_of_nodes == 1) { firstNode.next = newNode; lastNode = newNode; } else { lastNode.setNext(null); while (head.next != null) { head = head.next; } head.next = newNode; lastNode = newNode; lastNode.setNext(firstNode); } } no_of_nodes++; } public void remove(Object o) { if (isEmpty()) JOptionPane.showMessageDialog(null, "Can't delete, the list is empty!!"); else if (no_of_nodes == 1 && (o + "").equalsIgnoreCase(firstNode.getName() + "")) { clear(); JOptionPane.showMessageDialog(null, "The list is null"); } else { lastNode.setNext(null); head = firstNode; Node prev = new Node(); while (head != null) { if ((o + "").equalsIgnoreCase(firstNode.getName() + "")) { firstNode = firstNode.next; JOptionPane.showMessageDialog(null, "Data " + o + " have been deleted"); no_of_nodes--; break; } if ((o + "").equalsIgnoreCase(head.getName() + "")) { if (head == lastNode) { lastNode = prev; } else { head = head.next; if (prev == null) prev = head; else prev.next = head; } JOptionPane.showMessageDialog(null, "Data " + o + " have been deleted"); no_of_nodes--; break; } else { prev = head; head = head.next; if (head == null) { JOptionPane.showMessageDialog(null, "data not found"); head = prev; break; } } } lastNode.setNext(firstNode); } } public void search(Object s) { if (isEmpty()) JOptionPane.showMessageDialog(null, "The list is empty!!"); else { lastNode.setNext(null); head = firstNode; Node prev = new Node(); while (head != null) { if ((s + "").equalsIgnoreCase(head.getName() + "")) { JOptionPane.showMessageDialog(null, head.getName() + ", " + head.getAuthor() + "-" + head.getId() + "\n"); break; } else { prev = head; head = head.next; if (head == null) { JOptionPane.showMessageDialog(null, "data not found"); head = prev; break; } } } lastNode.setNext(firstNode); } } }PHP Code:import java.util.*; import java.util.List; import java.awt.*; import java.awt.event.*; import javax.swing.*; import static javax.swing.JOptionPane.*; public class ClientNode extends JFrame { private static final long serialVersionUID = 1L; private JButton addNewBook = new JButton("Add a Book"); private JButton getSpecificBook = new JButton("Get a Book"); private JButton getAllBooks = new JButton("Get all Books"); private JButton removeOneBook = new JButton("Remove a Book"); private JButton removeAllBooks = new JButton("Remove all Books"); private JButton checkList = new JButton("Check List"); private JPanel buttonsPanel = null; private JPanel statusPanel = null; private List exampleLinkedList = null; private DefaultListModel model; JList list; CircularList listt = new CircularList(); public ClientNode() { exampleLinkedList = new LinkedList(); exampleLinkedList.clear(); exampleLinkedList.add("Linked List Example"); model = new DefaultListModel(); list = new JList(model); buttonsPanel = new JPanel(); statusPanel = new JPanel(); JScrollPane scrollPane = new JScrollPane(list); getContentPane().add(scrollPane, BorderLayout.SOUTH); // Add Them to a panel buttonsPanel.add(addNewBook); buttonsPanel.add(getSpecificBook); buttonsPanel.add(getAllBooks); buttonsPanel.add(removeOneBook); buttonsPanel.add(removeAllBooks); buttonsPanel.add(checkList); statusPanel.add(scrollPane); // Add elements to the frame add(buttonsPanel, BorderLayout.NORTH); add(statusPanel, BorderLayout.SOUTH); addNewBook.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String n = JOptionPane.showInputDialog("Enter book name : "); String a = JOptionPane.showInputDialog("Enter author name : "); String i = JOptionPane.showInputDialog("Enter book ID : "); listt.add(n, a, i); String result = "Book : " + " " + n + " Author : " + a + " ID: " + i; exampleLinkedList.add(result); model.add(list.getModel().getSize(), result); } }); getSpecificBook.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { if (!exampleLinkedList.isEmpty()) { // addLogMessage("\nGet Specific Item Example"); String result = ""; int max = exampleLinkedList.size() - 1; result = JOptionPane .showInputDialog("Enter number between 0 and " + max); Object x = model.get(Integer.parseInt(result)); JOptionPane.showMessageDialog(null, x); System.out.println(x); String y = x.toString(); JTextPane text = new JTextPane(); text.setText(y); // Font f = new Font("verdana", Font.BOLD + Font.ITALIC, // 16); // O setJTextPaneFont(text, f, Color.blue); list.setForeground(new Color( (int) (Math.random() * 222), (int) (Math .random() * 205), (int) (Math.random() * 213))); // list.setFont(f); } else { JOptionPane .showMessageDialog(null, "no item to return ,please add at least one item"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "no such item index"); } } }); getAllBooks.addActionListener(new ActionListener() { ArrayList<String> items = new ArrayList<String>(); public void actionPerformed(ActionEvent e) { if (!exampleLinkedList.isEmpty()) { for (int i = 0; i < model.getSize(); i++) { System.out.println(model.get(i)); items.add((String) model.get(i)); } JOptionPane.showMessageDialog(null, items.toArray()); } else { JOptionPane.showMessageDialog(null, "no items to return ,please add at least one item"); } } }); removeOneBook.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { if (!exampleLinkedList.isEmpty()) { // addLogMessage("\nRemove One Item Example"); String result = ""; int max = exampleLinkedList.size() - 1; result = JOptionPane .showInputDialog("Enter number between 0 and " + max); model.remove(Integer.parseInt(result)); } else { JOptionPane .showMessageDialog(null, "no items to remove ,please add at least one item"); } } catch (Exception exe) { JOptionPane.showMessageDialog(null, "no such item " + exe.getMessage()); } } }); removeAllBooks.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!exampleLinkedList.isEmpty()) { // addLogMessage("\nRemove All Items Example"); // exampleLinkedList.clear(); model.clear(); // addLogMessage("List has been cleared"); } else { // you can make it as exception for the array size JOptionPane.showMessageDialog(null, "no items to remove ,please add at least one item"); } } }); checkList.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (exampleLinkedList.isEmpty()) { // addLogMessage("List is empty"); JOptionPane.showMessageDialog(null, "no items to check ,please add at least one item"); } else { JOptionPane.showMessageDialog(null, "List is not empty"); } } }); } public static void main(String args[]) { ClientNode theApplication = new ClientNode(); theApplication.setDefaultCloseOperation(EXIT_ON_CLOSE); theApplication.setSize(500, 500); theApplication.pack(); theApplication.setVisible(true); } }
- 03-24-2011, 09:24 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
I need ur help guys .. as soon as possible .. the submitting after 2 hours
- 03-24-2011, 10:43 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
I don't understand the problem,
You're buttons are not working or you have not created an Iterator?
Mjall
- 03-25-2011, 01:38 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
LinkedList
By [RaIdEn] in forum New To JavaReplies: 7Last Post: 10-13-2009, 12:59 AM -
Help with LinkedList<AnyType>
By wesgarner in forum New To JavaReplies: 2Last Post: 09-24-2009, 03:00 PM -
LinkedList help
By jigglywiggly in forum New To JavaReplies: 6Last Post: 09-19-2009, 07:24 AM -
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:52 AM -
Problem with LinkedList
By Eric in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks