Answered You can hire a professional tutor to get the answer.
Hello. I was having difficulty in implementing code to reverse the list of inserted numbers from a file. I am not sure how to reverse the list using...
Hello. I was having difficulty in implementing code to reverse the list of inserted numbers from a file. I am not sure how to reverse the list using a recursive function. Also, I cannot make a copy of the list to make a duplicate or external input list. The command I have to use is the "r" command. Here is the code I have so far. We can check it works by using the "p" function to see if it actually printed all the numbers in reverse. Thank you so much!
* The objective of this lab is to add the following
* functionality to the code shown below.
* 1) Insert a node after a specific data element in the
* list. (This was implicit in the lecture this after-
* noon. It is now explicit.)
* 2) Remove a node from the list.
*
* Note that both the insert and the removal (deletion)
* would require that a node has been specified or
* selected for insertion or removal.
* Therefore the following functions would be needed.
* A) Find, where the node's data would match the input
* data.
* B) There is an implication that the input file would
* be able to accept an insert, find, and delete command
* a specific node based on the input data value.
* C) The input file specification should therefore be
* modified to support the following input data format.
*
* Input data format:
** command value
** where a command is identified as follows:
** - i integerValue // i is the insert command
** // where the node is inserted
** // in a new node AFTER having
** // "found" the node specified
** // in the preceding "find"
** - f integerValue // f is the find command
** - d integerValue // d is the delete command
** - p // p is the printAll command
*
* The function prototypes are:
-> struct node * insert(struct node * start, int dataAfter, int newData);
*** returns the inserted node's address is successful
*** NULL address if it fails.
**** inserts newData in the created slot following first dataAfter
**** if no dataAfter exists, add to the bottom of the list
-> struct node * find(struct node * start, int data)
*** returns the found node's address is successful
*** NULL address if it fails to find the node.
-> int delete(struct node * start, int data);
*** returns 0 if successful
*** 1 if it fails.
* See the sample input statements at the end of the source file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int data;
struct node *next;
} node;
struct node * addToList(struct node *base, int data)
{
struct node *newNode = NULL;
struct node *temp = base;
if (base == NULL)
{
base = malloc( sizeof(struct node));
// malloc defend
base->data = data;
base->next = NULL;
return base;
}
//Find the bottom of the list starting from the base
while (base->next != NULL)
{
base = base->next;
}
//Now at the end of the list
newNode = malloc( sizeof(struct node)); //get memory for new node
//Defend against bad malloc
base->next = newNode; // add the new node to bottom of the list
newNode->data = data; // slap the data into the list
newNode->next = NULL; // terminate the list by setting next to NULL
return temp; //return the new end of the list to the caller
//Shouldnt we return newNode?
}
/*
* Walk the list and print the data
*/
void printList(struct node *list)
{
fprintf(stdout,"data: ");
while (list != NULL)
{
fprintf(stdout, "%3d ", list->data);
list = list->next;
}
fprintf(stdout,"n");
return;
}
struct node* find(struct node* start, int data)
{
struct node* temp = start;
while (temp != NULL)
{
if (temp->data == data)
{
return temp;
}
else
{
temp = temp->next;
}
}
return NULL;
}
/*
Insert a node after the given dataAfter.
*/
struct node* insert(struct node* start, int dataAfter, int newData)
{
// If the start node is NULL, insert the data at the start node.
if (start == NULL)
{
start = malloc(sizeof(struct node));
start->data = newData;
start->next = NULL;
}
else
{
// Search for the dataAfter node in the list.
struct node* temp = find(start, dataAfter);
if (temp == NULL)
{
// dataAfter node is not found. Insert the newData at the end of the list.
start = addToList(start, newData);
}
else
{
struct node* newNode = malloc(sizeof(struct node));
newNode->data = newData;
newNode->next = temp->next;
temp->next = newNode;
}
}
return start;
}
int delete(struct node* start, int data)
{
if (start == NULL)
{
return 0;
}
else
{
struct node* temp = start;
struct node* pointer = NULL;
while(temp != NULL)
{
if (temp->data == data)
{
pointer->next = temp->next;
free(temp);
return 1;
}
pointer = temp;
temp = temp->next;
}
return 0;
}
}
/*
* pass the input file to main, then add the data from the input file
* to the list. NB The list starts as an empty list.
*/
int main(int argc, char **argv)
{
struct node *root = NULL; // The root of the list
struct node *temp = NULL;
// struct node *base = NULL; // Placeholder for current end of the list
char inBuf[100]; // input buffer
int data = 0;
FILE * ifp;
ifp = fopen(argv[1], "r"); //Get the filename from the command line
if (NULL == ifp) // Check for success
{ //Let 'em know the filename wasn't found
fprintf(stderr, "%s file not found.n", argv[1]);
return -1;
}
/*
* Read the file, then add the data to the list
* All the while keep track of the last added node in temp
*/
while (fgets(inBuf, 100, ifp) != NULL)
{
char c = inBuf[0];
char* token = strtok(inBuf," ");
if (c == 'i')
{
token = strtok(NULL," ");
char* token2 = strtok(NULL," ");
if (token2 == NULL)
{
root = insert(root,-1,atoi(token));
}
else
{
root = insert(root,atoi(token),atoi(token2));
}
}
if (c == 'f')
{
token = strtok(NULL," ");
int data = atoi(token);
temp = find(root,data);
if (temp == NULL)
{
printf("Not foundn");
}
else
{
printf("Foundn");
}
}
if (c == 'd')
{
token = strtok(NULL," ");
int data = atoi(token);
if (delete(root,data) == 0)
{
printf("Node could not be deletedn");
}
else
{
printf("Node has been deletedn");
}
}
if (c == 'p')
{
printList(root);
}
}
fclose(ifp);
return 0;
}
/*
* New input file with commands. (Note the comments are for
* clarification and are NOT a requirement for this program.)
---Begin sample booyah2.txt
i 23 // insert 23
i 78 // insert 78
i 900 // insert 900 //remember if not two number the first is addToList only
i 23 42 // insert 42 after 23
p // print
f 78 // find 78
d 78 // delete 78
p // print
i 905 47 // insert 47 after 905 (failure: no afterData)
f 901 // find 901 (failure case test)
p //print
---End sample booyah2.txt
*/