Potrzebuje napsiać funkję clear() która czyści stos

0

czyli ustatwia głowe na początwk czy coś

#include <stdio.h>
#define MAXSIZE 5

struct stack {
    int stk[MAXSIZE];
    int top;
};
typedef struct stack STACK;
STACK s;

void push()
{
    int num;
    if (s.top == (MAXSIZE - 1)) {
        printf("\nStack is Full\n");
        return;
    }
    else {
        printf("\nEnter the element to be pushed\n");
        scanf("%d", &num);
        s.top = s.top + 1;
        s.stk[s.top] = num;
    }
    return;
}
int IsEmpty_Full()
{
    if (s.top == -1) {
        printf("\nStack is Empty\n");
        return 1;
    }
    return 0;
}
int pop()
{
    int num;
    if (IsEmpty_Full()) {
        return (s.top);
    }
    else {
        num = s.stk[s.top];
        printf("\npoped element is = %dn", s.stk[s.top]);
        s.top = s.top - 1;
    }
    return (num);
}

void PrintStack()
{
    int i;
    if (s.top == -1) {
        printf("\nStack is empty\n");
        return;
    }
    else {
        printf("\n The status of the stack is \n");
        for (i = s.top; i >= 0; i--) {
            printf("%d\n", s.stk[i]);
        }
    }
    printf("\n");
}
void main()
{
    int choice;
    int option = 1;
    s.top = -1;

    while (option) {
        printf("\n---------   STACK   ------------\n");
        printf("      1 -PUSH\t2 -POP\t3 -DISPLAY\t4 -EXIT \n");
        printf("-----------          ------------\n");

        printf("Enter your choice\n");
        scanf("%d", &choice);
        switch (choice) {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            PrintStack();
            break;
        case 4:
            return;
        }
      //  fflush(stdin); do czego to służy ?
    }
    return 0;
}

0

Po prostu napisz ten kod sam ;-)

0

napisałem

#include <stdio.h>
#define MAXSIZE 5

struct stack {
    int stk[MAXSIZE];
    int top;
};
typedef struct stack STACK;
STACK s;

void push()
{
    int num;
    if (s.top == (MAXSIZE - 1)) {
        printf("\nStack is Full\n");
        return;
    }
    else {
        printf("\nEnter the element to be pushed\n");
        scanf("%d", &num);
        s.top = s.top + 1;
        s.stk[s.top] = num;
    }
    return;
}
int IsEmpty_Full()
{
    if (s.top == -1) {
        printf("\nStack is Empty!!!\n");
        return 1;
    }
    return 0;
}
int pop()
{
    int num;
    if (IsEmpty_Full()) {
        return (s.top);
    }
    else {
        num = s.stk[s.top];
        printf("\npoped element is = %dn", s.stk[s.top]);
        s.top = s.top - 1;
    }
    return (num);
}
void Clear()
{
    s.top = -1;
}
void PrintStack()
{
    int i;
    if (s.top == -1) {
        printf("\nStack is empty\n");
        return;
    }
    else {
        printf("\n The status of the stack is \n");
        for (i = s.top; i >= 0; i--) {
            printf("%d\n", s.stk[i]);
        }
    }
    printf("\n");
}
int main()
{
    int choice;
    s.top = -1;

    while (1) {
        printf("\n---------   STACK   ------------\n");
        printf("      1 -PUSH\t2 -POP\t3 -DISPLAY\t4 -CLEAR ALL\t5 -EXIT \n");
        printf("-----------          ------------\n");

        printf("Enter your choice\n");
        scanf("%d", &choice);
        switch (choice) {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            PrintStack();
            break;
        case 4:
            Clear();
            break;
        case 5:
            return 0;
        }
        //  fflush(stdin); do czego to służy ?
    }
    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1