Please do this in Java!!!
Describe a method for performing a card shuffle of a singly linked
list of 2n elements, by
converting it into two lists. A card shuffle is a permutation where
a list L is cut
into two lists, L1 and L2, where L1 is the first half of L and L2
is the second half
of L, and then these two lists are merged into one by taking the
first element in
L1, then the first element in L2, followed by the second element in
L1, the second
element in L2, and so on.
Assume the input list is a singly linked list. Your algorithm has
to perform the shuffle
in-place, i.e., it is allowed to use only O(1) additional memory
cells. So, you are not allowed
to use any additional (or duplicate) list of any kind that may use
more than O(1) cells.
Space complexity: O(1).
Note: example is just shown for explanation purpose only.
Required 'shuffle' method(java):
public static LinkedList shuffle(LinkedList
card)
{//divide list into two equal
list.
// example: list
:1->2->3->4->5->6->7->8.
//List L1:
1->2->3->4->null.
List L2:5->6->7->8->null.
//List L1
head.
Node L1_head=card.head;
//List
L2_head
Node
L2_head=findMiddleNode(card);
//Current pointer to
traverse the list L1.
Node L1_currNode=L1_head;
//next pointer of current
pointer of List L1.
Node
L1_nextptr=L1_currNode.next;
//Current pointer to
traverse the list L2.
Node L2_currNode=L2_head;
//next pointer of current
pointer of List L2.
Node
L2_nextptr=L2_currNode.next;
while(L2_nextptr!=null)
{
//exchange the
link between List L1 and L2.
L1_currNode.next=L2_currNode;
L2_currNode.next=L1_nextptr;
//Incrementing
the list L1 pointers.
L1_currNode=L1_nextptr;
L1_nextptr=L1_nextptr.next;
//Incrementing
the list L2 pointers.
L2_currNode=L2_nextptr;
L2_nextptr=L2_nextptr.next;
}
//exchange the last
node links of List L1 and L2.
L1_currNode.next=L2_currNode;
L2_currNode.next=null;
//retrun the reference
of head.
return card;
}
Complete java code:
public class LinkedList {
Node head; // head of list
static class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
//insert data into linked List.
public static LinkedList insert(LinkedList card,
int data)
{
Node new_node = new
Node(data);
new_node.next =
null;
if (card.head == null)
{
card.head = new_node;
}
else {
Node last = card.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
return card;
}
public static Node findMiddleNode(LinkedList
card)
{
Node slowPointer=card.head;
Node fastPointer=card.head;
while(fastPointer!=null)
{
slowPointer=slowPointer.next;
fastPointer=fastPointer.next.next;
}
return slowPointer;
}
public static LinkedList shuffle(LinkedList
card)
{//divide list into two equal list.
//List L1 head.
Node L1_head=card.head;
//List L2_head
Node
L2_head=findMiddleNode(card);
//Current pointer to traverse the
list L1.
Node L1_currNode=L1_head;
//next pointer of current pointer of
List L1.
Node
L1_nextptr=L1_currNode.next;
//Current pointer to traverse the
list L2.
Node L2_currNode=L2_head;
//next pointer of current pointer of
List L2.
Node
L2_nextptr=L2_currNode.next;
while(L2_nextptr!=null)
{
//exchange the link
between List L1 and L2.
L1_currNode.next=L2_currNode;
L2_currNode.next=L1_nextptr;
//Incrementing the list
L1 pointers.
L1_currNode=L1_nextptr;
L1_nextptr=L1_nextptr.next;
//Incrementing the list
L2 pointers.
L2_currNode=L2_nextptr;
L2_nextptr=L2_nextptr.next;
}
//exchange the last node links
of List L1 and L2.
L1_currNode.next=L2_currNode;
L2_currNode.next=null;
//retrun the reference of
head.
return card;
}
public static void printList(LinkedList
card)
{
Node currNode =
card.head;
System.out.print("Card:
");
while (currNode != null)
{
System.out.print(currNode.data + " ");
currNode = currNode.next;
}
}
public static void main(String[] args)
{
/* Start with the empty
list. */
LinkedList card = new
LinkedList();
//insert the data into
LinkedList.
card=insert(card,1);
card=insert(card,2);
card=insert(card,3);
card=insert(card,4);
card=insert(card,5);
card=insert(card,6);
card=insert(card,7);
card=insert(card,8);
printList(card);
card=shuffle(card);
System.out.println("\n\nAfter shulfle: ");
printList(card);
}
}
Output:
Card: 1 2 3 4 5 6 7 8 Node slowPointer=card.head; Node fastPointer=card.head; After shulfle: Card: 1 5 2 6 3 7 4 8 while(fastPointer !=null) slowPointerEslowPointer.next; fastPointer-fastPointer.next.next; return slowPointer; public static LinkedList shuffle(Linkedlist card), {//divide list into two equal list. //List Li head. Node Li_head=card.head; //List L2_head Node L2_head=findMiddleNode(card); //Current pointer to traverse the list L1. Node L1_currNode=L1_head; //next pointer of current pointer of List L1. Node L1_nextptr=L1_currNode.next; //Current pointer to traverse the list L2.
Please do this in Java!!! Describe a method for performing a card shuffle of a singly...
Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list L1 with 4 nodes. You can use insert operation to add nodes to the list. Each element represent an airport code (e.g. BOS, ATL, JFK, MSP, etc.). Display the list L1 after it is created. B. Given singly linked list L1, create another singly linked list L2 that contains the same elements but in the reverse order. Display the content of both L1 and...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...
*Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...
Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games One of the nice things about Java is it is very easy to do fun things with graphics. The start code provided here will display a deck of cards on the screen and shuffle them. Your mission, is to start with this code and build a card game. Blackjack, poker solitaire, what ever your heart desires. Blackjack is the easiest. Obviously any program you...
8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....
java programming course use java import. Follow instructions and comments to explain the code thanks in advance Print L A polymomial can be represented as a linked list, where each node called a polyhede contoins the coefficient and the exponent of a term of the pelynomial For example, The pelynomial 4x-31-5 would be represented as the linked list 3 3xa Write a Polynomial class that has methods for creating a polhynomial, reading and writing a polynomial, and adding a pair...
Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! The Learning Goal for this exercise is to use and understand and know the difference between arrays and array lists. !!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in...
Java - Data structures and algorithms VI. Matching Game Consider a matching game in which you have a list of random integer values between 10 and 99. You remove from the list any pair of consecutive integers that match. If first integer has digits xly1 and the second integer has digits x2y2 the match is found if any ofthe following is true xi is the same as x2 x1 is the same as y2 yl is the same as x2...
Pull out question 8 on Exercises. See also: Program 4.16 invert (), Program 4.4 printList () Configuring -main () 1. Create a linked list 2. Function call of 8 (a) and (b) 3. Check the accuracy of the output of r list and l list 4. Repeat steps 1-3 above Please use c language void printlist(listPointer first) printf ("The list contains: ) for (; first; first = first→link) printf ("%4d", first-"data); printf("In") Program 4.4: Printing a list the nodes are...