C Programming: Can sombody help me fix the errors in this program pls. Here is t
ID: 3856440 • Letter: C
Question
C Programming:
Can sombody help me fix the errors in this program pls.
Here is the code:
---------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#ifdef _MSC_VER
#include <crtdbg.h> // needed to check for memory leaks (Windows only!)
#endif
#define NUM_STU 6
typedef struct
{
char name[31];
int midterm[2];
int final;
} STUDENT;
typedef struct node NODE;
struct node
{
STUDENT data;
struct node next;
};
void printStu(const STUDENT *pStu);
NODE *push(NODE *stack, const STUDENT *pStu);
NODE *pop(NODE **stack);
void enqueue(NODE **queue, NODE **rear, const STUDENT *pStu);
NODE *dequeue(NODE **queue, NODE **rear);
int main (void)
{
STUDENT stuList[NUM_STU] =
{
{"Taylor, Noah", {85, 94}, 92},
{"Smith, Olivia", {91, 89}, 86},
{"Brown, Liam", {87, 88}, 90},
{"Davis, Emma", {96, 88}, 97},
{"Garcia, Mason", {79, 93}, 92},
{"Lopez, Sophia", {83, 78}, 95}
};
NODE *stack;
NODE *top = NULL;
NODE *queue = NULL, *rear = NULL;
NODE front;
int i, n, count = 4;
// build stack and queue with data from an array of STUDENT structures
srand((unsigned int)time(NULL));
for ( n = 0; n < count; n++)
{
i = rand() % NUM_STU;
push(stack, &stuList[i]);
enqueue(&queue, &rear, stuList[i]);
}
// display stack
printf("STACK contents from top to bottom: ");
while ((top = pop(stack))) // top != NULL
{
printStu(&top->data);
}
printf(" ");
// display queue
printf("QUEUE contents from front to rear: ");
while ((front = dequeue(queue, rear))) // front != NULL
{
printStu(&front->data);
}
printf(" ");
#ifdef _MSC_VER
printf( _CrtDumpMemoryLeaks() ? "Memory Leak " : "No Memory Leak ");
#endif
return 0;
}
/***************************************************
Displays the fileds of a STUDENT structure
Pre pStu - a pointer to a STUDENT structure
Post
*/
void printStu(const STUDENT *pStu)
{
printf("%-30s %4d %4d %4d ",
pStu->name,
pStu->midterm[0],
pStu->midterm[1],
pStu->final);
}
/***************************************************
Stack Insert: insert in the beginning
*/
NODE *push(NODE *stack, const STUDENT *pStu)
{
NODE *pnew;
pnew = (NODE *) malloc(sizeof (NODE));
if (!pnew)
{
printf("... error in push! ");
exit(1);
}
pnew->data = *pStu;
pnew->next = stack;
stack = pnew;
return stack;
}
/***************************************************
Stack Delete: delete the first node
*/
NODE *pop(NODE **stack)
{
NODE *first;
if (*stack == NULL) return NULL;
first = *stack;
*stack = (*stack)->next;
first->next = NULL;
return first;
}
/***************************************************
Queue Insert: insert at the end
*/
void enqueue(NODE **queue, NODE **rear, const STUDENT *pStu)
{
NODE *pnew;
pnew = (NODE *) malloc(sizeof (NODE));
if (!pnew)
{
printf("... error in enqueue! ");
exit(1);
}
pnew->data = *pStu;
pnew->next = NULL;
if (*queue == NULL) *queue = pnew;
else (*rear)->next = pnew;
*rear = pnew;
return;
}
/***************************************************
Queue Delete: remove the first node
*/
NODE *dequeue(NODE **queue, NODE **rear)
{
NODE *first;
if (*queue == NULL) return NULL;
first = *queue;
*queue = (*queue)->next;
if (*queue == NULL) *rear = NULL;
first->next = NULL;
return first;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#ifdef _MSC_VER
#include <crtdbg.h> // needed to check for memory leaks (Windows only!)
#endif
#define NUM_STU 6
typedef struct
{
char name[31];
int midterm[2];
int final;
} STUDENT;
typedef struct node NODE;
struct node
{
//STUDENT data; ERROR #1
STUDENT *data;
//struct node next; ERROR #2
struct node *next;
};
void printStu(const STUDENT *pStu);
NODE *push(NODE *stack, const STUDENT *pStu);
NODE *pop(NODE **stack);
void enqueue(NODE **queue, NODE **rear, const STUDENT *pStu);
NODE *dequeue(NODE **queue, NODE **rear);
int main (void)
{
STUDENT stuList[NUM_STU] =
{
{"Taylor, Noah", {85, 94}, 92},
{"Smith, Olivia", {91, 89}, 86},
{"Brown, Liam", {87, 88}, 90},
{"Davis, Emma", {96, 88}, 97},
{"Garcia, Mason", {79, 93}, 92},
{"Lopez, Sophia", {83, 78}, 95}
};
//NODE *stack; ERROR #3
NODE *stack = NULL;
NODE *top;
NODE *queue = NULL, *rear = NULL;
NODE *front;
int i, n, count = 4;
// build stack and queue with data from an array of STUDENT structures
srand((unsigned int)time(NULL));
for ( n = 0; n < count; n++)
{
i = rand() % NUM_STU;
//push(stack, &stuList[i]); ERROR #4
stack = push(stack, &stuList[i]);
//enqueue(&queue, &rear, stuList[i]); ERROR #5
enqueue(&queue, &rear, &stuList[i]);
}
// display stack
printf("STACK contents from top to bottom: ");
//top = (NODE *) malloc(sizeof(NODE));
//if (!top) printf("... error! "), exit(1); ERROR #6 (uneccessary to dynamically allocate a node here)
//while ((top = pop(stack))) ERROR #7
while ((top = pop(&stack))) // top != NULL
{
//printStu(&top->data); ERROR #8
printStu(top->data);
free(top); // ERROR #9 (added this line to resolve memory leak)
}
printf(" ");
// display queue
printf("QUEUE contents from front to rear: ");
//while ((front = dequeue(queue, rear))) ERROR #10
while (front = dequeue(&queue, &rear)) // front != NULL
{
//printStu(&front->data); ERROR #11
printStu(front->data);
free(front); // ERROR #12 (added this line to resolve memory leak)
}
printf(" ");
#ifdef _MSC_VER
printf( _CrtDumpMemoryLeaks() ? "Memory Leak " : "No Memory Leak ");
#endif
system("pause"); //ERROR? Can't read the output to the screen without a pause
return 0;
}
/***************************************************
Displays the fileds of a STUDENT structure
Pre pStu - a pointer to a STUDENT structure
Post
*/
void printStu(const STUDENT *pStu)
{
printf("%-30s %4d %4d %4d ",
pStu->name,
pStu->midterm[0],
pStu->midterm[1],
pStu->final);
}
/***************************************************
Stack Insert: insert in the beginning
*/
NODE *push(NODE *stack, const STUDENT *pStu)
{
NODE *pnew;
pnew = (NODE *) malloc(sizeof (NODE));
if (!pnew)
{
printf("... error in push! ");
exit(1);
}
//pnew->data = *pStu; Error #13
pnew->data = pStu;
pnew->next = stack;
stack = pnew;
return stack;
}
/***************************************************
Stack Delete: delete the first node
*/
NODE *pop(NODE **stack)
{
NODE *first;
if (*stack == NULL) return NULL;
first = *stack;
*stack = (*stack)->next;
first->next = NULL;
return first;
}
/***************************************************
Queue Insert: insert at the end
*/
void enqueue(NODE **queue, NODE **rear, const STUDENT *pStu)
{
NODE *pnew;
pnew = (NODE *) malloc(sizeof (NODE));
if (!pnew)
{
printf("... error in enqueue! ");
exit(1);
}
//pnew->data = *pStu; ERROR #14
pnew->data = pStu;
pnew->next = NULL;
if (*queue == NULL) *queue = pnew;
else (*rear)->next = pnew;
*rear = pnew;
return;
}
/***************************************************
Queue Delete: remove the first node
*/
NODE *dequeue(NODE **queue, NODE **rear)
{
NODE *first;
if (*queue == NULL) return NULL;
first = *queue;
*queue = (*queue)->next;
if (*queue == NULL) *rear = NULL;
first->next = NULL;
return first;
}
/* ================= Sample Output ================= */
/* Results:
STACK contents from top to bottom:
Davis, Emma 96 88 97
Lopez, Sophia 83 78 95
Smith, Olivia 91 89 86
Lopez, Sophia 83 78 95
QUEUE contents from front to rear:
Lopez, Sophia 83 78 95
Smith, Olivia 91 89 86
Lopez, Sophia 83 78 95
Davis, Emma 96 88 97
No Memory Leak
Press any key to continue . . .
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.