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 11 of 11

Thread: Simple linked list

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Simple linked list

    Hello I do Singly-Linked Lists. But i have probelm. I canīt do some methods (empty methods in my code). How can i do it?
    Thank you.
    My code:
    Main class:
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
          Pomoc o1 = new Pomoc();
     
          System.out.println(o1.isEmpty());
          o1.addFirst(10);
          o1.addFirst(20);
          o1.addFirst(25);
          System.out.println(o1.isEmpty());
          System.out.println(o1.getFirst());
     
     
     
        }
     
    }
    Item class:
    public class Item {
        private int data;
        private Item next;
        private Item previous;
     
        public void setData(int data)
        {
            this.data=data;
        }
     
        public int getData()
        {
            return data;
        }
     
        public void setNext(Item next)
        {
            this.next=next;
        }
     
        public Item getNext()
        {
            return next;
        }
     
           public void setPrevious(Item previous)
        {
            this.previous=previous;
        }
     
        public Item getPrevious()
        {
            return previous;
        }
     
     
     
    }
    Method class
    public class Pomoc {
        private Item act;
        private Item first;
     
        public boolean isEmpty()
        {
                return first == null;
        }
     
        public void addFirst(int o)
        {
           Item newItem = new Item();
           newItem.setData(o);
           newItem.setNext(first);
           first = newItem;
        }
     
        public void removeFirst()
        {
            if(first==null)
            {
                System.err.println("Seznam je prazdny");
            }
            else
            {
                this.first=first;
            }
     
            if(act==first)
            {
                act = null;
            }
        }
     
        public int getFirst()
        {
            return first.getData();
        }
     
        public void setActFirst()
        {
            act=first;
        }
     
        public void setActNext()
        {
            Item actNext = new Item();
            actNext.setPrevious(act);
            act = actNext;
     
        }
     
        public void removeAfterAct()
        {
            act.setNext(act);
        }
     
        public void addAfterAct(int i)
        {
            act.setData(i);
            act.setNext(act);
        }
     
        public int getAct()
        {
            return act.getData();
        }
     
        public int getSize()
        {
     
        }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Location
    Mount Morgan, Queensland, Australia
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple linked list

    The only problem I see is that: public int getSize() is not returning anything, nor is it being used. So you can either edit it out or have it return something. I'm still in the process of learning this stuff, so I might be way off base.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple linked list

    I need know how can i do empty methods...

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,370
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Simple linked list

    Is your question that you need to figure out the size of the linked list? Search google and you will get tons of code listing how to do this. An alternative way is to use the java.util.LinkedList. But to do with you class, you must iterate through the list from first to last counting up along the way. Here is a basic way you can do this. It can also be done using recursion, but I won't post that code
    public static int getSize(){
    	Item f = first;
    	int count = 1;//count the first node
    	while ( f.next != null ){//if the list continues
    		count++;
    		f = f.next;
    	}
    	return count;
    }

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple linked list

    Oh thnak you very much anf how can i do this method:
    public void addAfterAct(int i)
    {

    }

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Simple linked list

    That's not a particularly efficient method to get the size of the linked list Each time, you must run through all the elements just to find the size. A better way is to have a size variable, then update it whenever you add/remove something from the list. Then just return that value when the user asks for the size.

    Here's how to insert after act:

    1. create a new Item with i in it.
    2. set that Item's next as act's next.
    3. set act's next to be the newly created Item.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,370
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Simple linked list

    Quote Originally Posted by helloworld922 View Post
    That's not a particularly efficient method to get the size of the linked list
    I totally agree, its probably the least efficient way to go about it. But its self sustaining and easier (at least for me) to write out and explain
    Last edited by copeg; November 1st, 2009 at 02:04 PM.

  8. #8
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple linked list

    Thank you so much. I will try do it. But do you have idea how can i do these three methods<
    act = actual...
        public void setActNext()
        {
     
        }
     
        public void removeAfterAct()
        {
     
        }
     
        public void addAfterAct(int i)
        {
     
        }

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Simple linked list

    setActNext is probably the easiest:
    act = act.next

    To remove after act:

    Since you have a doubly-linked list, you need to adjust both the next and the previous of the elements before and after the element after act.

    act.next.previous = act
    act.next=act.next.next

    Because of Java's garbage collector, the de-referenced item will automatically be removed for you.

    I put the pseudo code for adding after act above.

  10. The Following User Says Thank You to helloworld922 For This Useful Post:

    Koren3 (November 2nd, 2009)

  11. #10
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple linked list

    Thank you!

  12. #11
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Simple linked list

    The best way to do the size is probably to maintain a size variable your self and increment it upon a call to add or decrement it upon a call to remove and then all you need to do is return the value of the size variable in your size method.

    // Json

Similar Threads

  1. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 11:40 AM
  2. Which collection is best to do mathematical operation on it?
    By Sterzerkmode in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2009, 05:48 AM
  3. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 12:48 PM
  4. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 07:23 PM
  5. String substring, concatenation operation on linked list
    By oaks12 in forum Collections and Generics
    Replies: 1
    Last Post: January 15th, 2009, 07:12 AM