Question: Stack ADT with Linked List Implementation in C language (not cpp nor c#) Your program should not contain main(). You can only write below //your
Stack ADT with Linked List Implementation in C language (not cpp nor c#)
Your program should not contain main().
You can only write below //your code here, I have finished void stack_init(), void stack_make_empty(), void stack_push(), void stack_free(), please finish the remains.
#include
struct node { char data; struct node *next; }; typedef struct node Stack;
// Return a new empty stack // An empty stack is data storing CHAR_MAX and next pointing to NULL void stack_init(Stack** s){ // Your code here *s = (Stack*) malloc(sizeof(Stack)); stack_make_empty(*s); }
// Make the stack empty // An empty stack is data storing CHAR_MAX and next pointing to NULL void stack_make_empty(Stack* s){ // Your code here s->next = NULL; s->data = CHAR_MAX; }
// Return the size of the stack int stack_size(Stack* s){ // Your code here
}
// Check if the stack is empty // Return 1 if the stack is empty // Return 0 if the stack is not empty int stack_is_empty(Stack* s){ // Your code here
}
// Push the value x to the top of the stack void stack_push(Stack* s, char x){ // Your code here Stack *t = malloc(sizeof(Stack)); t->data = x; t->next = s->next; s->next = t; }
// Return the topmost element of the stack // If the stack is empty, return 0 char stack_top(Stack* s){ // Your code here
}
// If s is not empty, pop an element from the stack // If s is empty, do nothing void stack_pop(Stack* s){ // Your code here
}
// Free the stack, if the stack is not NULL // Assign NULL to q after free void stack_free(Stack** s){ // Your code here if (*s != NULL) free(*s); *s = NULL; }
// Print the stack in the format // e.g. s: a b c d, where d is the topmost element // output: "(top) d c b a" char* stack_print(Stack* s){ // Your code here }
if you don't know what is the stack_print() about, you can refer to the finished codes in another program about queue.(just for reference) 
// Print the queue in the format // e.g. q: 0 1 2 3, where 0 is the front // output: "(front) 0 1 2 3 (rear)" char* queue_print(Queue* q){ int len, i, cnt; char *output; len = q->size*12+15; output = malloc(sizeof(char)*len); memset(output, 0, sizeof(char)*len); // Your code here sprintf(output, "(front)"); for (i = q->front, cnt = 0; cnt size; i = (i+1)%MAX_SIZE, cnt++) { sprintf(output, "%s %d", output, q->data[i]); } sprintf(output, "%s (rear)", output); return output; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
