Question

In: Computer Science

Please Correct this Code. DO NOT, I repeat DO NOT edit the struct definitions and createNode...

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;
}

Solutions

Expert Solution

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;
}

Related Solutions

Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
use repil.it edit my code please i already did all part but need to edit more...
use repil.it edit my code please i already did all part but need to edit more its run for some not shwing all intro to C-programin be sure to edit on my code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For this project you need to create at least two files: proj1.cpp, and makefile. Both files should be placed in the proj1 directory. The file proj1.cpp should contain the main function, int main(). In the main() function, the program should read the input until it reaches the end, counting the number of times each word, number, and character is used. A word is defined as a sequence of letters ('a'..'z' or 'A'..'Z')....
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
Please match the correct definitions withe the correct terms. Part A Descriptions Terms This base, or...
Please match the correct definitions withe the correct terms. Part A Descriptions Terms This base, or foundational, interest rate is the rate that banks charge on large loans to their most creditworthy business borrowers; rates charged to other, riskier, customers are scaled up from this rate. Accruals    A legal claim against a borrowing firm’s entire inventory created to secure a loan in which the borrower retains control over the inventory and can sell items without the lender’s permission. Blanket...
Is my writing for this email correct? I mean academy and grammar. (you can edit and...
Is my writing for this email correct? I mean academy and grammar. (you can edit and add any sentence) Dear sir/madam, UK Visas and Immigration I hope you are doing well. I am working hard to provide evidence from The University of Sydney confirming my new admission dates commencing within 3 months according to your e-mail request on October 2, 2020. Noting that my Visa Application( GWF0565848 ) was to get an alternate visa with new entry dates because I...
Is my writing for this email correct? I mean academy and grammar. (you can edit and...
Is my writing for this email correct? I mean academy and grammar. (you can edit and add any sentence) Dear Prof. Joseph, I hope you are doing well. As you know that the circumstances of the Corona pandemic caused the borders to close and the suspension of international flights, which led to my delay in coming to my mission headquarters in Britain, in addition to that the University of Manchester has been largely closed to students since March 2020. I...
Is my writing for this email correct? I mean academy and grammar. (you can edit and...
Is my writing for this email correct? I mean academy and grammar. (you can edit and add any sentence) I hope you will help me in drafting this e-mail Dear Prof. Joseph. I hope you are doing well. As you know, I received an email from the decision-making center stating that it is necessary to update the Academic Technology Approval Scheme (ATAS) certificate which ended on July 15,2020. Consequently, I have applied for the ATAS online to UKVI on October...
please correct the error and fix this code: (i need this work and present 3 graphs):...
please correct the error and fix this code: (i need this work and present 3 graphs): Sampling_Rate = 0.00004; % which means one data point every 0.001 sec Total_Time = 0:Sampling_Rate:1; % An array for time from 0 to 1 sec with 0.01 sec increment Omega = 49.11; % in [rad/s] zeta=0.0542; %unitless Omega_d=49.03; % in [rad/s] Displacement_Amplitude = 6.009; % in [mm] Phase_Angle = 1.52; % in [rad] Total_No_of_Points = length(Total_Time); % equal to the number of points in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT