Hey This question has been asked here multiple times still all of the current answers result in some form of valgrind er

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Hey This question has been asked here multiple times still all of the current answers result in some form of valgrind er

Post by answerhappygod »

Hey
This question has been asked here multiple times still all ofthe current answers result in some form of valgrind error, which inthis case isn’t allowed. The question is following:
Hey This Question Has Been Asked Here Multiple Times Still All Of The Current Answers Result In Some Form Of Valgrind Er 1
Hey This Question Has Been Asked Here Multiple Times Still All Of The Current Answers Result In Some Form Of Valgrind Er 1 (60.64 KiB) Viewed 38 times
F or this question we are given three files Main.c, queue.cand queue.h. From these we are only allowed to modify queue.c.
Main.c:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "queue.h"
int main(void)
{
struct studentqueue q = { NULL, NULL };
int go_on = 1;
char buffer[100];
while(go_on) {
printf("Enter name of the student (\"-\" will end reading):");
scanf("%99s", buffer);
buffer[99] = 0;
if (strlen(buffer) > 0 && strcmp(buffer, "-")) {
go_on = enqueue(&q, buffer);
} else {
go_on = 0;
}
}
while(dequeue(&q, buffer, 100)) {
printf("Fetched %s from queue\n", buffer);
}
return 0;
}
Queue.h:
#ifndef AALTO_C_QUEUE_H
#define AALTO_C_QUEUE_H
struct studentqueue {
char *name; // Name of queue member (dynamicallyallocated)
struct studentqueue *next; // (pointer to nextqueue member)
};
int enqueue(struct studentqueue *q,const char *name);
int dequeue(struct studentqueue *q, char*buffer, unsigned int size);
#endif
Queue.c
#include "queue.h"
int enqueue(struct studentqueue *q,const char*name){
}
int dequeue(struct studentqueue *q,char*buffer,unsigned int size){
}
Closest to finishing this task I have come with answer posted tohttps://www.answers.com/homework-help/questions-a ... -q93894825:
#include "queue.h"
#include <stdlib.h>
#include <string.h>
int enqueue(struct studentqueue *q,const char *name){
struct studentqueue *stud=(structstudentqueue*)malloc(sizeof(struct studentqueue));
if(stud==NULL)
return 0;
int len=strlen(name);
stud->name=(char*)malloc(sizeof(char)*(len+1));
int i=0;
for(i=0;i<len;i++)
stud->name=name;
stud->name='\0';
//stud->name=name;
stud->next=NULL;
if(q->next==NULL){
q->next=stud;
return 1;
}
struct studentqueue* temp=q->next;
while(temp->next!=NULL)
temp=temp->next;
temp->next=stud;
return 1;
}
//dequeue
int dequeue(struct studentqueue *q,char *buffer,unsigned intsize){
if(q->next==NULL)
return 0;
struct studentqueue *temp=q->next;
unsigned int len=strlen(temp->name);
if(size<len+1)
return 0;
unsigned int i;
for(i=0;i<len;i++)
buffer=temp->name;
buffer='\0';
q->next=q->next->next;
free(temp);
return 1;
}
Even if it does make program work it results in followingvalgrind error with command “valgrind --leak-check=full ./main”that I haven’t been able to fix:

Hey This Question Has Been Asked Here Multiple Times Still All Of The Current Answers Result In Some Form Of Valgrind Er 2
Hey This Question Has Been Asked Here Multiple Times Still All Of The Current Answers Result In Some Form Of Valgrind Er 2 (40.15 KiB) Viewed 38 times
Any help would be greatly appreciated :)
In this task you are required to implement a student queueing system using a linked list. The queue stores the name of the students as strings. The length of the students is not limited. The nodes of the linked list are are composed of single character pointer value member, and the next pointer as shown below. The linked list is organized in such a way that the value of the head node, the name of the student, is nulL. When a new student is added to the queue, the next pointer of the head node is modified to point to the new node, but the new node become the last node since its next pointer will be is empty, both value and next pointers are null. A two student queue is visualized below. You are required to implement two functions. 1. Define a function to add a student to the queue. - The complete function declaration is int enqueue(struct studentqueue ∗q, const char ∗ name); where is the head node of the queue is the name of the student that will be added to the queue The function returns 0 if the memory allocations fail; 1 if the student can be added to the queue. Hint This function should allocate memory for the linked list node, and its value member. 2. Define a function to remove the first student from the queue. - The complete function declaration is int dequeue(struct studentqueue ∗q, char ∗ buffer, unsigned int size);
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply