java在节点列表中按字母顺序插入和排序

时间:2015-07-21 16:45:46

标签: java sorting nodes

我正在尝试创建一个程序,可以按字母顺序对提供的“杂志标题”列表进行排序,并在插入新杂志时刷新排序列表。

以下是具体的作业说明: 修改杂志架程序以按字母顺序添加新杂志(您必须修改杂志以使其与compareTo()方法实现Comparable等。)

BONUS:同时实施一个删除方法,并在您的驱动程序中进行演示。

以下是我目前的清单:

public class MagazineList_jcm
{
private MagazineNode list;

//----------------------------------------------------------------
//  Sets up an initially empty list of magazines.
//----------------------------------------------------------------
public MagazineList_jcm()
{
    list = null;
}

//  public boolean delete(Magazine mag)
//   {
    //  MagazineNode current = list;
    //   MagazineNode previous = null;
    //look through list of magazines
    //first, check for a null list
    // if so, return false
    //if a current.mag with the same title as mag
    //delete it from the list

    //if no mag found, return false

//  }

//----------------------------------------------------------------
//  Creates a new MagazineNode object and adds it to the end of
//  the linked list.
//----------------------------------------------------------------
public void add(Magazine_jcm mag)
{
    MagazineNode node = new MagazineNode(mag);
    MagazineNode current;
    MagazineNode previous;
    boolean done = false;

    if (list == null)
        list = node;
    else
    {
        current = list;
        while (!done)   
        {
            int comp = current.magazine.compareTo(mag);
            if (comp == 0) 
                //- duplicate
            else if (comp > 0)
                //add before current
            if previous null
            //insert at front of list
            list = node;
             node.next = current;

            //ELSE, INSERT BETWEEN PREVIOUS AND CURRENT

        //  else if (comp <0)
                //add after....
            //if current.next null
            //add right after 
//  else 
        //move to the next node
            previous = current;
            current = current.next;


            current = current.next;
        }
        current.next = node;
    }
}

//----------------------------------------------------------------
//  Returns this list of magazines as a string.
//----------------------------------------------------------------
public String toString()
{
    String result = "";

    MagazineNode current = list;

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

    return result;
}

//*****************************************************************
//  An inner class that represents a node in the magazine list.
//  The public variables are accessed by the MagazineList class.
//*****************************************************************
private class MagazineNode
{
    public Magazine magazine;
    public MagazineNode next;

    //--------------------------------------------------------------
    //  Sets up the node
    //--------------------------------------------------------------
    public MagazineNode(Magazine mag)
    {
        magazine = mag;
        next = null;
    }
}
}

这是我的“杂志”:

public class Magazine_jcm 
{
   private String title;

   //-----------------------------------------------------------------
   //  Sets up the new magazine with its title.
   //-----------------------------------------------------------------
   public Magazine_jcm(String newTitle)
   {    
      title = newTitle;
   }

   //-----------------------------------------------------------------
   //  Returns this magazine as a string.
   //-----------------------------------------------------------------
   public String toString()
   {
      return title;
   }


}

这是我的驱动程序:

public class MagazineRack_jcm
{
   //----------------------------------------------------------------
   //  Creates a MagazineList object, adds several magazines to the
   //  list, then prints it.
   //----------------------------------------------------------------
   public static void main(String[] args)
   {    



  MagazineList_jcm rack = new MagazineList_jcm();

  rack.add(new Magazine_jcm("Time"));
  rack.add(new Magazine_jcm("Woodworking Today"));
  rack.add(new Magazine_jcm("Communications of the ACM"));
  rack.add(new Magazine_jcm("House and Garden"));
  rack.add(new Magazine_jcm("GQ"));

  String newMag = JOptionPane.showInputDialog("Please enter a Magazine to add to the list:");
  rack.add(new Magazine_jcm(newMag));
  System.out.println(rack); 
   }
}

1 个答案:

答案 0 :(得分:-1)

这个答案可以在in another question上看到。

只需更改功能即可比较杂志标题而不是整理