如何修复从堆栈中删除元素的功能?

时间:2019-06-03 05:31:12

标签: c stack

我尝试创建一个函数,该函数将获得一些带有整数和“ n”(要删除的数字)的堆栈。该函数将检查堆栈,并在找到元素后将其删除。删除“ n”元素后,堆栈需要保持相同的顺序。我尝试将所有元素复制到临时堆栈中,并且在找到该元素时不复制他。之后,我想将所有元素返回到第一个堆栈。我的功能出了点问题。我围绕在网上找到的一些代码构建函数。

const int ID_box1 = 1; 

box1 = new Boxes(ID_box1, position(10,10); 

box1->Append("option 1");
box1->Append("option 2"); etc..

我试图解决所有问题,但是出现了所有相同的警告……并且代码不起作用。 == >>>新版本:

#include<stdio.h>
#define MAX 3
typedef int item;

typedef struct
{
    int TOP;
    int ele[MAX];
}Stack;

void init(Stack* s)
{
    s->TOP = -1;
}

int isFull(Stack* s)
{
    if (s->TOP == MAX - 1)
        return 0;
    return -1;
}

int isEmpty(Stack* s)
{
    if (s->TOP == -1)
        return 0;
    return -1;
}

void push(Stack* s, int item)
{
    if (!isFull(s))
    {
        printf("\nStack is full");
        return;
    }
    s->TOP = s->TOP + 1;
    s->ele[s->TOP] = item;
}

int pop(Stack* s, int* item)
{
    if (!isEmpty(s))
    {
        printf("\nStack is empty");
        return -1;
    }
    *item = s->ele[s->TOP];
    s->TOP = s->TOP - 1;

    return 0;
}

void func(Stack* s, item num)
{
    //Check stack
    if (s->TOP == -1)
    {
        printf_s("is empty");
        return 0;
    }       

    Stack temp;      //def temporary stack
    init(&temp);     //init temporary stack
    item current=0;

    while (s->TOP != -1)
    {       
        pop(&s, &current);
        if (current != num)
        {
            push(&temp, current);
        }           
    }

    while (!isEmpty(&temp))
    {
         pop(&temp, &current);
         push(&s, current);
    }

    while (s->TOP != -1)
    {
        pop(&s, &current);
        {
            printf_s("\nPoped Item : %d", current);
        }   
    }
}

int main()
{
    Stack s;
    item num = 20;

    init(&s);

    push(&s, 4);
    push(&s, 20);
    push(&s, 11);

    func(&s, num);    //delete specific element in stack

    getch();

    return 0;
}

enter image description here

2 个答案:

答案 0 :(得分:2)

    pop(&s, &current);

s已经是一个指针。它定义为Stack * s。因此&sStack **,而pop期望Stack *作为第一个参数。只需在此处删除&

答案 1 :(得分:0)

感谢所有为我提供提示和技巧的用户!经过所有更正后,这是最终版本。终于成功了!

#include<stdio.h>
#define MAX 7
typedef int item;

typedef struct
{
    int top;
    int ele[MAX];
}Stack;

void init(Stack* s)
{
    s->top = -1;
}

int isFull(Stack* s)
{
    return s->top == MAX - 1;
}

int isEmpty(Stack* s)
{
    return s->top == -1;
}

void push(Stack* s, int item)
{
    if (isFull(s))
    {
        printf("\nStack is full");
        return;
    }
    ++s->top;         //can be written as: s->TOP = s->TOP + 1;
    s->ele[s->top] = item;
}

int pop(Stack* s, int* item)
{
    if (isEmpty(s) == 1)
    {
        printf("\nStack is empty");
        return -1;
    }
    *item = s->ele[s->top];
    --s->top;        //can be written as: s->top = s->top - 1;

    return 0;
}

void func(Stack* s, item num)
{
    //Check stack
    if (isEmpty(s) == 1)
    {
        printf_s("is empty");
        return;
    }

    Stack temp;      //def temporary stack
    init(&temp);     //init temporary stack
    item current;

    printf_s("\n\nCopy all items from s stack to temp stack");
    while (isEmpty(s) != 1)
    {
        pop(s, &current);
        if (current != num)
        {
            push(&temp, current);
            printf_s("\nPushed Item is: %d", current);
        }
        else
            printf_s("\nDeleted Item is: %d", current);
    }

    printf_s("\n\n\nCopy all items from temp stack to s stack");

    while (isEmpty(&temp) != 1)
    {
        pop(&temp, &current);
        printf_s("\nPoped Item is: %d", current);
        push(s, current);
    }

    printf_s("\n\n\nPosition of elements in s stack");

    while (isEmpty(s) != 1)
    {
        pop(s, &current);
        printf_s("\nPoped Item is: %d", current);
    }
}

int main()
{
    Stack s;
    item stackElem;
    item num = 20;

    init(&s);

    printf("Fill stack \'s\'\n"); 
    while (!isFull(&s))
    {
        printf_s("Enter integer number please: ");
        scanf_s("%d", &stackElem);
        push(&s, stackElem);
        printf_s("Pushed Item is: %d\n", stackElem);
    }

    func(&s, num);    //delete specific element in stack

    getch();

    return 0;
}

enter image description here