In: Computer Science
Please Correct this Code. DO NOT, I repeat DO NOT edit the struct definitions and createNode Function because our prof said we shouldn't touch that part at all (I also staled that in the code). You can edit the others as far as it works. The task is to reverse 10 numbers in linked list using recursion. The 10 numbers should be inputted by the user. Please add comments to the edited code.
#include <stdio.h>
#include <stdlib.h>
//TASK: Reverse 10 numbers of linked list using recursion.
//Struct Definitions (DO NOT EDIT THIS PART)
typedef struct node { // (DO NOT EDIT THIS PART)
void* dataPtr; //(DO NOT EDIT THIS PART)
struct node* link; //(DO NOT EDIT THIS PART)
} NODE; // (DO NOT EDIT THIS PART)
//create NODE (DO NOT EDIT THIS PART)
NODE* createNode(void* itemPtr) { //(DO NOT EDIT THIS PART)
NODE* nodePtr; //(DO NOT EDIT THIS PART)
nodePtr = (NODE*) malloc (sizeof (NODE)); //(DO NOT EDIT THIS
PART)
nodePtr->dataPtr = itemPtr; //(DO NOT EDIT THIS PART)
nodePtr->link = NULL; //(DO NOT EDIT THIS PART)
return nodePtr; //(DO NOT EDIT THIS PART)
}
struct node* head;
void reverse(NODE* prev, NODE* curr) {
if (!curr){
return;
}
if (!curr->link) {
head=curr;
curr->link=prev;
return;
}
reverse(curr, curr->link);
curr->link=prev;
}
void display() {
struct node* value;
value = head;
while(value!=NULL) {
printf("%d ",value->dataPtr);
value=value->link;
}
printf("\n");
}
int main()
{
NODE* head=NULL;
int x;
printf("Enter 10 numbers to reverse ");
for(int i=0; i<9; i++) {
scanf(" %d ",&x);
}
head=createNode(x);
reverse(NULL, head); //reverse list recursively
printf("Reverse Linked List is ");
display();
return 0;
}
Here is the modified code. Firstly, stodring void * in the node makes it difficult to store different address for each input value, so we have to maintain an array to hold different address for different input. Secondly, you condition for reversing the list was correct and needed no changes. The part of storing and referencing and printing the data required changes. You can see that I have stored the input data in an array, passed the address of the element to add to the linked list. Also print the data using *(int *)node->data . These are most of the changes. The rest is working fine. Also, remember to enter according to the input format expected. Thirdly, your loop was running from 0 to <9 that denotes 9 inputs, which I changed to <=9.
#include <stdio.h>
#include <stdlib.h>
//TASK: Reverse 10 numbers of linked list using recursion.
//Struct Definitions (DO NOT EDIT THIS PART)
typedef struct node
{ // (DO NOT EDIT THIS PART)
void *dataPtr; //(DO NOT EDIT THIS PART)
struct node *link; //(DO NOT EDIT THIS PART)
} NODE; // (DO NOT EDIT THIS PART)
//create NODE (DO NOT EDIT THIS PART)
NODE *
createNode (void *itemPtr)
{ //(DO NOT EDIT THIS PART)
NODE *nodePtr; //(DO NOT EDIT THIS PART)
nodePtr = (NODE *) malloc (sizeof (NODE)); //(DO NOT EDIT THIS PART)
nodePtr->dataPtr = itemPtr; //(DO NOT EDIT THIS PART)
nodePtr->link = NULL; //(DO NOT EDIT THIS PART)
return nodePtr; //(DO NOT EDIT THIS PART)
}
struct node *head;
void
reverse (NODE * prev, NODE * curr)
{
if (!curr)
{
return;
}
if (!curr->link)
{
head = curr;
curr->link = prev;
return;
}
reverse (curr, curr->link);
curr->link = prev;
}
void
display ()
{
struct node *value;
value = head;
while (value != NULL)
{
printf ("%d ", *(int *)value->dataPtr);
value = value->link;
}
printf ("\n");
}
int
main ()
{
head = NULL;
int *x = malloc(sizeof(int)*10);
printf ("Enter 10 numbers to reverse ");
for (int i = 0; i <= 9; i++)
{
scanf (" %d ", &x[i]);
if(head == NULL) head = createNode(&x[i]);
else{
NODE *temp = head;
while(temp->link != NULL) temp = temp->link;
NODE *newNode = createNode(&x[i]);
temp->link = newNode;
}
// printf(" %d ", x[i]);
}
// display();
//head = createNode (x);
reverse (NULL, head); //reverse list recursively
printf ("Reverse Linked List is ");
display ();
return 0;
}