a) Write a function that gets a stack of ints and returns the number of elements in it. When the function returns, the stacks must be in their initial state. // returns the size of the stack int stack_size(stack_t* s) b) Write a function that gets two stacks of ints and checks if they are equal (i.e., have the same elements in the same order). When the function returns, the stacks must be in their initial state. // checks if the two stacks are equal bool stack_equal(stack_t* s1, stack_t* s2) c) Write a function that gets a stack of chars and returns the string consisting of the chars in it. When the function returns, the stack must be in its initial state. // converts stack to a string // For example, suppose we push 'A', then 'B', and then 'C'. The function needs to return the string "ABC". char* stack_to_string(stack_t* s) ** Remember: you should only use the provided interface, and not assume that stack_t is implemented in a certain way. /* Question 1 */ // returns the size of the stack int stack_size(stack_t* s); // checks if the two stacks are equal bool stack_equal(stack_t* s1, stack_t* s2); // converts stack to a string // For example, suppose we push 'A', then 'B', and then 'C'. // The function needs to return the string "ABC". char* stack_to_string(stack_t* s);
we get a stack of chars. The implementation is given in a separate file. You should not make assumptions about the exact implementation details. You may only use the following functions to access the stack.
typedef struct {
// not known
} stack_t;
// creates a new stack
stack_t* stack_create();
// pushes a given item to the stack
void stack_push(stack_t* s, char item);
// pops the top element from the stack
// Pre condition: stack is not empty
char stack_pop(stack_t* s);
// checks if the stack is empty
bool stack_is_empty(stack_t* s);
// frees the stack
void stack_free(stack_t* s);
a) Write a function that gets a stack of ints and returns the number of elements in it. When the function returns, the stacks must be in their initial state.
// returns the size of the stack
int stack_size(stack_t* s)
b) Write a function that gets two stacks of ints and checks if they are equal (i.e., have the same elements in the same order). When the function returns, the stacks must be in their initial state.
// checks if the two stacks are equal
bool stack_equal(stack_t* s1, stack_t* s2)
c) Write a function that gets a stack of chars and returns the string consisting of the chars in it. When the function returns, the stack must be in its initial state.
// converts stack to a string
// For example, suppose we push 'A', then 'B', and then
'C'. The function needs to return the string "ABC".
char* stack_to_string(stack_t* s)
** Remember: you should only use the provided interface, and not assume that stack_t is implemented in a certain way.
test function for the question:
Trending now
This is a popular solution!
Step by step
Solved in 3 steps