Results 1 to 4 of 4
  1. #1
    Usman is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default 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); 
    
        } 
    }

  2. #2
    Usman is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    I need ur help guys .. as soon as possible .. the submitting after 2 hours

  3. #3
    Mjall is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    I don't understand the problem,
    You're buttons are not working or you have not created an Iterator?

    Mjall

  4. #4
    Usman is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    my Buttons are working .. but I have to call the method from CircularList to ClientNode
    and create an Iterator

Similar Threads

  1. LinkedList
    By [RaIdEn] in forum New To Java
    Replies: 7
    Last Post: 10-13-2009, 12:59 AM
  2. Help with LinkedList<AnyType>
    By wesgarner in forum New To Java
    Replies: 2
    Last Post: 09-24-2009, 03:00 PM
  3. LinkedList help
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 09-19-2009, 07:24 AM
  4. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:52 AM
  5. Problem with LinkedList
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •