-1

尝试使用 ADT 为大学课程设置程序,该课程Storm使用多层 LinkedList 对对象进行分类。这是我需要做的。

  1. 使用两级链表来组织数据库中的信息。
  2. 上层链表中的节点代表年份,每年一个节点。
  3. 每个年份节点都将包含对该年风暴链接列表的引用。

  4. 您应该按排序顺序维护每个链接列表中的项目,按年份排序上部列表(按数字顺序,从低到高),按名称排序下部列表(按标准字典顺序)。

  5. 您可以在链表上使用您想要的任何变体;虚拟头节点、头/尾指针、双向链表等。
  6. 您可以根据需要实现其他类以管理列表。

谁能帮我完成这个?我很难过,并且已经努力思考这个问题。

这两个列表我已经有 4 个不同的类

  • 年节点.java
  • 年表.java
  • StormNode.java
  • StormList.java
  • Storm.java

这是代码:

年节点.java

    public class YearNode
{

        int year;
        YearNode next;
        StormNode sNext;

    public int getYear()
    {
        return year;
    }

    public StormNode getSNext()
    {
        return sNext;
    }

    public YearNode getNext()
    {
        return next;
    }

    public void setYear(int year)
    {
        this.year = year;
    }

    public void setNext(YearNode next)
    {
        this.next = next;
    }

    public void setSNext(StormNode next)
    {
        this.sNext = sNext;
    }
}

年表.java

    public class YearList
{
    YearNode head;
    YearNode sHead;

    public YearList()
    {
        YearNode head = null;
        YearNode sHead = null;
    }

    public boolean isEmpty()
    {
        return(head == null);
    }

    public boolean isStormEmpty()
    {
        return(sHead == null);
    }

    public void setSList(StormNode node)
    {
        StormNode sHead = node;
    }

    public int size()
    {
        YearNode current = head;
        int counter = 0;
        while(current != null)
        {
            counter++;
            current = current.getNext();
        }
        return counter;
    }

    public int get(int index)
    {
        int size = size();
        YearNode current = head;

        if((index > size) || (index < 0))
        {
            throw new IndexOutOfBoundsException();
        }
        else
        {
            int counter = index;
            while(counter != 0)
            {
                current = current.getNext();
                counter--;
            }
            return current.getYear();
        }
    }

    public void add(int index, int year)
    {
        if((index < 0) || (index > size()))
        {
            throw new IndexOutOfBoundsException();
        }
        YearNode current = head;
        YearNode previous = null;
        while (index > 0)
        {
            index--;
            previous = current;
            current = current.getNext();
        }
        YearNode Splice = new YearNode();
        Splice.setYear(year);
        Splice.setNext(current);
        if(previous == null)
        {
            head = Splice;
        }
        else
        {
            previous.setNext(Splice);
        }
    }

    public int remove(int index)
    {
        if((index < 0) || (index > size()))
        {
            throw new IndexOutOfBoundsException();
        }
        YearNode current = head;
        YearNode previous = null;
        while (index > 0)
        {
            index--;
            previous = current;
            current = current.getNext();
        }
        if(previous == null)
        {
            head = current.getNext();
        }
        else
        {
            previous.setNext(current.getNext());
        }

        return current.getYear();

    }

    public void removeAll()
    {
            head = null;
    }
}

StormNode.java

 public class StormNode
{

        Storm storm;
        StormNode next;

    public Storm getStorm()
    {
        return storm;
    }

    public StormNode getNext()
    {
        return next;
    }

    public void setStorm(Storm storm)
    {
        this.storm = storm;
    }

    public void setNext(StormNode next)
    {
        this.next = next;
    }
}

StormList.java

    public class StormList
{
    StormNode head;

    public StormList()
    {
        StormNode head = null;
    }

    public StormNode getHead()
    {
        return head;
    }

    public boolean isEmpty()
    {
        return(head == null);
    }

    public int size()
    {
        StormNode current = head;
        int counter = 0;
        while(current != null)
        {
            counter++;
            current = current.getNext();
        }
        return counter;
    }

    public Object get(int index)
    {
        int size = size();
        StormNode current = head;
        if((index >= size) || (index < 0))
        {
            throw new IndexOutOfBoundsException();
        }
        else
        {
            int counter = index;
            while(counter != 0)
            {
                current = current.getNext();
                counter--;
            }
            return current.getStorm();
        }
    }

