# | Problem | Pass Rate (passed user / total user) |
---|---|---|
13697 | EE2310_Lab_8_1 |
|
13699 | EE2310_Lab_8_2 |
|
Description
/* dynamic stack : Please follow our exact rules otherwise you WILL LOSE POINTS */
/* dynamic push rule: if the stack is full then double the stack's size */
/* dynamic pop rule: after the pop, if the number of elements in stack < (1/4)*max_size, then shrink the stack by half. */
#include <stdio.h>
#include <stdlib.h>
#define MAX_NEG -1000000000
typedef struct stack {
int *head;
int top;
int max_size;
} stack_arr_t;
void stack_init(stack_arr_t *s, int size){
/*
* YOUR WORK HERE !
*/
}
void stack_destroy(stack_arr_t *s){
/*
* YOUR WORK HERE !
*/
}
/* DO NOT CHANGE THIS FUNCTION */
void show_stack(stack_arr_t *s) {
if(s->top==0)
printf("Stack empty!\n");
else{
printf("Stack contains %d element(s)\n", s->top);
for(int i=0; i< s->top - 1; ++i) {
printf("%d ", s->head[i]);
}
printf("%d\n", s->head[s->top - 1]);
}
printf("top = %d, max_size = %d\n", s->top,s->max_size);
}
void dynamic_push(int elem, stack_arr_t *s){
/*
* YOUR WORK HERE !
*/
}
int dynamic_pop(stack_arr_t *s){
/*
* YOUR WORK HERE !
*/
}
/*** DO NOT CHANGE MAIN!!! You will lose points if you make any change ***/
int main(){
stack_arr_t my_stack;
int input_size, temp, max_size, pop_size;
/* input stack's max size */
scanf("%d", &max_size);
stack_init(&my_stack, max_size);
/* input elements to be pushed */
scanf("\n%d", &input_size);
int i;
for(i=0; i<input_size; ++i){
scanf("\n%d", &temp);
dynamic_push(temp, &my_stack);
}
/* input number of elements to be popped */
scanf("\n%d", &pop_size);
for(i=0; i < pop_size-1; ++i){
printf("%d ", dynamic_pop(&my_stack));
}
if (0 != pop_size){
printf("%d\n", dynamic_pop(&my_stack));
}
show_stack(&my_stack);
stack_destroy(&my_stack);
return 0;
}
Input
4
5
21 3 4 5 8
4
Output
8 5 4 3
Stack contains 1 element(s)
21
top = 1, max_size = 4
Sample Input Download
Sample Output Download
Tags
Discuss
Description
/* Please follow our exact rules otherwise you WILL LOSE POINTS */
#include <stdio.h>
#include <stdlib.h>
/* DO NOT CHANGE STRUCT */
struct node{
int data;
struct node *next;
};
typedef struct node node_t;
typedef struct linked_list{
node_t *head;
node_t *tail;
} linked_list_t;
void output_list(linked_list_t *ptr_list){
/* your code here
*
*/
}
void add_node(linked_list_t *list_ptr, int data){
/* your code here
*
*/
}
/* DO NOT CHANGE MAIN */
int main(){
linked_list_t my_list;
my_list.head = NULL;
my_list.tail = NULL;
int data, input_size;
/* when you add a node, you add it at the tail */
scanf("%d\n", &input_size);
for(int i=0; i < input_size; ++i){
scanf("%d", &data);
add_node(&my_list, data);
}
output_list(&my_list);
return 0;
}
Input
5
5 4 7 2 9
Output
5 4 7 2 9