Question

In: Computer Science

I know this code takes in a set of numbers into a doubly linked list and...

I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working?

main.c Source Code:

#include
#include
#include "node.h"

int main()
{
struct mynode *head=NULL;
int value;

printf("Give first value: \n");
scanf("%d",&value);

printf("Give next values, and input 0 to end list: \n");
do{
if(value>0){
   head = pushNode(head, value);
   scanf("%d",&value);
}
}while (value>0);

printf("Before insertion sort: ");
printlist(head);

head=insertsort(head);
printf("After insertion sort: ");
printlist(head);
return 0;
}

Source Code node.c:

//defines all functions of myNode declared in node.h
#include
#include
#include "node.h"

void printlist (struct mynode *node)
{
while(node)
{
if(node->next)
printf("%d<==>", node->value);
else
printf("%d\n", node->value);

node=node->next;
}
return;
}

struct mynode* pushNode(struct mynode *node, int const newValue)
{
struct mynode *newNode = (struct mynode*)malloc(sizeof(struct mynode));
//assign the head in the list which will be returned every time
struct mynode *list = node;
int *ptr = &newNode->value;
*ptr = newValue;
newNode->next = NULL;
if(node == NULL)
{
newNode->prev=NULL;
node=newNode;
list=node;
return list;
}
while(node->next != NULL)
{
node=node->next;
}
newNode->prev = node;
node->next = newNode;
return list;
}

struct mynode* insertsort(struct mynode *head)
{
//list variable to store the sorted list
struct mynode* sorted = NULL;
struct mynode* list = NULL;

struct mynode* current = head;
while (current != NULL) {
struct mynode* next = NULL;
next = current->next;
current->next = NULL;
current->prev = NULL;

if (sorted == NULL)
{
sorted = current;
list=sorted;
}
else if (sorted->value >= current->value)
{
current->next = sorted;
current->next->prev = current;
sorted = current;
list=sorted;
}
else
{
list=sorted;

struct mynode* newVal = NULL;
newVal = sorted;
//check where to enter the new value
while (newVal->next != NULL && newVal->next->value < current->value)
{
newVal = newVal->next;
}

current->next = newVal->next;

if (newVal->next != NULL)
current->next->prev = current;

newVal->next = current;
current->prev = newVal;
}

current = next;
}

//return the sorted list
return list;
}

Source Code node.h:

//declares the data structure and the functions of said data structure
#ifndef NODE_H_
#define NODE_H_


struct mynode {
int const value;
struct mynode *next;
struct mynode *prev;
};


struct mynode * insertsort(struct mynode *head);
void printlist (struct mynode *node);
struct mynode* pushNode(struct mynode *node, int const newValue);
#endif

Solutions

Expert Solution

Dear Learner

As doubly linked list work in the way as I given below:

It has 4 ways to insert the node in to doubly linked list such as insert from front, last, before particular and after particular node.

As per this code the insertion only take place at the end/last of the list.

Step 1: Coder create a header file which contains

· Structure of doubly linked list by using structure called mynode

· Three functions called pushNode (adding node), printlist (traversal) and insertion sort.

Step 2: Save the file node.h, which become header. With this header file he has to import the file only instead declaring the structure in the same.

struct mynode

{
int const value;
struct mynode *next;
struct mynode *prev;
};


struct mynode * insertsort(struct mynode *head);
void printlist (struct mynode *node);
struct mynode* pushNode(struct mynode *node, int const newValue);

Step 3: coder open new file code source code of C like below

#include "node.h"                                                           // calling header file                       

int main()
{
struct mynode *head=NULL;       // declare head pointer as a pointer by declaring the Null value
int value;

printf("Give first value: \n");
scanf("%d",&value);

Step 4: Do while look will accept the input and add to the list and jump every time to puchNode() function definition to perform the action till user will not provide the 0 for exit it add the values.

printf("Give next values, and input 0 to end list: \n");

do

{
if(value>0)

{
   head = pushNode(head, value);
   scanf("%d",&value);
}
}while (value>0);

//

struct mynode* pushNode(struct mynode *node, int const newValue)
{
struct mynode *newNode = (struct mynode*)malloc(sizeof(struct mynode));

//assign the head in the list which will be returned every time
struct mynode *list = node;

int *ptr = &newNode->value;
*ptr = newValue;
newNode->next = NULL;
if(node == NULL)
{
newNode->prev=NULL;
node=newNode;
list=node;
return list;
}
while(node->next != NULL)
{
node=node->next;
}
newNode->prev = node;
node->next = newNode;
return list;
}

Step 5: Called printlist () function before Insertion sort to display all elements of linked list.

printf("Before insertion sort: ");
printlist(head);

//function will work like this

void printlist (struct mynode *node)
{
while(node)
{
if(node->next)
printf("%d: ", node->value);
node=node->next;
}
return;
}

Step 6: Called insertionsort () function to perform the sorting

head=insertsort(head);

// calling function to it will jump to the function definition

struct mynode* insertsort(struct mynode *head)
{
//list variable to store the sorted list
struct mynode* sorted = NULL;
struct mynode* list = NULL;

struct mynode* current = head;
while (current != NULL) {
struct mynode* next = NULL;
next = current->next;
current->next = NULL;
current->prev = NULL;

if (sorted == NULL)
{
sorted = current;
list=sorted;
}
else if (sorted->value >= current->value)
{
current->next = sorted;
current->next->prev = current;
sorted = current;
list=sorted;
}
else
{
list=sorted;

struct mynode* newVal = NULL;
newVal = sorted;
//check where to enter the new value
while (newVal->next != NULL && newVal->next->value < current->value)
{
newVal = newVal->next;
}

current->next = newVal->next;

if (newVal->next != NULL)
current->next->prev = current;

newVal->next = current;
current->prev = newVal;
}

current = next;
}

//return the sorted list
return list;
}

Step 7: Called printlist () function after Insertion sort to display all elements of linked list.
           printf("After insertion sort: ");
             printlist(head);

Step 8: Exit

For more understanding see the image..


Related Solutions

This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I need an example of how to swap and index within a doubly linked list with...
I need an example of how to swap and index within a doubly linked list with index + 1. This is written in java. public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values   ...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
Use BASH to make a code that takes any list of numbers and calculate the Median...
Use BASH to make a code that takes any list of numbers and calculate the Median and Mode.
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly...
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly linked lists L1 and L2, where L1 contains the odd-number-th elements in L and L2 contains the even-number-th elements in L. Elements in both L1 and L2 should appear in the same order as they appear in L. For example, if L contains the numbers 4, 7, 9, 1, and -3, in that order, then the output L1 should contain 4, 9, and -3,...
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT