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
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.
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
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?
so we need create method in link list class?
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(); } } }
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(); } } }
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.