Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***Please help debug the C code below so that it compiles*** / /- Implement each of the functions to create a working stack. // -

***Please help debug the C code below so that it compiles***

/ /- Implement each of the functions to create a working stack. // - Do not change any of the function declarations // - (i.e. stack_t* create_stack() should not have additional arguments) // - You should not have any 'printf' statements in your stack functions. // - (You may consider using these printf statements to debug, but they should be removed from your final version) // ================================================== #ifndef MYSTACK_H #define MYSTACK_H

// Stores the maximum 'depth' of our stack. // Our implementation enforces a maximum depth of our stack. // (i.e. capacity cannot exceed MAX_DEPTH for any stack) # define MAX_DEPTH 32

// Create a node data structure to store data within // our stack. In our case, we will stores 'integers' typedef struct node{ int data; struct node* next; }node_t;

// Create a stack data structure // Our stack holds a single pointer to a node, which // is a linked list of nodes. typedef struct stack{ int count; // count keeps track of how many items // are in the stack. unsigned int capacity; // Stores the maximum size of our stack node_t* head; // head points to a node on the top of our stack. }stack_t;

// Creates a stack // Returns a pointer to a newly created stack. // The stack should be initialized with data on the heap. // (Think about what the means in terms of memory allocation) // The stacks fields should also be initialized to default values. stack_t* create_stack(unsigned int capacity){ // Modify the body of this function as needed. stack_t* myStack = (stack_t*)malloc(sizeof(stack_t)); myStack -> capacity = capacity; myStack ->count = 0; myStack -> head = NULL;

return myStack; }

// Stack Empty // Check if the stack is empty // Returns 1 if true (The stack is completely empty) // Returns 0 if false (the stack has at least one element enqueued) int stack_empty(stack_t* s){

return (s -> head == NULL; (s-> count == 0): }

// Stack Full // Check if the stack is full // Returns 1 if true (The Stack is completely full, i.e. equal to capacity) // Returns 0 if false (the Stack has more space available to enqueue items) int stack_full(stack_t* s){

return (s -> head != NULL && (s-> count == s -> capacity): }

// Enqueue a new item // i.e. push a new item into our data structure // Returns a -1 if the operation fails (otherwise returns 0 on success). // (i.e. if the Stack is full that is an error, but does not crash the program). int stack_enqueue(stack_t *s, int item){ if(stack_full(s)){ return -1; // Note: you should have two return statements in this function. node_t*newNode = (node_t*)malloc(sizeof(node_t)); newNode -> data = item; newNode -> next = s -> head; s -> head = newNode; s -> count += 1; return 0: }

// Dequeue an item // Returns the item at the front of the stack and // removes an item from the stack. // Removing from an empty stack should crash the program, call exit(1). int stack_dequeue(stack_t *s){ if(stack_empty(s)){ exit(1);} int x = s -> head -> data; node_t * node = s -> head; s -> head = s -> head -> next; s -> count -= 1;

free(node) return x; // Note: This line is a 'filler' so the code compiles. }

// Stack Size // Queries the current size of a stack // A stack that has not been previously created will crash the program. // (i.e. A NULL stack cannot return the size) unsigned int stack_size(stack_t* s){ return s->count; }

// Free stack // Removes a stack and ALL of its elements from memory. // This should be called before the proram terminates. void free_stack(stack_t* s){ while(!stack_empty(s)){ stack_dequeue(s);} }

#endif

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

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

Students also viewed these Databases questions

Question

Different types of Grading?

Answered: 1 week ago

Question

Explain the functions of financial management.

Answered: 1 week ago

Question

HOW MANY TOTAL WORLD WAR?

Answered: 1 week ago

Question

Discuss the scope of financial management.

Answered: 1 week ago