Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Build a doubly linked list given the keyboard inputs or the user provided data file; Perform a insertion-sort (in the ascending order of the value)

  1. Build a doubly linked list given the keyboard inputs or the user provided data file;
  2. Perform a insertion-sort (in the ascending order of the value) for the above linked list, and print the list out to the screen. For comparison purpose, please also print the unsorted linked list.

Your doubly linked list node data structure is defined as in the following:

struct mynode {

int const value; // once initialized, cannot be updated

struct mynode *next;

struct mynode *prev;

};

NOTE: YOU ARE NOT ALLOWED TO MODIFY THE ABOVE DEFINITION, OTHERWISE YOU WILL RECEIVE ZERO.

A pre-prepared data file example is listed at below:

30

20

50

70

10

0

Given the file above, your program should build a linked list with 5 nodes. Note that 0 indicates the end of the data file and 0 should not be included in the linked list. Given the above 5 data, your unsorted linked list should look like:

3020507010==>==>==>==>

Then your program prints out the sorted list:

7050302010==>==>==>==>

Your program's implementation must include the following features:

  • Your program must be compiled from 3 source files:
    • main.c (Handles input and output, as well as top-level program logic.)
    • node.h (Declares the data structure, e.g., struct mynode, and function insertsort(), which sorts a given doubly linked list with the ascending order), and printlist(), which prints a linked list to the screen.
    • node.c (Defines the function insertsort() and printlist(), as declared innode.h.)
  • How to initialize a linked list node with a const value? Since the value field is declared as const, you are not allowed to use regular assignment to initialize the value. Instead, you should using the following method. Assuming that you allocated sufficient memory space for an array of nodes, i.e., nodelist[0], nodelist[1], nodelist[2], ..., nodelist[k-1] (totally k nodes), to initialize the nodes, use scanf() directly:

scanf(\"%d\", &nodelist[i].value); // where i is an int variable, you may want to put this statement inside a for/while loop

  • You must write insertsort() and rest of the project by yourself. If your code is \"adopted\" from the Internet, e.g., geeksforgeeks.org, or any other sources, your submission will not be graded and will be considered violating the ethics policy.
  • Your node.h file must contain the proper preprocessor directives to prevent multiple inclusion.
  • The main function must use scanf() function call to read the input data from keyboard (note that the input redirection can be used to directly read the data from a data file). The number of data (in the data file) is not pre-determined.
  • The function insertsort() must be declared exactly as follows.
    • return value:struct mynode *.
    • argument (only one): struct mynode *, which is the head of the given linked list.
    • Note that you must implement the insertsort() function by yourselves and cannot use any existing implementation from other library. You have to use insertsort algorithm instead of other sorting algorithm.
  • The function printlist() must be declared exactly as follows.
    • return value: void.
    • argument (only one):struct mynode *, which is the head of the given linked list.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Usability

Authors: Jakob Nielsen, Raluca Budiu

1st Edition

0133122131, 9780133122138

Students also viewed these Programming questions

Question

What is a root name server?

Answered: 1 week ago