【问题标题】:LinkedList insert using head insertLinkedList 使用头部插入插入
【发布时间】:2013-05-11 02:03:06
【问题描述】:

好的,我现在遇到的问题是我在“public class MagazineView extends Applet {”处收到错误,我得到“空白字段 Jlist 可能尚未初始化”

这是我的 GUI 小程序

public class MagazineView extends Applet {
Button button, button2;
final MagazineList Jlist;


public void init() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//NORTH PANEL
JPanel northPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Add Magazine:     ");
final JTextField text1 = new JTextField();
JButton button = new JButton("List Magazines");


button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
            final String name=text1.getText();
            Magazine mag = new Magazine(name);
            Jlist.insert(mag);
        }

});

    topPanel.add(label, BorderLayout.LINE_START);
    topPanel.add(text1, BorderLayout.CENTER);
    topPanel.add(button, BorderLayout.LINE_END);
   //CENTER PANEL
     JPanel centerPanel = new JPanel(new BorderLayout());
     JTextArea atext = new JTextArea();
     centerPanel.add(atext, BorderLayout.CENTER);

    //BOTTOM PANEL  
    JPanel bottomPanel = new JPanel(new BorderLayout());
    JLabel label2 = new JLabel("Delete All:     ");
    JTextField text2 = new JTextField();
    JButton button2 = new JButton("Delete Magazine");
    bottomPanel.add(label2, BorderLayout.LINE_START);
    bottomPanel.add(text2, BorderLayout.CENTER);
    bottomPanel.add(button2, BorderLayout.LINE_END);

    northPanel.add(topPanel, BorderLayout.NORTH);
    northPanel.add(centerPanel, BorderLayout.CENTER);
    northPanel.add(bottomPanel, BorderLayout.SOUTH);

    //Frame
    frame.add(northPanel);
    frame.setSize(600, 400);
    frame.setVisible(true);

   }


}

这是我的方法

      public class MagazineList {
      private MagazineNode list;

      public MagazineList(){
           list=null;
      }
      public void insert (Magazine mag){
    MagazineNode node = new MagazineNode(mag);
    node.next = list;
    list = node;
  }
      public void add (Magazine mag){
           MagazineNode node = new MagazineNode (mag);
           MagazineNode current;

           if(list==null)
                list = node;
           else
           {
                current = list;
                while(current.next !=null)
                     current = current.next;
                current.next = node;
            }
    }

    public void DeleteNode(Magazine mag)
    {
        if(list == null) throw new RuntimeException("Cannot delete, Empty List");

        if( list.magazine.equals(mag) )
        {
            list = list.next;
            return;
        }

        MagazineNode cur  = list;
        MagazineNode prev = null;

        while(cur != null && !cur.magazine.equals(mag) )
        {
            prev = cur;
            cur = cur.next;
        }

        if(cur == null) throw new RuntimeException("Cannot delete, not in list");

        //delete cur node
        prev.next = cur.next;
    }

    public String toString(){
        String result ="";

        MagazineNode current = list;

        while (current !=null){
            result += current.magazine + "\n";
            current = current.next;
        }

        return result;
    }

    private class MagazineNode {
        public Magazine magazine;
        public MagazineNode next;

        public MagazineNode(Magazine mag){
            magazine = mag;
            next = null;
        }
    }   
}

【问题讨论】:

  • 有什么问题?你看到了什么你不应该看到的行为?你试过什么?具体的错误信息或行为是什么?如果没有事情可做,我们无法为您提供帮助。
  • 请发布您的确切错误信息或写下确切的问题。
  • 没有错误。就是不知道在 public void Insert(Magazine mag){ list = new MagazineNode(mag);} 中写什么代码让节点插入新的字符串;或者如何在 public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { final String name=text1.getText(); }
  • 如果您的代码与问题无关,请删除批量。见SSCCE

标签: java linked-list


【解决方案1】:

如果你在头部插入节点,你不需要遍历列表。您的 list 变量是链表的头部,因此让您的新节点指向它并使其成为头部。

/**
 * Add a new node containing the input Magazine to the front of the linked list.
 *
 * @param mag A magazine to put at the head of the list.
 */
public void add (Magazine mag){
    MagazineNode node = new MagazineNode(mag);
    node.next = list;
    list = node;
}

您可能希望将名称 list 更改为 head。请注意,这不需要任何 if 语句。顺便说一句,您不必显示 GUI 代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2012-08-17
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多