Menu
CoddyTech

top

Lektion 5 von 13 im Kurs Stack – Datenstrukturen Serie #1 von Coddy.

challenge icon

Aufgabe

Einfach

Füge zu Stack eine Methode namens top hinzu, die keine Argumente erhält und die Ganzzahl oben auf dem Stack zurückgibt. 

Probier es selbst

#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));
        }
    }
    return 0;
}

Alle Lektionen in Stack – Datenstrukturen Serie #1

Übe selbstständig: Online-C-Compiler