Trying to figure out what needs to be in the headers.h file. I have written the print function.
Qualities in header file
Please copy-and-paste the following files (0 Points):
insertionSort.c
/*--------------------------------------------------------------------------*
*---- ----*
*---- insertionSort.c ----*
*---- ----*
*---- This file defines a function that implements insertion ----*
*---- sort on a linked-list of integers. ----*
*---- ----*
*---- ---- ---- ---- ---- ---- ---- ---- ---- ----*
*---- ----*
*---- ----*
*---- ----*
*--------------------------------------------------------------------------*/
#include "headers.h"
// PURPOSE: To sort the linked-list of nodes pointed to by 'nodePtr' using
// the insertion sort algorithm. Returns the first node of the sorted
// list.
struct Node* insertionSort (struct Node* nodePtr
)
{
struct Node* startPtr = NULL;
struct Node* endPtr = NULL;
struct Node* lowestPtr;
struct Node* lowestPrevPtr;
while (nodePtr != NULL)
{
struct Node* prevPtr;
struct Node* run;
lowestPrevPtr = NULL;
lowestPtr = nodePtr;
for (prevPtr = nodePtr, run = nodePtr->nextPtr_;
run != NULL;
prevPtr = run, run = run->nextPtr_
)
{
if (lowestPtr->value_ > run->value_)
{
lowestPtr = run;
lowestPrevPtr = prevPtr;
}
}
if (lowestPrevPtr == NULL)
{
if (startPtr == NULL)
{
startPtr = endPtr = lowestPtr;
}
else
{
endPtr->nextPtr_ = lowestPtr;
endPtr = endPtr->nextPtr_;
}
nodePtr = nodePtr->nextPtr_;
endPtr->nextPtr_ = NULL;
}
else
{
if (startPtr == NULL)
{
startPtr = endPtr = lowestPtr;
}
else
{
endPtr->nextPtr_ = lowestPtr;
endPtr = endPtr->nextPtr_;
}
lowestPrevPtr->nextPtr_ = lowestPtr->nextPtr_;
endPtr->nextPtr_ = NULL;
}
}
print(startPtr);
return(startPtr);
}
mergeSort.c
/*--------------------------------------------------------------------------*
*---- ----*
*---- mergeSort.c ----*
*---- ----*
*---- This file defines a function that implements merge-sort on ----*
*---- a linked-list of integers. ----*
*---- ----*
*---- ---- ---- ---- ---- ---- ---- ---- ---- ----*
*---- ----*
*---- ----*
*---- ----*
*--------------------------------------------------------------------------*/
#include "headers.h"
// PURPOSE: To sort the linked-list of nodes pointed to by 'nodePtr' using
// the merge sort algorithm. Returns the first node of the sorted list.
struct Node* mergeSort (struct Node* nodePtr
)
{
if ( (nodePtr == NULL) || (nodePtr->nextPtr_ == NULL) )
{
return(nodePtr);
}
struct Node* run;
struct Node* run2;
struct Node* lastPtr = NULL;
for ( run = run2 = nodePtr;
(run2 != NULL) && (run2->nextPtr_ != NULL);
lastPtr = run, run = run->nextPtr_, run2 = run2->nextPtr_->nextPtr_
);
lastPtr->nextPtr_ = NULL;
run2 = mergeSort(run);
run = mergeSort(nodePtr);
nodePtr = NULL;
lastPtr = NULL;
while ( (run != NULL) && (run2 != NULL) )
{
if (run->value_ < run2->value_)
{
if (nodePtr == NULL)
{
nodePtr = lastPtr = run;
}
else
{
lastPtr = lastPtr->nextPtr_ = run;
}
run = run->nextPtr_;
}
else
{
if (nodePtr == NULL)
{
nodePtr = lastPtr = run2;
}
else
{
lastPtr = lastPtr->nextPtr_ = run2;
}
run2 = run2->nextPtr_;
}
}
if (run == NULL)
{
if (lastPtr == NULL)
{
nodePtr = run2;
}
else
{
lastPtr->nextPtr_ = run2;
}
}
else
{
if (lastPtr == NULL)
{
nodePtr = run;
}
else
{
lastPtr->nextPtr_ = run;
}
}
return(nodePtr);
}
struct Node* mergeSortWrapper(struct Node* nodePtr
)
{
nodePtr = mergeSort(nodePtr);
print(nodePtr);
return(nodePtr);
} C programming (20 Points):
These two files need a main() to run their functions insertionSort() and mergeSortWrapper(). Then all three C files need a header file to inform them of what the others have that they need, including Node.h which defines the data-structure. Please finish both the main.c and headers.h
headers.h
/*--------------------------------------------------------------------------*
*---- ----*
*---- headers.h ----*
*---- ----*
*---- This file declares common headers used through-out the ----*
*---- the singly-linked list sorting program. ----*
*---- ----*
*---- ---- ---- ---- ---- ---- ---- ---- ---- ----*
*---- ----*
*---- Version 1a 2020 January 5 Joseph Phillips ----*
*---- ----*
*--------------------------------------------------------------------------*/
#include
#include
#include "Node.h"
// YOUR CODE HERE
WHAT GOES HERE
Node.h
/*--------------------------------------------------------------------------*
*---- ----*
*---- Node.h ----*
*---- ----*
*---- This file declares the struct that stores an integer and ----*
*---- a next-pointer to implement a node in a singly-linked list. ----*
*---- ----*
*---- ---- ---- ---- ---- ---- ---- ---- ---- ----*
*---- ----*
*---- Version 1a 2020 January 5 Joseph Phillips ----*
*---- ----*
*--------------------------------------------------------------------------*/
struct Node
{
int value_;
struct Node* nextPtr_;
};
main.c
/*--------------------------------------------------------------------------*
*---- ----*
*---- main.c ----*
*---- ----*
*---- This file defines the main functions for a program that ----*
*---- sorts a linked list of randomly-generated integers. ----*
*---- ----*
*---- ---- ---- ---- ---- ---- ---- ---- ---- ----*
*---- ----*
*---- ----*
*---- ----*
*--------------------------------------------------------------------------*/
#include "headers.h"
#define TEXT_LEN 256
#define NUM_NUMBERS (2*65536)
const int numNumbers = NUM_NUMBERS;
// PURPOSE: To create and return the address of the first node of a linked
// list of 'length' struct Node instances, each with a randomly-generated
// integer in its 'value_' member variable.
struct Node* createList (int length
)
{
if (length == 0)
{
return(NULL);
}
struct Node* startPtr = (struct Node*)malloc(sizeof(struct Node));
struct Node* endPtr = startPtr;
startPtr->value_ = rand() % 4096;
startPtr->nextPtr_ = NULL;
for (length--; length > 0; length--)
{
endPtr->nextPtr_ = (struct Node*)malloc(sizeof(struct Node));
endPtr->nextPtr_->value_ = rand() % 4096;
endPtr->nextPtr_->nextPtr_ = NULL;
endPtr = endPtr->nextPtr_;
}
return(startPtr);
}
// PURPOSE: To print integer values in the linked list pointed to by
// 'nodePtr'. No return value.
void print (const struct Node* nodePtr
)
{
I've already written this code
}
// PURPOSE: To 'free()' the 'struct Node' instances of the linked list
// pointed to by 'nodePtr'. No return value.
void freeList (struct Node* nodePtr
)
{
struct Node* nextPtr;
for ( ; nodePtr != NULL; nodePtr = nextPtr)
{
nextPtr = nodePtr->nextPtr_;
free(nodePtr);
}
}
// PURPOSE: To run this program. Ignores command line arguments. Returns
// 'EXIT_SUCCESS' to OS.
int main ()
{
int choice;
struct Node* nodePtr = createList(numNumbers);
print(nodePtr);
do
{
char text[TEXT_LEN];
printf
("How do you want to sort %d numbers?\n"
"(1) Insertion sort\n"
"(2) Merge sort\n"
"Your choice (1 or 2)? ",
NUM_NUMBERS
);
fgets(text,TEXT_LEN,stdin);
choice = strtol(text,NULL,10);
}
while ( (choice < 1) || (choice > 2) );
switch (choice)
{
case 1 :
nodePtr = insertionSort(nodePtr);
break;
case 2 :
nodePtr = mergeSortWrapper(nodePtr);
break;
}
freeList(nodePtr);
return(EXIT_SUCCESS);
} header.h contains all the header files your code needs.
Here are some header files your code needs:
#include <stdio.h> // for printf(), sizeof(), fgets()
#include <stdlib.h> // for rand(), malloc(), free(), strtol()
#include "Node.h"
#include "insertionSort.c" // these two header files are needed by the main function for various functions
#include "mergeSort.c"
Hope this helps.
Please rate the answer if you like it.
Do leave a comment.An suggestion/query is much appreciated.
Trying to figure out what needs to be in the headers.h file. I have written the...
The source code I have is what I'm trying to fix for the assignment at the bottom. Source code: //Group 1 - Jodie Butterworth, Brandon Kidd, Matt Heckler //11-21-19 //CSC 201 // Exercise to practice the basic manipulations of a linked list // Note: Uses nullptr, so need to make sure your compiler is set to use C++11 // In Code::Blocks this is under Settings==>Compiler #include <iostream> using namespace std; // Structure to contain data for a node (Could be...
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...
Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...
Must be written and C, and compile with MinGW. Thank
you!
am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...
can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...
Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...
Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() function. Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...
Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...
c++
File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file contains the implementation file for the 2170_10_7b.h header file. This implementation uses a dynamic linked list structure implementing the // Multi ListClass object. The implementation uses nodes which have two pointer fields one to point to the next record by name (nextName) and one to point Ito the next record by account number(nextNum). The linked list is also maintained I with a dummy header...