Question: Programming Project 6 Linked List using pointers and dynamic memory / pgm6.c Problem: Create a C program that does the following: reads real numbers stored

Programming Project 6 Linked List using pointers and dynamic memory / pgm6.c Problem: Create a C program that does the following:

reads real numbers stored as ASCII text, one number per line from a file o numbers may be positive or negative o number may or may not have an embedded decimal point o numbers are not ordered in any way within the file

dynamically allocates memory for a structure (linked list node) containing two variables o one of type float for the real number o one a structure pointer type that will point to the next node in the list

inserts each node into a singly-linked list in ascending order o start at the list head and move towards the tail o link the new node into its proper place in the list

traverse the linked list o start at the head and advance to the next node until the tail is reached o write the number in each node to output file

Requirements:

1. The program must ask the user for the input file name and continue to do so until a valid input file (i.e., one that actually exists) is entered.

2. The output file name must be constructed from the input file name. If a file extension is present, it must be removed by stripping off the dot extension. An extension of _out.txt must be added to the end of the base input file name. For example, if the input file name is pgm1_data.txt, then the output file name should be pgm1_data_out.txt.

3. An input /output report for each program execution must be printed. The following example is to serve as a template. Input numbers are in testfile.txt and numbers from the ordered linked list are in testfile_out.txt. Numbers should be counted as read in and counted as written out. Allowing for 999 numbers is more than sufficient. Program to read and insert a list of numbers into a singly-linked list in ascending order Enter filename: testfile.txt Input file testfile.txt contains 999 numbers Output file testfile_out.txt contains 999 numbers

4. If you need to use printf() statements for debugging you may comment these out using the C++ comment delimiter // instead of deleting lines, otherwise remember this is a C program.

Algorithm:

1. Dynamically allocates memory for filename strings. a. Input filename string (long enough to hold any valid name). b. Output filename created from input filename by adding _out before extension.

2. Prompt the user for the name of an input file. a. Open this file if it exists. b. if file does not exist prompt the user for another file name. c. Continue until a file that exists is entered.

3. Read a float data item from each line in this file.

4. Dynamically allocates memory for and creates a node to hold this data. a. Data type for content of this node is float. b. Data type for next node is for struct node type.

5. Insert a node into linked list in ascending order (a linked list is a pointer to a node type).

6. Close the input file.

7. After the linked list is built, it contains the numbers from the input file in ascending order.

8. Open the output file by its name.

9. Traverse the linked list from its head to its tail. a. Write the float from the current node to the output file. b. Advance to the next node.

10. Close the output file.

11. Free all dynamically allocated memory. a. Filename strings. b. Nodes in the linked list.

12. Exit program.

Constraints: The program must adhere to the following:

This is a C program. Command I/O must be implemented using scanf() and printf(). File I/O must be implemented using fscanf() and fprintf().

No global variables may be declared or defined or used. Variables defined in main() and other functions are not global.

The program must use dynamic memory allocation to define all complex data types (structs, strings, and arrays) needed. In addition, pointers must be used for all operations that access these complex data types. Thus, your program must not include any square brackets ('['or ']') anywhere in the code.

This program can be written efficiently in a single C source code file, but you must use the following function prototypes to modularize your code.

int main(); The main function can be prototyped and the following is adequate

void insert(NodePtr *head, float number); This function allocates a new node with the value of number and inserts it into place in the linked list using the pointer to head.

NodePtr first(NodePtr head); This function returns the address of the first node in the linked list pointed to by head.

NodePtr next(NodePtr curr); This function returns the address of the next node in the linked list after node curr.

float value(NodePtr curr); This function returns the value of the number stored in linked list node curr.

void freelist(NodePtr *head); This function frees memory allocated to each node in the linked list pointed to by head.

All files that are opened must be closed and all dynamic memory allocated must be freed before the program exits. Your source code files must compile and link to create an executable file named pgm1.exe using the included make utility Makefile. You must include the following program identification comment block at the start of the source code file that contains your main() function and add the correct relevant information wherever it is not present. The pledge must be present.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!