Describe in detail how to swap two nodes x and y (and not just their contents) in a singly linked list L given references only to x and y. Repeat this exercise for the case when L is a doubly-linked list. Which algorithm takes more time. C++ please
//Swapping in singly Linked List
void swappingNodes(Node **head, Node* x, Node* y)
{
//if both nodes are same
if (x == y) return;
// Search for y (keep track of previous of node y and Current
node y )
Node *prevY = NULL, *currentY = *head;
while (currentY && currentY!= y)
{
prevY = currentY;
currentY = currentY->next;
}
// Search for x (keep track of previous of node x and Current node
x )
Node *prevX = NULL, *currentX = *head;
while (currentX && currentX != x)
{
prevX = currentX;
currentX = currentX->next;
}
// If either x or y is not present in the list
if (currentX == NULL || currentY == NULL)
return;
// If x is not head of linked list
if (prevX != NULL) prevX->next = currentY;
else // Else make y as new head
*head = currentY;
// If y is not head of linked list
if (prevY != NULL)
prevY->next = currentX;
else // Else make x as new head
*head = currentX;
// Swap next pointers
Node *temp = currentY->next;
currentY->next = currentX->next;
currentX->next = temp;
}
//swapping nodes in doubly linked list
void swapingNodes(struct Node* x, struct Node* y){
if ( x->prev != NULL){
x->prev->next = y;
}
else {
head = y;
}
if ( y->next !=NULL){
y->next->prev = x;
}
x->next = y->next;
y->prev = x->prev;
y->next = x;
x->prev = y;
}
swapping node in singly linked list takes more time.
Describe in detail how to swap two nodes x and y (and not just their contents)...
Describe in detail an algorithm for reversing a singly linked list L using only a constant amount of additional space. Please provide pseudocode for Java.
Describe in detail an algorithm for reversing a singly linked list L using only a constant amount of additional space and not using any recursion.
Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable { // ---------------- nested Node class...
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...
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...
I also don't quite understand the meaning of N.size, is N.size
(the sum of nodes) (a node and its sub trees) has? please also
explain that, thank you.
modify the standard definition of a binary search tree to add a field N.size at each Suppose that we node, which records the size of the subtree under N'Tincluding N itself). A. Explain how to modify the procedure for adding both the case where X is not yet in the tree and...
1) Design a greedy algorithm that solves the problem; describe your algorithm with clear pseudocode; and prove the time efficiency class of your algorithm: If x, y are two adjacent elements in a sequence, with x before y, we say that the pair x, y is in order when x <= y and the pair is out of order when x > y. For example, in the string “BEGGAR” the pair G, A are out of order, but all the...
In this exercise, you will make a struct that contains x and y coordinates: struct point {int x; int y;}; The object of this exercise is to make a singly linked list of these point structures. Add one more member called "next" to your point struct. It is a pointer to the next member of the linked list. The next member of the last point struct should point to NULL, which indicates the end of the list. Your program will...
can you please solve this CORRECTLY?
Exercise 4 - Shortest path (25 pts) Using Dijkstra's algorithm, find the shortest path from A to E in the following weighted graph: a- Once done, indicate the sequence (min distance, previous node) for nodes D and E. (15pts) b- Below is a high-level code for Dijkstra's algorithm. The variables used in the code are self-explanatory. Clearly explain why its running time (when we use a min-heap to store the values min distance of...
this is the page no. 255 ..
can some one please help me with please. just give a way to do it
assuming that we have add algs4.jar.
thank you!!
As a general r libraries provided by the authors of our text in algs4.jar. However, java.util.Arrays.sort(), should be used as indicated below ule for programming assignments for this class, you should feel free to use the Exercise Comparing Sorting Methods . The purpose of this exercise is to get a...