Question

This is a debugging question. Please write comments where it is fixed. Thanks. q7.4 Fix the...

This is a debugging question. Please write comments where it is fixed. Thanks.

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");

}

Homework Answers

Answer #1

Fixed all the errors, the code is working fine now.

//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>
#include <stdlib.h>

struct Node{

int data;

struct Node* next; // fixed the error here by adding 'struct'

};


// Moved PrintList upwards to prevent any conflicts/warnings
void PrintList(struct Node* n){

while(n != NULL){

printf("%d ", n->data); // Changed n.data to n->data

n = n->next; //Changed n.next to n->next

}

printf("\n");

}

int main(void){

// Initialised the nodes to avoid incompatible pointer type error
struct Node* first;
first = (struct Node *) malloc( sizeof(struct Node) );
struct Node* second;
second = (struct Node *) malloc( sizeof(struct Node) );
struct Node* third;
third = (struct Node *) malloc( sizeof(struct Node) );
struct Node* fourth;
fourth = (struct Node *) malloc( sizeof(struct Node) );
struct Node* fifth;
fifth = (struct Node *) malloc( sizeof(struct Node) );

int i=0;
// Syntax node->data = i is used because node is a pointer here
// Syntax node->next = nextnode is used to assign the next node in the linked list
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);

fourth->data = i;

third->next = fourth;

scanf(" %d", &i);

fifth->data = i;

fourth->next = fifth;

PrintList(first);

}


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
debug code on C(not C+ or C++) I try to fix by myself, node part show...
debug code on C(not C+ or C++) I try to fix by myself, node part show error. This program should work for scan 5 ints from the user Using those 5 ints, it require construct a linked list of 5 elements. After this, it sould prntf of the list using the PrintList. #include <stdio.h> struct Node{    int data;    Node* next; }; int main(void){    struct Node first = {0, 0};    struct Node* second = {0, 0};   ...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8 13 21 34 55 ....... and also need add code complexity time if you want! here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[ n + 1 ]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for ( i = 2; i < n; i++ ) { fib[ i ] = fib[ i -...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning. /INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include #include #include #include typedef struct ENTRY { char name[81]; }ENTRY; typedef struct LISTREC /* LISTREC is...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
Question Rewrite the following C program using switch-case statement. and give me an output please use...
Question Rewrite the following C program using switch-case statement. and give me an output please use printf and scanf #include<stdio.h> int main(void) {      int semester;           printf("What is your Studying Level (1-8)?> ");      scanf("%d" , &semester);      if(semester == 1 || semester == 2)           printf("Your are a freshman!\n ");      else if(semester == 3 || semester == 4)           printf("Your are sophomore!\n ");      else if(semester == 5 || semester == 6)           printf("Your are...