Question

In: Computer Science

Swap two adjacent elements by adjusting only the links (and not the data) using a. singly...

Swap two adjacent elements by adjusting only the links (and not the data) using
a. singly linked lists
b. doubly linked lists

Solutions

Expert Solution

(a) Sample data structure for the list in C++ language is as follows

struct node

{

int data;

struct node* next;

}l-list, temp_0, temp, temp, temp_1;

Suppose next is the pointer used to form the linked list and points to the next element and l-list is the pointer to the first element of the list. Also temp_0, temp and temp_1 are the temporary element pointers used for traversing the list.

Now let us suppose the temp points to element of the linked list, temp_0 points to the previous elementand temp_1 is the next element of the linked list.

For swapping two elements, (in C++ pointer style)

temp_0->next = temp_1

temp = temp_1->next

temp_1->next = temp

(b) Sample data structure for the list is as follows

struct node

{

int data;

struct node* next_0;

struct node* next;

}l-list, temp_0, temp, temp, temp_1;

Suppose next_0 and next is the pointer used to form the linked list where next_0 is the pointer to the previous element of the current element and next is the pointer to the next element of the list. l-list is the pointer to the first element of the list. Also temp_0, temp and temp_1 are the temporary element pointers used for traversing the list.

Now let us suppose the temp points to element of the linked list, temp_0 points to the previous elementand temp_1 is the next element of the linked list.

For swapping two elements, (in C++ pointer style)

temp_0->next = temp_1

temp = temp_1->next

temp_1->next = temp

Note: Only usefulness of the doubly linked list is in the calculation of temp_0 where it can be calculated as temp_0 = temp->next_0. Whereas in the case of singly linked list to calculate temp_0 we have to iterate from the starting of the list to the previous element of the current element.


Related Solutions

Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
Write a program in Python where you can swap only two consecutive elements. You have to...
Write a program in Python where you can swap only two consecutive elements. You have to show all steps to convert a string into another string (both strings will be anagrams of each other). E.g. GUM to MUG GUM GMU MGU MUG
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly...
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly linked lists if you can make it clean and short that be nice
Atoms of two adjacent elements in the fourth period are in theground state. An atom...
Atoms of two adjacent elements in the fourth period are in the ground state. An atom of element A has only s electrons in its valence shell. An atom of element B has at least one p electron in its valence shell.
Atoms of two adjacent elements in the periodic table are in the ground state. Both atoms...
Atoms of two adjacent elements in the periodic table are in the ground state. Both atoms have a total of 10 d electrons. Atom A has no p electrons in its valence shell but atom B has one electron in the p subshell of its valence shell. Element A is ___ and element B is ___.
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the following operations: 1. Adding an element at the middle of the list. 2. Removing the middle element of the list (and return the data). 3. Adding an element at a given index. 4. Removing an element at a given index (and return the data). For #1 and #2, ignore the operations if the length of the list is an even number. For #3, if...
Show that there are only two distinct groups with four elements, as follows. Call the elements...
Show that there are only two distinct groups with four elements, as follows. Call the elements of the group e, a. b,c. Let a denote a nonidentity element whose square is the identity. The row and column labeled by e are known. Show that the row labeled by a is determined by the requirement that each group element must appear exactly once in each row and column; similarly, the column labeled by a is determined. There are now four table...
Using only the elements Ca, Cl, Si, give formulas for the following: (a) an ionic compound....
Using only the elements Ca, Cl, Si, give formulas for the following: (a) an ionic compound. (b) a molecular compound with polar covalent bonds that obeys the octect rule and has no formal charge. For part b I drew the lewis structure Cl(double bond)-Ca-(double bond)Cl with two lone pair on each chloride, is that correct? Please explain. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT