Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

help me solve this in the C language . What are the errors in the code and how do i fix and implement them? Pointers,

help me solve this in the C language . What are the errors in the code and how do i fix and implement them?

Pointers, Stacks & Queues There are a number of errors (about 10) in the following program. Locate all errors, fix them (as shown below), run the program and save its output as a comment at the end of the source file. Here is an example: int num = 10; int *ptr; num = &ptr; // <== Error: Comment the line and write the correct line below // num = &ptr; // Error #1 ptr = # ********************************************************** ***************************************************************************/ #include #include #include #include

#ifdef _MSC_VER #include // needed to check for memory leaks (Windows only!) #endif

#define NUM_CLS 9

typedef struct { char course[10]; int noSections; int sections[16]; } CIS_CLASS;

typedef struct node NODE; struct node { CIS_CLASS data; struct node next; };

void printClass(const CIS_CLASS *pStu); NODE *push(NODE *stack, const CIS_CLASS *pStu); NODE *pop(NODE **stack); void enqueue(NODE **queue, NODE **rear, const CIS_CLASS *pStu); NODE *dequeue(NODE **queue, NODE **rear);

int main (void) { CIS_CLASS clsList[NUM_CLS] = { {"CIS 35A", 2, {61, 63}}, {"CIS 35B", 1, {62}}, {"CIS 41A", 3, {1, 2, 61}}, {"CIS 28", 1, {61}}, {"CIS 22C", 4, {3, 4, 61, 63}}, {"CIS 26B", 1, {61}}, {"CIS 22B", 8, {1, 2, 3, 4, 6, 61, 62, 63}}, {"CIS 29", 1, {61}}, {"CIS 22A", 8, {1, 3, 5, 6, 7, 8, 61, 63}}, }; 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 CIS_CLASS structures srand((unsigned int)time(NULL)); for ( n = 0; n < count; n++) { i = rand() % NUM_CLS; push(stack, &clsList[i]); enqueue(&queue, &rear, clsList[i]); } // display stack printf("STACK contents from top to bottom: "); while ((top = pop(stack))) // top != NULL { printClass(&top->data); } printf(" "); // display queue printf("QUEUE contents from front to rear: "); while ((front = dequeue(queue, rear))) // front != NULL { printClass(&front->data); } printf(" "); #ifdef _MSC_VER printf( _CrtDumpMemoryLeaks() ? "Memory Leak " : "No Memory Leak "); #endif return 0; } /*************************************************** Displays the fileds of a CIS_CLASS structure Pre pCls - a pointer to a CIS_CLASS structure Post */ void printClass(const CIS_CLASS *pCls) { printf("%-10s %2d ", pCls->course, pCls->noSections); } /*************************************************** Stack Insert: insert in the beginning */ NODE *push(NODE *stack, const CIS_CLASS *pCls) { NODE *pnew; pnew = (NODE *) malloc(sizeof (NODE)); if (!pnew) { printf("... error in push! "); exit(1); } pnew->data = *pCls; 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 CIS_CLASS *pCls) { NODE *pnew; pnew = (NODE *) malloc(sizeof (NODE)); if (!pnew) { printf("... error in enqueue! "); exit(1); } pnew->data = *pCls; 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: */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions