In: Computer Science
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");
}
#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 */