Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   ADT - basic list data type (http://www.programmingforums.org/showthread.php?t=12221)

JavaBeginner Dec 19th, 2006 10:52 PM

ADT - basic list data type
 
Calling on All Programmers;

I have a challenging (for me) question that I really need some help to get going in the right direction. I need to code a NewList (ADT) that will implement a separate file shown below:

--------------------------------------------

interface SmallList <ItemType> //Small list
{
public static final int MAX_LENGTH=5;

public boolean insertItem (ItemType newItem);
public boolean delItem();
public boolean newPos();
public ItemType getItem();

} // End of SmallList

--------------------------------------------

What I have at this point is a blank page quite frankly. I need some help to implement the interface shown above. Any help, comments, direction, etc. would be much appreciated.

To start:

public class NewList <ItemType> implements SmallList <ItemType>
{

}

Thanks

ReggaetonKing Dec 19th, 2006 11:14 PM

When a class implements an interface, the programmer must define all the method that are in that interface. Here's a small example of a class that implements an interface.
:

import java.util.Stack;
import java.util.ArrayList;

public class ChrisList implements Stack
{
        private ArrayList list;
       
        public ChrisStack
        {
                list = new ArrayList();
        }
        public boolean isEmpty()
        {
                return list.size() == 0;
        }
        public void push(Object o)
        {
                list.add(o);
        }
        public Object pop()
        {
                return list.remove(list.size() - 1);
        }
        public Object pop()
        {
                return list.get(list.size() - 1);
        }
}

The methods I have written for this class are all the method the interface declares. I have written my own version of a Stack data structure. This is basically all there is to it.

For your ADT, you just have to define all those methods in your class.
:

public class NewList<ItemType> implements SmallList<ItemType>
{
        public boolean insertItem(ItemType newItem)
        {
                //write your own implementation
        }
        public boolean delItem()
        {
                //write your own implementation
        }
        public boolean newPos()
        {
                //write your own implementation
        }
        public ItemType getItem();
        {
                //write your own implementation
        }
}

If you would explain the purpose of this SmallList interface you are trying to create. I would be glad to help you out more.

JavaBeginner Dec 20th, 2006 12:36 AM

Thanks for your help. Here is a quick description of the above:

:

public class NewList<ItemType> implements SmallList<ItemType>
{
            public NewList()
            {
                      //Constructs a new empty list and sets m & p to 0.
                      //m is the length of the list and p marks the current position.
                      //One constructor and no parameters.
              }

        public boolean insertItem(ItemType newItem)
        {
                //If p or m = the max length in SmallList, it fails
                          //If success, every item i is moved to the right
                          //Length of list m is increased by 1
                          //Value of p has not changed
                          //Current item is newItem.
        }
        public boolean delItem()
        {
                //If m or p = 0, it fails
                          //If success, the item in p is deleted
                          //Every item i is shifted to the left by one position
                          //Length of list m is decreased by 1
                          //Current position p has not changed.
        }
        public boolean newPos()
        {
                //Is true if 1 is less than or equal to p and p is less
                          //than or equal to m and returns false otherwise
        }
        public ItemType getItem();
        {
                //If 1 is less than or equal to p and p is less than or equal
                          //to m, it returns the item in p.  Otherwise it is null.
        }
}


bl00dninja Dec 20th, 2006 1:09 AM

perhaps a "heap" data structure would fulfill your needs? if java doesn't have one, then oh well...

ReggaetonKing Dec 20th, 2006 1:15 AM

Dude, if you can't convert your pseudocode into Java statements then I suggest you read up on Java. All you have to do is create private list object within your NewList class and just remove according to the directions given.

Also, the p value never changes. Is p the first index of your list? Why do you keep track of the position when all you do is remove an object from the front of the list? This data structure of yours does not make sense and has no point because all you are doing in reinventing the wheel.


All times are GMT -5. The time now is 12:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC