【问题标题】:Remove items less than 10 from the stack从堆栈中移除少于 10 个的项目
【发布时间】:2021-05-10 22:45:57
【问题描述】:

数字被压入堆栈,你需要将整个堆栈出栈,然后将小于 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的数字,并且我设法将堆栈一个一个地完全清除。无法为此设定条件。

【问题讨论】:

  • 可能用英文文本替换俄文文本。并展示输入和预期与实际输出的示例
  • 我不确定你的问题是什么。判断一个数n是否小于10的条件是n &lt; 10
  • @Anton Tretiak 您是否需要删除堆栈中小于 10 的所有元素?如果是这样,那么您需要一个辅助堆栈,如果要使用堆栈接口进行操作而不直接访问数组 elem。
  • 您是只需要使用栈接口(只需要push和pop)还是可以直接访问底层数组?后一种方式会更简单...
  • @SergeBallesta 不,不需要,我昨天试过这个方法,但不成功。

标签: c struct


【解决方案1】:

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

* 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


  

【讨论】:

    【解决方案2】:

    看来您使用的是旧的 MS 编译器。例如,标头 &lt;malloc.h&gt; 不是标准的 C 标头。相反,您应该使用标题&lt;stdlib.h&gt;。也没有使用来自标题 &lt;math.h&gt; 的声明。所以你可以删除标题的包含。

    函数Gettop 不执行函数注释中所写的操作

    // top element of the stack without delting
    

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

    另一方面,函数Vulychtop的注释

    // deleting the top of the stack
    

    不正确。该函数不会从堆栈中删除元素,因为数据成员st-&gt;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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-10
      • 2018-07-05
      • 2012-11-03
      • 2020-01-25
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多