Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: List methods add(int k, Data data), set(int k, Data data), remove(int k)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default List methods add(int k, Data data), set(int k, Data data), remove(int k)

    Hello,I've recently started studying Java, and I've encountered some problems with lists. There's pretty much close to none information about this where I am studying at, so I would be really grateful if someone could create these 3 methods for me, since I would have a great example to use later on. I have to create the 3 methods add(int k, Data data), set(int k, Data data), remove(int k) . Here's the code I have : ( although I think the main part needed for these methods is:
    private Node<Data> first;
    private Node<Data> last;
    private Node<Data> current;


    package studijosKTU;
     
    public class ListKTU<Data> implements ListADT<Data> {
     
        private Node<Data> first;   
        private Node<Data> last;    
        private Node<Data> current; 
        private int size;           
        /**
         * Constructs an empty list.
         */
        public ListKTU() {
        }
        public boolean add(Data data) {      // adds an element to the end of the list
            if(data==null) return false;        // doesnt take null objects
            if (first == null) {
                first = new Node<Data>(data, first);
                last = first;
            } else {
                Node<Data> e1 = new Node(data, null);
                last.next = e1;
                last = e1;
            }
            size++;
            return true;
        }
        public boolean add(int k, Data data){  // inserts before k position
            if(data==null) return false;       // doesnt take null bojects
            if (k<0||k>=size)return false;     // returns null if k isn't correct.
            throw new UnsupportedOperationException
                    ("Have to create this one");
     
        public int size() {     
            return size;
        }
        public boolean isEmpty() {      
            return first == null;
        }
        public void clear() {
            size = 0;
            first = null;
            last = null;
            current = null;
        }
        public Data get(int k){           // returns the value of k
            if (k<0||k>=size)return null;
            current=first.findNode(k);   
            return current.element;
        }
        public Data set(int k, Data data){   // sets the value of element k
            if(data==null) return null;     
            throw new UnsupportedOperationException
                    ("Have to create this one");
        }
        public Data getNext(){       
            if(current==null) return null;
            current=current.next;
            if(current==null) return null;
            return current.element;
        }
        public Data remove(int k) {
           throw new UnsupportedOperationException
                    ("Have to create this one");
        }
     
    class Node<Data> {          // class that ensures incapsulation
        public Data element;   
        public Node<Data> next; 
     
        Node(Data data, Node<Data> next) { 
            this.element = data;
            this.next = next;
        }
        public Node<Data> findNode(int k){ 
            Node<Data> e=this;
            for(int i=0;i<k;i++){
               e=e.next;
            }
            return e;
        }
    }
    }
    Last edited by Enirox; September 19th, 2012 at 03:31 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)

    To see what a list looks like and how to manipulate it, use a piece of paper and a pencil. Draw a list with some nodes connected by lines representing next pointers and having data.
    Then work on the logic of how to do the functions of the three methods you need.
    Do them one at a time. If you have problems working out the logic, post some pseudo code for what you think a method should do, and we'll help you get the logic right.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)

    Quote Originally Posted by Norm View Post
    To see what a list looks like and how to manipulate it, use a piece of paper and a pencil. Draw a list with some nodes connected by lines representing next pointers and having data.
    Then work on the logic of how to do the functions of the three methods you need.
    Do them one at a time. If you have problems working out the logic, post some pseudo code for what you think a method should do, and we'll help you get the logic right.
    Could you tell me what should I pick of THIS list to get the examples for the methods I need? None of the "List" or "ArrayList" methods look even close to the code I have, or I'm not looking at the right place. I really cannot spend whole day trying to get one method to work, considering I have 5 more things to work on at the university, so I just wanted to get an example which I could use. For me, it makes more sense to analyze a working method so I can use it in the future, instead of trying to write one without any kind of knowledge about lists in java ( but hey, that's what the professors at the damn university is asking me to do ).

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)

    Post your code that you are having problems with and ask questions about it.

    Also posted at: http://www.java-forums.org/new-java/...ove-int-k.html
    Last edited by Norm; September 20th, 2012 at 08:56 AM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: April 9th, 2012, 05:13 PM
  2. [SOLVED] Extracting data from data base (yes)
    By bobbyk in forum Collections and Generics
    Replies: 1
    Last Post: March 13th, 2012, 07:07 AM
  3. Replies: 2
    Last Post: February 17th, 2012, 01:13 AM
  4. Replies: 2
    Last Post: June 15th, 2011, 03:49 PM
  5. Replies: 1
    Last Post: June 11th, 2011, 05:39 AM