Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Norm

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

  1. #1
    Enirox is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    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. I would be really grateful if someone could create these 3 methods for me, since I would have a great example to use. I have to create the 3 methods in bold. Here's the code I have:
    Java Code:
    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; 09-19-2012 at 09:23 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

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

    Please use [code] tags [/code] when posting code.
    Unformatted code is hard to read.

    This isn't a code factory (see my sig).
    If you have a problem then ask a question, posting relevant code and any errors/exceptions you are having.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Enirox is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

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

    Quote Originally Posted by Tolls View Post
    Please use [code] tags [/code] when posting code.
    Unformatted code is hard to read.

    This isn't a code factory (see my sig).
    If you have a problem then ask a question, posting relevant code and any errors/exceptions you are having.
    Sorry, didn't know about the [code], I edited it. And my problem is that I do not know how to create these 3 methods. The code I posted, in my opinion, is relevant, because it defines some of the functions that you have to use, and overall structure.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

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

    Pick one and then explain what you think you would need to do to fulfill the requirements for the method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Enirox is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

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

    Quote Originally Posted by Tolls View Post
    Pick one and then explain what you think you would need to do to fulfill the requirements for the method.
    Could you just 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 ).

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

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

    That page is giving examples of how to use those methods, not what those methods do (ie the code in them).

    You have a singly linked list there.
    Each Node knows what the next Node in the list is.
    The List itself knows what the first, last and current Nodes are.
    Not sure whay exactly it needs to know all that, but hey ho.

    All three of those methods need you to find a Node at k.
    In order to change the Node at k (either replacing, inserting or removing) you'll need to change the Node at k-1 to point at whatever the new Node is.

    That is your basic logic.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

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

    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Enirox is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

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

    Quote Originally Posted by Tolls View Post
    That page is giving examples of how to use those methods, not what those methods do (ie the code in them).

    You have a singly linked list there.
    Each Node knows what the next Node in the list is.
    The List itself knows what the first, last and current Nodes are.
    Not sure whay exactly it needs to know all that, but hey ho.

    All three of those methods need you to find a Node at k.
    In order to change the Node at k (either replacing, inserting or removing) you'll need to change the Node at k-1 to point at whatever the new Node is.

    That is your basic logic.
    What about methods for combining lists like these? And finding the same nodes of 2 different lists?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

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

    I suggest that you work on one method at a time. Trying to do several at the same time can make the project more complicated.
    Tolls likes this.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

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

    As Norma says, do those three methods you originally asked about.
    That should give you the ammunition to use for more complex tasks.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

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

    Quote Originally Posted by Tolls View Post
    As Norma says, ...
    She does?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

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

    Quote Originally Posted by DarrylBurke View Post
    She does?

    db
    Oops.
    Sorry Norm.
    I seem to have a bad habit of doing that...I usually manage to catch it before I post though!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Getting problem inserting data in data base
    By anupama in forum New To Java
    Replies: 4
    Last Post: 12-15-2010, 11:03 PM
  2. how to store the data in data base
    By eclipse3.4ide in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 05:25 AM
  3. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 05:10 PM
  4. Replies: 0
    Last Post: 10-31-2008, 03:13 PM
  5. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 03:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •