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: Implementation of remove method in linkedlist Iterator

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Implementation of remove method in linkedlist Iterator

    import java.util.*;
    public class LinkedListADT
    {
        private Node first;
        int size=0;
     
        public LinkedListADT ()
        {
            first = null;
        }
     
        public int getSize()
        {
            return size;
        }
     
        public void addToPosition(Object newData, int position)
        {
            if (isEmpty())
            {
                first = new Node(newData, null);
            }
            else if (position > size)
                addToBack(newData);
            else if (position == 1)
                addToFront(newData);
            else
            {
                Node temp = new Node(newData,null);
                int current=1;
                Node stop = first;
                while(stop.next!=null)
                {
                    if (current+1 != position)
                    {
                       current++;
                        stop = stop.next; 
                    }  
                    else
                    {
                        temp.next = stop.next;
                        stop.next = temp;
                        break;
                    }   
                }    
            }
        }
     
        public void addToFront(Object newData)
        {
            if (isEmpty())
            {
                first = new Node(newData, null);
            }
            else
            {
                Node temp = new Node(newData,null);
                temp.next = first;
                first = temp;
            }
            size++;
        }
     
        public void addToBack(Object newData)
        {          
            if (isEmpty()) 
                first = new Node(newData, null);
            else
            {
                Node temp = new Node(newData,null);
                Node end = first;
                while(end.next!=null)
                {
                    end = end.next;
                }                           
                end.next = temp;
                end = null;
            }
            size++;
        }
     
        public void removeFront()
        {
            if(!isEmpty())
            {
                first = first.next;
                size--;
            } 
        }
     
        public void removeBack()
        {
            Node current = first;
            if(!isEmpty())
            {
                if (current.next != null)
                {
                    while(current.next.next != null)
                    {
                            current = current.next;
                    }
                    current.next = null;
                }else
                {
                    first = null;
                    current = null;
                }
                size--;
            }
        }
     
        public boolean isEmpty()
        {
            return first == null;
        }
     
        public void clear()
        {
            first = null;
            size = 0;
        }
     
        public String getAllData()
        {
            String output = "";
            Node temp = first;
            while(temp.next != null)
            {
                output += temp.data + "\n\n";
                temp = temp.next;
            }
            output += temp.data + "\n\n";
            return output;
        }
     
        private class Node
        {
            Object data;
            Node next;
     
            public Node (Object newData, Node newNext)
            {
                data = newData;
                next = newNext;
            }
        }
     
        public Iterator getIterator()
        {
            return new MyIterator();
        }
     
        //iterator class for MyLinkedList
        private class MyIterator implements Iterator
        {
            //attribute - a ref to a current node in the list
            private Node current;
            private boolean endOfIteration;
     
            public MyIterator()
            {
                current = first;
                endOfIteration = false;
     
            }
     
            public boolean hasNext()
            {
                if(current == null)
                        return false;
                else if(endOfIteration == true)
                        return false;
                else
                        return true;
            }
     
            public Object next() 
            {
                if (!isEmpty())
                {
                    Object value;
                    value = current.data;    
                    current = current.next;
                    if (current == null)
                    {
                        endOfIteration = true;
                    }   
                    return value;
                }
                else
                    throw new NoSuchElementException();
            }
     
            public  void remove()
            {
                //how to implement this method
            }
        }
    }

    I would like to ask how to implement a remove method in an iterator like this. need it for my college assignment, thx in advance. helps will be much appreciate!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Implementation of remove method in linkedlist Iterator

    We aren't a homework service, and we aren't going to help you cheat. What have you tried? Where are you stuck? What is confusing you?

    Recommended reading: http://www.javaprogrammingforums.com...e-posting.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementation of remove method in linkedlist Iterator

    Sorry i didn't meant to cheat here. I have tried to implement the remove method myself but it seem cannot work. I just need some guide on implementing the remove method.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Implementation of remove method in linkedlist Iterator

    I would recommend you draw out a linked list on paper. Then iterate over it by, say, drawing an arrow to each node. What would you have to do to remove an element? What would have to change? Where do you get that information?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. About add and remove method of ListNode, i don't understand....
    By Timloneungpo in forum Collections and Generics
    Replies: 1
    Last Post: September 28th, 2011, 08:09 AM
  2. LinkedList insert, remove and change
    By Alexie91 in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2011, 07:16 AM
  3. How to remove argument from abstract method.
    By jainshasha in forum Object Oriented Programming
    Replies: 12
    Last Post: June 30th, 2011, 10:33 PM
  4. I need to fix the remove method on DoublyLinkedList.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 23rd, 2010, 09:20 PM
  5. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM