📜  如何删除堆栈c ++代码示例中的重复元素

📅  最后修改于: 2022-03-11 14:44:50.976000             🧑  作者: Mango

代码示例1
int* stack = (int*)malloc(10 * sizeof(int));
int size = 10;
int sp = -1;

bool isempty() {
    return (sp == -1);
}

bool isfull() {
    return (sp == size - 1);
}

void push(int x) {
    if (isfull()) {
        printf("Full!");
    }
    else {
        sp++;
        stack[sp] = x;
    }
}

int pop() {
    int x;
    if (isempty()) {
        printf("Empty!");
    }
    else {
        x = stack[sp];
        sp--;
    }
    return x;
}

void peek() {
    if (!isempty()) {
        printf("%d", stack[sp]);
    }
}

void clear() {
    while (!isempty()) {
        pop();
    }
}

void print() {
    if (!isempty()) {
        for (int i = 0; i < sp+1; i++) {
            printf("%d ", stack[i]);
        }
    }
    printf("\n");
}