Вопрос

Trying to setup a program using ADTs for a college course that sorts out Storm Objects using a multi-layer LinkedList. Here is what I need to do.

  1. Use a two-level linked list to organize the information within the database.
  2. The nodes in the upper-level linked list will represent years, with one node per year.
  3. Each year node will contain a reference to a linked list of storms for that year.

  4. You should maintain the items in each linked list in sorted order, sorting the upper list by year (in numerical order, low-to-high) and the lower list by name (in standard dictionary order).

  5. You may use any variation on linked lists which you desire; dummy head nodes, head/end pointers, doubly-linked lists, etc..
  6. You may implement additional classes as desired in order to manage the list.

Can anyone walk me through this? Im stumped and have tried too hard thinking about this.

I already have 4 different classes for the two lists

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

Here is the code:

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; 

      }

   }

Sorry its alot. Thanks for your help!

Это было полезно?

Решение

I dont understand how to attach a linked list to another linked list.

You add a member of type StormList to class YearNode, along with a getter and setter method. You could also add a convenience method YearNode#addStorm(StormNode sn) that would add the StormNode to the contained StormList.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top