In: Computer Science
Solve the following Double Linked List sort: use int. array = {8, 3, 12, 9, 6, 2} and provide visualization development to the 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