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

Thread: please teach me how to do this Link List assignment

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default please teach me how to do this Link List assignment

    please teach me to do this assignment question no7,just question no 7 because i really dont understand *public*void*
    swapMiddle() in Linked ListCapture.JPG
    Attached Files Attached Files


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: please teach me how to do this Link List assignment

    Swapping two elements of linked list requires changing the current pointers to the way they'd be if the items were swapped. In the original:

    1 -> 2
    2 -> 3
    3 -> 4

    Becomes

    1 -> 3
    3 -> 2
    2 -> 4

    Given two elements of a linked list, write a method to swap them by changing the pointers.

    But what's that got to do with "*public*void*?" 'public' is the access modifier and 'void' is the return type. What don't you understand about those?

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please teach me how to do this Link List assignment

    so we need create method in link list class?

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default im trying to do this LinkList method but still not work

    can somebody help me fix this code as i want to swap the middle number
     
    public class LinkList
    {
        private Node first;
        private Node last;
        private Node current;
     
        public LinkList()
        {
            first = null;
            last = null;
            current = null;
        }
     
        public void insertAtBack(Object insertitem)
        {
            Node newNode = new Node(insertitem);
     
            if(isEmpty())
            {
                first = newNode;
                last = newNode;
            }
            else
            {
                last.next = newNode;
                last = newNode;
            }
        }
     
        public void insertAtFront(Object insertitem)
        {
            Node newNode = new Node(insertitem);
     
            if(isEmpty())
            {
                first = newNode;
                last = newNode;
            }
            else
            {
                newNode.next=first;
                first=newNode;
            }
        }
     
        public Object getFirst()
        {
            if(isEmpty())
            {
                return null;
            }
            else
            {
                current = first;
                return current.data;
            }
        }
     
        public Object getNext()
        {
            if(current ==last)
            {
                return null;
            }
            else
            {
                current = current.next;
                return current.data;
            }
        }
     
        public Object removeFromFront()
        {
            Object removeItem=null;
            if(isEmpty())
            {
                return removeItem;
            }
            removeItem = first.data;
            if(first==last)
            {
                first=null;
                last=null;
            }
            else
            {
                first=first.next;
            }
            return removeItem;
        }
     
        public Object removeFromBack()
        {
            Object removeItem=null;
            if(isEmpty())
            {
                return removeItem;
            }
            removeItem=last.data;
            if(first==last)
            {
                first=null;
                last=null;
            }
            else
            {
                current=first;
                while(current.next!=last)
                {
                    current=current.next;
                }
                last=current;
                last.next=null;
            }
            return removeItem;
        }
     
        public boolean isEmpty()
        {
            return(first==null);
     
        }
     
        public void swapMiddle(Object obj)
        {
            Node tmp =new Node(obj) ;
            first = last;
            last = tmp;
        }
     
       //this is LinkList class 
     
    }
     
    import javax.swing.*;
     
    public class swap
    {
        public static void main(String[]args)
        {
            LinkList swapM=new LinkList();
     
            for(int i=1;i<=4;i++)
            swapM.insertAtBack(i);
            System.out.println("Sequence of data BEFORE insertion:");
            Object obj=swapM.getFirst();
            while(obj!=null)
            {
                System.out.println(obj);
                obj=swapM.getNext();
            }
     
            swapM.swapMiddle(obj);
     
            System.out.println("Sequence of data AFTER insertion:");
            obj=swapM.getFirst();
            while(obj!=null)
            {
                System.out.println(obj);
                obj=swapM.getNext();
            }
        }
    }

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default can somebody fix my coding?

    can somebody help me fix this code as i want to swap the middle number
     
    public class LinkList
    {
        private Node first;
        private Node last;
        private Node current;
     
        public LinkList()
        {
            first = null;
            last = null;
            current = null;
        }
     
        public void insertAtBack(Object insertitem)
        {
            Node newNode = new Node(insertitem);
     
            if(isEmpty())
            {
                first = newNode;
                last = newNode;
            }
            else
            {
                last.next = newNode;
                last = newNode;
            }
        }
     
        public void insertAtFront(Object insertitem)
        {
            Node newNode = new Node(insertitem);
     
            if(isEmpty())
            {
                first = newNode;
                last = newNode;
            }
            else
            {
                newNode.next=first;
                first=newNode;
            }
        }
     
        public Object getFirst()
        {
            if(isEmpty())
            {
                return null;
            }
            else
            {
                current = first;
                return current.data;
            }
        }
     
        public Object getNext()
        {
            if(current ==last)
            {
                return null;
            }
            else
            {
                current = current.next;
                return current.data;
            }
        }
     
        public Object removeFromFront()
        {
            Object removeItem=null;
            if(isEmpty())
            {
                return removeItem;
            }
            removeItem = first.data;
            if(first==last)
            {
                first=null;
                last=null;
            }
            else
            {
                first=first.next;
            }
            return removeItem;
        }
     
        public Object removeFromBack()
        {
            Object removeItem=null;
            if(isEmpty())
            {
                return removeItem;
            }
            removeItem=last.data;
            if(first==last)
            {
                first=null;
                last=null;
            }
            else
            {
                current=first;
                while(current.next!=last)
                {
                    current=current.next;
                }
                last=current;
                last.next=null;
            }
            return removeItem;
        }
     
        public boolean isEmpty()
        {
            return(first==null);
     
        }
     
        public void swapMiddle(Object obj)
        {
            Node tmp =new Node(obj) ;
            first = last;
            last = tmp;
        }
     
       //this is LinkList class 
     
    }
     
    import javax.swing.*;
     
    public class swap
    {
        public static void main(String[]args)
        {
            LinkList swapM=new LinkList();
     
            for(int i=1;i<=4;i++)
            swapM.insertAtBack(i);
            System.out.println("Sequence of data BEFORE insertion:");
            Object obj=swapM.getFirst();
            while(obj!=null)
            {
                System.out.println(obj);
                obj=swapM.getNext();
            }
     
            swapM.swapMiddle(obj);
     
            System.out.println("Sequence of data AFTER insertion:");
            obj=swapM.getFirst();
            while(obj!=null)
            {
                System.out.println(obj);
                obj=swapM.getNext();
            }
        }
    }

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: please teach me how to do this Link List assignment

    Please don't start multiple threads on the same topic.

    What help do you need with the code you posted? Are there errors? If so, post them. Does the swap not work as intended? If so, describe what is happening and what should be happening. Ask specific questions about what you need help with.

Similar Threads

  1. Anyone can teach me whats wrong with my program ... (Linked List)
    By darkagui23 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2014, 05:31 AM
  2. Replies: 3
    Last Post: October 5th, 2013, 09:14 AM
  3. Best way to teach myself Data structures?
    By EatSleepProgram in forum Java Theory & Questions
    Replies: 2
    Last Post: August 12th, 2013, 05:47 AM
  4. Can someone teach me Java?
    By deano2213 in forum The Cafe
    Replies: 3
    Last Post: May 25th, 2012, 09:28 AM
  5. Teach me about inheritance and extends?
    By tripline in forum Java Theory & Questions
    Replies: 7
    Last Post: November 26th, 2011, 04:24 PM

Tags for this Thread