    public void add(int index, Storm Storm)
    {
        if((index < 0) || (index > size()))
        {
            throw new IndexOutOfBoundsException();
        }
        StormNode current = head;
        StormNode previous = null;
        while (index > 0)
        {
            index--;
            previous = current;
            current = current.getNext();
        }
        StormNode Splice = new StormNode();
        Splice.setStorm(Storm);
        Splice.setNext(current);
        if(previous == null)
        {
            head = Splice;
        }
        else
        {
            previous.setNext(Splice);
        }
    }

    public Object remove(int index)
    {
        if((index < 0) || (index > size()))
        {
            throw new IndexOutOfBoundsException();
        }
        StormNode current = head;
        StormNode previous = null;
        while (index > 0)
        {
            index--;
            previous = current;
            current = current.getNext();
        }
        if(previous == null)
        {
            head = current.getNext();
        }
        else
        {
            previous.setNext(current.getNext());
        }

        return current.getStorm();

    }

    public void removeAll()
    {
            head = null;
    }
}

Storm.java

    public class Storm{

      private int year; //
      private String name;
      private int startDay;
      private int endDay;
      private int category; 


      public Storm(int year, String name, int startDay, int endDay, int category){
         this.year = year;
         this.name = name;
         this.startDay = startDay;
         this.endDay = endDay;
         this.category = category;

      }

      public Storm()
      {
         this.year = 0;
         this.name = "";
         this.startDay = 0;
         this.endDay = 0;
         this.category = 7;
      }


      public String getName()
      {
         return this.name;
      }

      public int getYear()
      {
         return this.year;
      }

        public int getStartDay()
      {
         return this.startDay;
      }

        public int getEndDay()
      {
         return this.endDay;
      }

        public int getCategory()
      {
         return this.category;
      }

      public String getFormattedStartDay()
      {
            String startDayReturn = "";
            if (this.startDay == 9999)
                {
                    startDayReturn = "(no start)";
                }
            else
                {   
                 int startDayDigit1 = (this.startDay / 1000) % 10;
                 int startDayDigit2 = (this.startDay / 100) % 10;
                 int startDayDigit3 = (this.startDay / 10) % 10;
                 int startDayDigit4 = this.startDay % 10;

                 startDayReturn = (startDayDigit1 + startDayDigit2 + "/" + startDayDigit3 + startDayDigit4);
                }
         return startDayReturn;
      }

      public String getFormattedEndDay()
      {
         String endDayReturn = "";
         if(this.endDay == 9999)
         {
            endDayReturn = "(no end)";
         }
         else
         {
            int endDayDigit1 = (this.endDay / 1000) % 10;
            int endDayDigit2 = (this.endDay / 100) % 10;
            int endDayDigit3 = (this.endDay / 10) % 10;
            int endDayDigit4 = this.endDay % 10;

            endDayReturn = (endDayDigit1 + endDayDigit2 + "/" + endDayDigit3 + endDayDigit4);
         }
         return endDayReturn;
      }

      public String getFormattedCategory()
      {
         int cat = this.category;
         String categoryReturn = "";
         switch(cat){
            case 0:
               categoryReturn = "Tropical Storm";
               break;
            case 1:
               categoryReturn = "Hurricane Level 1";
               break;
            case 2:
               categoryReturn = "Hurricane Level 2";
               break;
            case 3:
               categoryReturn = "Hurricane Level 3";
               break;
            case 4:
               categoryReturn = "Hurricane Level 4";
               break;
            case 5:
               categoryReturn = "Hurricane Level 5";
               break;
            case 6:
               categoryReturn = "Hurricane Level 6";
               break;
            default:
               categoryReturn = "no info";
               break;
         }

         return categoryReturn; 

      }

   }

很抱歉。谢谢你的帮助!

4

1 回答 1

0

我不明白如何将链接列表附加到另一个链接列表。

您将 type 的成员以及 getter 和 setter 方法添加StormList到 class 。YearNode您还可以添加一个方便的方法YearNode#addStorm(StormNode sn),将 . 添加StormNode到包含的StormList.

于 2012-10-25T05:32:39.840 回答