Question

In: Computer Science

Solve the following Double Linked List sort: use int. array = {8, 3, 12, 9, 6,...

Solve the following Double Linked List sort: use int. array = {8, 3, 12, 9, 6, 2} and provide visualization development to the solution.

Solutions

Expert Solution

# include <stdio.h>
# include <stdlib.h>
//structure of a node of double link list
struct link
{
   int info;
   struct link *next;
   struct link *previous;
};
struct link start;
void create (struct link *);
void display (struct link *);
void sort(struct link *);
//sort method implements selection sort
void sort(struct link *node)
{
   struct link *New,*temp;
for(New = start.next; New->next != NULL; New = New->next)
   {
   for(temp = New->next; temp != NULL; temp = temp->next)
   {
       if(New->info > temp->info)
       {
       int t = New->info;
       New->info = temp->info;
       temp->info = t;
       }
       }
   }
}
//create method creates the link list
void create(struct link *node)
{
   char ch='y';
   start.next = NULL; /* Empty list */
   start.previous = NULL;
   node = &start; /* Point to the start of the list */
   while( ch == 'y' || ch=='Y')
   { //allocate memory for a new node
       node->next = (struct link *) malloc(sizeof(struct link));
       node->next->previous = node; //create the links to previous node
       node = node->next; //create the link to next node
       printf("\n ENTER THE NUMBER");
       fflush(stdin);
       scanf("%d", &node->info);//input the number
       node->next = NULL;
       fflush(stdin);
       printf("\nDO YOU WANT TO CREATE MORE NODES[Y/N] ");
       fflush(stdin); //ask for creating more nodes or not
       scanf("%c",&ch);
   }
}
//display nethod
void display (struct link *node)
{//assign the node to start node of list
   node = start.next;
   do
   {
       printf(" %d", node->info);//print the value of node
       node = node->next; //jump to next node
   }while(node!=NULL);//loop will continue till end
}
//driver program
void main()
{
   struct link *node;
   create(node); //create the link list
   printf("\n AFTER CREATING THE LINKED LIST IS \n");
   display(node);//display the list
   sort(node);//sort the list
   printf("\n AFTER SORTING THE LINKED LIST IS \n");
   display(node);//display the list after sorting
}

OUTPUT

EXPLANATION


Related Solutions

Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7...
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7 In java please.
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array...
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3: Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3:
Consider the following sorted int array: 1 3 4 5 7 9 10 12 If we...
Consider the following sorted int array: 1 3 4 5 7 9 10 12 If we search for the key value 10 in the array using the binary search algorithm, what is the sequence of indices that will be accessed in the array? (Hint: For a sublist between index values low and high, the middle index is calculated using: (low + high) / 2. So you need to show the sequence of indices of the middle index in each pass.)...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6};...
5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6}; and we also have a linked list L of 5 entries 1 -> 2 -> 3 -> 5 -> 6, where 1 is the first in the linked list L, followed by 2 etc. We want to put a new item 4 between 3 and 5 in the array a and in the linked list L (a) Explain in plain English how you do...
Given the matrix 7 7 -4 12 -5 A = 9 10 2 6 13 8 11 15 4 1 3. a. Sort each column and store the result in an array B.
Given the matrix a. Sort each column and store the result in an array B.b. Sort each row and store the result in an array C.c. Add each column and store the result in an array D.d. Add each row and store the result in an array E.  
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
For the following data set, X: 9, 6, 8, 3, 8, 9, 3, 4, 3, 7:...
For the following data set, X: 9, 6, 8, 3, 8, 9, 3, 4, 3, 7: Calculate: 1. Variance 2. Mode 3. Mean 4. Mean Average Deviation (MAD) about the mean 5. Median
use quick sort to sort the following array. show each pass and what the pivot is...
use quick sort to sort the following array. show each pass and what the pivot is during the pass. please explain why you are swapping positions. do not use a compiler. 30 5 40 11 20 9 15 2 60 25 80 3 73 35 4 75 20 6
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT