C 从堆栈中删除小于10的项
当数字被推到堆栈上时,您需要弹出完整的堆栈,然后移除其中小于10的元素的堆栈。移除堆栈的所有元素时的示例代码:C 从堆栈中删除小于10的项,c,struct,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
#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;
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
#包括
结构kkk
{
浮动元素[15];
int top;//顶级rlrment的索引
};
结构kkk*st,*元素;//指针
void Init(struct kkk*st)//初始化
{
st->top=NULL;
}
void Push(struct kkk*st,float f)//将项目推到堆栈上
{
如果(st->top<15)
{
st->elem[st->top]=f;
st->top++;
}
其他的
printf(“堆栈已满\n”);
}
float Pop(struct kkk*st)//从堆栈中弹出一个项
{
浮动el;
如果((st->top)>0)
{
圣->顶--;
el=st->elem[st->top];
返回el;
}
其他的
{
printf(“堆栈为空\n”);
返回0;
}
}
float Vulychtop(struct kkk*st)//删除堆栈顶部
{
如果((st->top)>0){
返回(st->elem[st->top-1]);
}
否则{
printf(“堆栈为空!\n”);
返回0;
}
}
int Gettop(struct kkk*st)//堆栈的顶部元素,不带delting
{
返回(st->top);
}
int-Isempty(结构kkk*st)//检查
{
如果((st->top)==0)
返回1;
否则返回0;
}
void Vuvid(struct kkk*st)//所有元素的输出
{
int i;
i=st->top;
如果(Isempty(st)==1)返回;
做{
我--;
printf(“%f\n”,st->elem[i]);
}而(i>0);
}
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
inti,n,k;
浮式znach;
元素=(结构kkk*)malloc(sizeof(结构kkk));
Init(元素);
printf(“输入堆栈中的项目数\n”);
scanf(“%d”和“&n”);
对于(i=0;i
结果:
我创建一个堆栈,然后开始在其中输入数字。有了这个,我很好。接下来,我找到堆栈的顶部元素并将其弹出。在那之后,我需要从堆栈中删除那些值小于10的数字,然后我设法一个接一个地完全清除堆栈。无法为此设置条件。一种方法是使用另一个堆栈作为临时存储。在伪代码中,类似于:
* 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
*写入索引=0;
*读取索引=0;
*当读取索引<顶部时
*如果数组[读取索引]>=10
*数组[写入索引]=数组[读取索引]
*写入索引=写入索引+1
*读取索引=读取索引+1
*top=写入索引
您似乎正在使用旧的MS编译器。例如,标题
不是标准的C标题。相反,您应该使用标题
。此外,也不使用来自标题
的声明。因此,您可以删除标题的包含
函数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
也不应发出任何消息并返回堆栈元素
此外,在文件范围中声明指针元素
也没有什么意义。它可以在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 );
}
可能将俄语文本替换为英语文本。并举例说明输入和预期输出与实际输出。我不确定你的问题是什么。如果数字
n
小于10,则测试的条件是n<10
@Anton Tretiak是否需要删除堆栈中小于10的所有元素?如果是这样,那么您需要一个辅助堆栈,例如,如果要在不直接访问数组元素的情况下使用堆栈接口进行操作,您需要只使用堆栈接口(仅push和pop)还是可以直接访问底层数组?后一种方法会更简单…@SergeBallesta不,这不是必需的,我昨天试过这种方法,但没有成功。
free( element );