【问题标题】:Creating a database using a "Two Layer LinkedLists" [closed]使用“两层链接列表”创建数据库 [关闭]
【发布时间】:2012-10-25 03:43:48
【问题描述】:

尝试为大学课程设置一个使用 ADT 的程序,该程序使用多层 LinkedList 整理 Storm 对象。这是我需要做的。

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

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

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

谁能帮我完成这个?我被难住了,我已经很努力地想这个了。

我已经为这两个列表设置了 4 个不同的类

  • YearNode.java
  • YearList.java
  • StormNode.java
  • StormList.java
  • Storm.java

代码如下:

YearNode.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;
    }
}

YearList.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; 

      }

   }

很抱歉。感谢您的帮助!

【问题讨论】:

  • "Can anyone walk me through this? Im stumped and have tried too hard thinking about this." -- 你最好提出具体问题以获得具体有用的答案。
  • 我只需要知道如何整体构建代码,两个链表如何交互等等。我可以编码的细节。我只是在设计阶段被难住了。
  • 我不明白如何将一个链表附加到另一个链表。这就是我需要知道的。

标签: java list linked-list abstract-data-type


【解决方案1】:

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

您将StormList 类型的成员与getter 和setter 方法一起添加到YearNode 类。您还可以添加一个方便的方法YearNode#addStorm(StormNode sn),它将StormNode 添加到包含的StormList

【讨论】:

    猜你喜欢
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2012-12-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多