Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Threaded View

  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.


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, 02:13 AM
  4. Replies: 2
    Last Post: June 15th, 2011, 03:49 PM
  5. Replies: 1
    Last Post: June 11th, 2011, 05:39 AM