从堆栈中删除少于 10 个的项目

时间:2021-05-10 09:30:37

标签: c struct

数字压入栈中,需要弹出满栈,然后移除栈中小于10的元素。移除堆栈中所有元素时的示例代码:

#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <stdio.h>
#include <math.h>
#include <malloc.h>
struct kkk 
{
    float elem[15]; 
    int top; // index of the top rlrment
};
struct kkk* st, * element; // pointers

void Init(struct kkk* st) // initialization
{
    st->top = NULL; 
}

void Push(struct kkk* st, float f) // push an item onto the stack
{
    if (st->top < 15)
    {
        st->elem[st->top] = f;
        st->top++;
    }
    else
        printf("Stack full\n");

}

float Pop(struct kkk* st) // pop an item from the stack
{
    float el;
    if ((st->top) > 0)
    {
        st->top--;
        el = st->elem[st->top];
        return el;
    }
    else
    {
        printf("Stack is empty \n");
        return 0;
    }
}

float Vulychtop(struct kkk* st) // deleting the top of the stack
{
    if ((st->top) > 0) {
        return(st->elem[st->top - 1]);
    }
    else {
        printf("Stack is empty!\n");
        return 0;
    }
}

int Gettop(struct kkk* st) // top element of the stack without delting
{
    return(st->top);
}

int Isempty(struct kkk* st) // check 
{
    if ((st->top) == 0)
        return 1;
    else return 0;
}

void Vuvid(struct kkk* st) // Output of all elements
{
    int i;
    i = st->top;
    if (Isempty(st) == 1) return;
    do {
        i--;
        printf("%f\n", st->elem[i]);
    } while (i > 0);
}
int main()
{
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    int i, n, k;
    float znach;
    element = (struct kkk*)malloc(sizeof(struct kkk));
    Init(element);
    printf("Enter the number of items in the stack \n");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("Enter the number %d: ", i);
        scanf("%f", &znach);
        Push(element, znach);
    }
    printf("In stack %d elements \n", Gettop(element));
    printf("\n");
    Vuvid(element);
    printf("Top element %f\n", Vulychtop(element));
    do {
        printf("The element to be removed %f, ", Pop(element));
        printf("Items left in the stack %d \n", Gettop(element));
    } while (Isempty(element) == 0);
    return 0;
}

结果:https://i.stack.imgur.com/wLczr.png

我创建了一个堆栈,然后我开始在其中输入数字。有了这个,我很好。接下来,我找到堆栈的顶部元素并将其弹出。之后,我需要从堆栈中删除那些值小于10的数字,并且我设法将堆栈一个一个彻底清除。不能为此设定条件。

2 个答案:

答案 0 :(得分:0)

一种方法是使用另一个堆栈作为临时存储。在伪代码中类似:

* create tmp-stack
* while org-stack isn't empty
    * data = pop org-stack
    * if (data >= 10) push data to tmp-stack
* while tmp_stack isn't empty
    * data = pop tmp_stack
    * push data to org-stack
* free tmp-stack

这可以使用已经存在的函数来实现。

直接对保存堆栈数据的数组进行操作可以获得更好的性能。根据@SergeBallesta 的想法,它可能看起来像:

* write-index = 0;
* read-index = 0;
* while read-index < top
    * if array[read-index] >= 10
        * array[write-index] = array[read-index]
        * write-index = write-index + 1
    * read-index = read-index + 1
* top = write-index


  

答案 1 :(得分:0)

您似乎在使用旧的 MS 编译器。例如,标头 <malloc.h> 不是标准的 C 标头。相反,您应该使用标题 <stdlib.h>。也没有使用来自标题 <math.h> 的声明。因此,您可以删除包含的标题。

函数Gettop没有执行函数注释中写的内容

// top element of the stack without delting

实际上它返回数据成员 st->top 的当前值,即堆栈中存在的元素数。

另一方面,函数Vulychtop的注释

// deleting the top of the stack

不正确。该函数不会从堆栈中删除元素,因为数据成员 st->top 的值没有减少。此外,这样的函数不应输出任何消息。函数 Pop 从堆栈中移除元素。

函数Vulychtop可以通过以下方式定义

int Vulychtop( struct kkk *st, float *value ) 
{
    if ( st->top != 0 ) *value = st->elem[st->top - 1];
    
    return st->top != 0; 
}

函数 Pop 也不应该发出任何消息并返回堆栈的元素。

在文件作用域中声明指针element也没有多大意义。它可以在 main 中声明。

文件作用域中声明的指针st

struct kkk* st, * element; // pointers
            ^^

未在程序中使用。

也不需要动态分配 struct kkk 类型的对象。 你可以写在 main

struct kkk element;
Init( &element ); 

要使用堆栈的开放接口从原始堆栈中删除小于 10 的元素,您需要一个辅助堆栈。

例如

struct kkk st;
Init( &st );

while( !Isempty( element ) )
{
    float value;
    Vulychtop( element, &value ); // here I am using the function definition I showed above

    if ( !( value < 10.0f ) ) Push( &st, value );
    Pop( element );
} 


while( !Isempty( &st ) )
{
    float value;
    Vulychtop( &st, &value ); // here I am using the function definition I showed above

    Push( element, value );
    Pop( &st );
} 

您可以自己在 while 循环中添加有关弹出、推送或删除哪些元素的消息。

请注意,当您动态分配 struct kkk 类型的对象时,您应该在退出程序之前将其释放

free( element );