Recursive function that keeps only unique elements in a stack with no loops/iteration

Viewed 44

the prototype is:

void unique(stack *s)
{
    // your code here
}

The following are Predefined:

#define N 20
typedef int element;
typedef struct
{
    element data[N];
    int top;
}stack;

stack CreateStack()
{
    stack p;
    p.top = -1;
    return p;
}

int isEmptyStack(stack p)
{
    return (p.top==-1);
}

int isFullStack(stack p)
{
    return (p.top==N-1);
}

int Push(stack *p, element e)
{
    if(isFullStack(*p)) return 0;
    p->data[++(p->top)] = e;
    return 1;
}

int Pop(stack* p)
{
    if(isEmptyStack(*p)) return 0;
    (p->top)--;
    return 1;
}

int Top(stack p, element* e)
{
    if(isEmptyStack(p)) return 0;
    *e = p.data[p.top];
    return 1;
}

Iteration of any kind is rejected, the use of a helper function is permitted

Original Stack:

1 2 3 3 8 4 5 6 5 7 7 8 2

Expected Stack:

1 6

I can't figure out how to be able to check if an element exists in stack, or how to remove an element that a duplicate has been found of, since iteration is not allowed

0 Answers
Related