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...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
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,...
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,...
HI i will write user's manual for a doubly/singly linked list , How can i write...
HI i will write user's manual for a doubly/singly linked list , How can i write User's manual ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT