Question

In: Computer Science

q7.4 Fix the errors in the code (in C) //This program is supposed to scan 5...

q7.4 Fix the errors in the code (in C)

//This program is supposed to scan 5 ints from the user

//Using those 5 ints, it should construct a linked list of 5 elements

//Then it prints the elements of the list using the PrintList function

#include <stdio.h>

struct Node{

int data;

Node* next;

};

int main(void){

struct Node first = {0, 0};

struct Node* second = {0, 0};

Node third = {0, 0};

struct Node fourth = {0, 0};

struct Node fifth = {0, &first};

int i;

scanf(" %d", &i);

first.data = i;

scanf(" %d", &i);

second.data = i

first.next = &second;

scanf(" %d", &i);

third.data = i;

second.next = third;

scanf(" %d", &i);

data = i;

third.next = &fourth;

scanf(" %d", &i);

fifth.data = i;

fourth->next = &fifth;

PrintList(first);

}

PrintList(struct Node* n){

while(n != 0){

printf("%d ", n.data);

n = n.next;

}

printf("\n");

}

Solutions

Expert Solution

#include <stdio.h>

struct Node{
   int data;
   struct Node *next;
};

void PrintList(struct Node* n){

   while(n != NULL){
  
       printf("%d ", n->data);
      
       n = n->next;
  
   }  
}
int main(void){
  
   // list with 5 nodes
   struct Node *first = NULL;
  
   struct Node *second = NULL;
  
   struct Node *third = NULL;
  
   struct Node *fourth = NULL;
  
   struct Node *fifth = NULL;
  
// allocate 5 nodes in the heap
   first = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
   fourth = (struct Node*)malloc(sizeof(struct Node));
   fifth = (struct Node*)malloc(sizeof(struct Node));
  
   int i;
  
   scanf(" %d", &i);
  
   first->data = i; // assign data in first node
  
   scanf(" %d", &i);
  
   second->data = i;
  
   first->next = second; // Link first node with the second node
  
   scanf(" %d", &i);
  
   third->data = i;
  
   second->next = third;
  
   scanf(" %d", &i);
  
   fourth->data = i;
  
   third->next = fourth;
  
   scanf(" %d", &i);
  
   fifth->data = i;
  
   fourth->next = fifth;
  
   fifth->next = NULL; // last node assign with null
  
   printf("List is: ");
   PrintList(first);
  
}

/* PLEASE UPVOTE */

  
/* OUTPUT */


Related Solutions

C++ Program for Project Alarm. ------------------------------------------------------------------------------ Fix ALL Errors in my EXISTING code for ALL 5...
C++ Program for Project Alarm. ------------------------------------------------------------------------------ Fix ALL Errors in my EXISTING code for ALL 5 files.   MUST add comments for FULL CREDIT. Take a screenshot of the working solution output. ------------------------------------------------------------------------------- Instructions: Use class composition to define and implement a new class called Alarm that contains a Time member variable. A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class...
q7.1 Fix the errors in the code (in C) //This program should read a string from...
q7.1 Fix the errors in the code (in C) //This program should read a string from the user and print it using a character pointer //The program is setup to use pointer offset notation to get each character of the string #include <stdio.h> #include <string.h> int main(void){ char s[1]; scanf(" %c", s); char *cPtr = s[1]; int i=0; while(1){ printf("%c", cPtr+i); i++; } printf("\n"); }
C++ : Find the syntax errors in the following program. For each syntax error, fix it...
C++ : Find the syntax errors in the following program. For each syntax error, fix it and add a comment at the end of the line explaining what the error was. #include <iostream> #include <cmath> using namespace std; int main(){ Double a, b, c; 2=b; cout<<"Enter length of hypotenuse"<<endl; cin>>c>>endl; cout>>"Enter length of a side"<<endl; cin>>a; double intermediate = pow(c, 2)-pow(a, 2); b = sqrt(intermediate); cout<<"Length of other side is:" b<<endline; return 0; }
Please fix all the errors in this Python program. import math def solve(a, b, c): """...
Please fix all the errors in this Python program. import math def solve(a, b, c): """ Calculate solution to quadratic equation and return @param coefficients a,b,class @return either 2 roots, 1 root, or None """ #@TODO - Fix this code to handle special cases d = b ** 2 - 4 * a * c disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 if...
In the assignment below please fix the errors in each code. The first code should have...
In the assignment below please fix the errors in each code. The first code should have a total of 3 erros. The second and third code should have 4 errors. Thanks //DEBUG05-01 // This program is supposed to display every fifth year // starting with 2017; that is, 2017, 2022, 2027, 2032, // and so on, for 30 years. // There are 3 errors you should find. start    Declarations       num year       num START_YEAR = 2017       num...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
Please fix all of the errors in this Python Code. import math """ A collection of...
Please fix all of the errors in this Python Code. import math """ A collection of methods for dealing with triangles specified by the length of three sides (a, b, c) If the sides cannot form a triangle,then return None for the value """ ## @TODO - add the errlog method and use wolf fencing to identify the errors in this code def validate_triangle(sides): """ This method should return True if and only if the sides form a valid triangle...
I'm supposed to create a c++ program which is supposed to take a sentence as an...
I'm supposed to create a c++ program which is supposed to take a sentence as an input and then output a sorted list of the occurrence of each letter. ex. Enter a phrase: It's a hard knock life A2 I2 K2 C1 D1 E1 F1 H1 L1 N1 O1 R1 S1 T1 I was also given a recommended code to use as a bubble sort procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped =...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
in c++ please follow instructions and fix the errors and please make a comment next to...
in c++ please follow instructions and fix the errors and please make a comment next to each code error you fix. Below are 25 code fragments, inserted into a try catch block. Each line has zero or more errors. Your task is to find and remove all errors in each fragment. Do not fix problems by simply deleting a statement; repair the problems by changing, adding, or deleting a few characters. There may be different ways to fix them You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT