Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.0 kB
2
Indexable
Never
#include <stdio.h>
#include <stdbool.h>

int n = 10;
int stack[10];
int top = -1;
int push(int a);
int pop();
int peek();
bool isEmpty();
bool isFull();

main(){
    push(3);
    push(5);
    push(7);
    int data = pop();
    printf("%d ", data);

    data = peek();
    printf("%d ", data);

    data = pop();
    printf("%d ", data);

    push(12);

    data = peek();
    printf("%d ", data);


}

int push(int a){
    if(isFull()){
        return -1;
    }
    top++;
    stack[top] = a;
    return a;
}

int pop(){
    if(isEmpty()){
        return -1;
    }
    //int item = arr[top];
    //top--;
    //return item;
    return stack[top--];
}

int peek(){
    if(isEmpty()){
        return -1;
    }
    return stack[top];
}

bool isEmpty(){
    if(top == -1){
        return true;
    } else {
        return false;
    }
}
bool isFull(){
    if(top == n-1){
        return true;
    } else {
        return false;
    }
}

Leave a Comment