Menu
CoddyTech

大小

Coddy 栈:数据结构系列 #1 课程的第 7 课,共 13 课。

challenge icon

挑战

简单

为 Stack 添加一个名为 size 的方法,该方法不接收参数,并返回栈中当前元素的数量。 

亲手试试

#include <stdio.h>
#include <string.h>
#include "stack.h"

int main() {
    Stack stack;
    Stack_init(&stack);
    char cmd[16];
    while (scanf("%15s", cmd) == 1) {
        if (strcmp(cmd, "push") == 0) {
            int x;
            scanf("%d", &x);
            Stack_push(&stack, x);
        }
        else if (strcmp(cmd, "top") == 0) {
            printf("%d\n", Stack_top(&stack));
        }
        else if (strcmp(cmd, "pop") == 0) {
            printf("%d\n", Stack_pop(&stack));
        }
        else if (strcmp(cmd, "size") == 0) {
            printf("%d\n", Stack_size(&stack));
        }
    }
    return 0;
}

栈:数据结构系列 #1中的所有课

自己练习: C 在线编译器