Max Heap Insertion and Deletion

Time limit: 500ms
Memory limit: 256mb

Description:
Please complete the missing code of the max heap ADT with an array, such that the program could output a correct answer.
1. isEmpty
2. isFull
3. insertHeap
4. deleteHeap

-------------------------Copy the following code, complete it and submit-------------------------

/*
I, , am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum {INSERTION = 1, DELETION, END, ERROR_OP} OP;
/* the minimum value of interger type. 0x80000000 = -2147483648
 * the maximum value of integer type. 0x7fffffff = 2147483647
 * They are used to represent the error of heap underflow and heap overflow, respectively
 */
#define ERROR_UNDERFLOW 0x80000000
#define ERROR_OVERFLOW  0x7fffffff

struct Heap {
    int * arr;
    int capacity;
    int size;
};

struct Heap * createHeap(int capacity) {
    struct Heap * heap = (struct Heap *)malloc(sizeof(struct Heap));
    heap->arr = (int*)malloc(sizeof(int) * capacity);
    heap->capacity = capacity;
    heap->size = 0;
    return heap;
}

int isFull(struct Heap * heap) {
    // write your code here
}

int isEmpty(struct Heap * heap) {
    // write your code here
}

int insertHeap(struct Heap * heap, int x) {
    if (isFull(heap)) {
        return ERROR_OVERFLOW;
    }
    // write your code here
}

int deleteHeap(struct Heap * heap) {
    if (isEmpty(heap)) {
        return ERROR_UNDERFLOW;
    }
    // write your code here
}

// decode the input operations
OP get_op() {
    char str[20];
    scanf("%s", str);
    if (strcmp(str, "ins") == 0) {
        return INSERTION;
    }
    else if (strcmp(str, "del") == 0) {
        return DELETION;
    }
    else if (strcmp(str, "end") == 0) {
        return END;
    }
    else {
        return ERROR_OP;
    }
}

void printHeap(struct Heap * heap) {
    int i;
    for (i = 1; i <= heap->size; i++) {
        print("%d ", heap->arr[i]);
    }
    return;
}

// DO NOT MODIFY THE CODE BELOW
int main() {
    int n, x, ret;
    scanf("%d", &n);
    struct Heap * heap = createHeap(n);

    int flag = 0;
    while (!flag) {
        switch(get_op())
        {
            case INSERTION:
                scanf("%d", &x);
                ret = insertHeap(heap, x);
                if (ret == ERROR_OVERFLOW) {
                    printf("heap overflow\n");
                }
                break;
            case DELETION:
                ret = deleteHeap(heap);
                if (ret == ERROR_UNDERFLOW) {
                    printf("heap underflow\n");
                }
                else {
                    printf("%d\n", ret);
                }
                break;
            case END:
                printHeap(heap);
                flag = 1;
                break;
            default:
                printf("error input\n");
        }
    }

    return 0;
}

-------------------------------------------End of Code-------------------------------------------

Input:
The first line contains a non-negative integer N;
Then M lines of heap operations, each chosen from one of the followings: ins x, del.
The last line is an ending indicator, “end”.

Output:
The top element for per pop operation. If there exists underflow or overflow, please output "heap underflow" or "stack overflow", respectively.
The remaining elements in the heap, separated by a space.

Sample Input 1:
5
ins 1
ins 2
ins 3
ins 4
ins 5
end

Sample Output 1:
heap overflow
4 3 2 1

Sample Input 2:
3
ins 1
ins 2
del
del
del
end

Sample Output 2:
2
1
heap underflow
Submit