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

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"); }
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...
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...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed … what you did to fix it. import java.util.Scanner; public static void main(String[] args) { // This program Converts grade Points into a Letter grade. int gradePoints == 00; // Declare variable and assign initial value System.out.printl("Enter Grade Points: "; //Input gradePoints; if ( gradePoints >= -42 ) { System.out.println("Grade = A"); } // if true, then ends here, if false drop to next...
fix all errors in code below: PLEASE DO ASAP, THIS IS IN MATLAB %Welcome Message fprintf('****Welcome...
fix all errors in code below: PLEASE DO ASAP, THIS IS IN MATLAB %Welcome Message fprintf('****Welcome to Comida Mexicana De Mentiras****\n'); flag =0; while flag ==1 % Asking the user for the order choice = input('Please enter your order \n 1. Burrito Bowl \n 2.Burrito \n 3. Tacos \n Enter 1 or 2 or 3: ','s'); switch choice case choice==1 % For Burrito Bowl BaseCost = 4; [Protein, PC] = proteinChoice(); [FinalAmount,~]= AmountCalculator(BaseCost,PC); displayMessage("Burrito Bowl",Protein,FinalAmount); case choice==2 end end %...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T,...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T, size> extends Throwable { private final T[] S = null ; public StackException(String s) { } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); int top = 0; return S[top]; } private boolean isEmpty() { return false; } public T pop() throws StackException { T item; if (isEmpty()) throw new StackException("Stack underflow."); int top = 0; item = S[top];...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT