关于栈的问题

xxj2012xxj xxj2012xxj 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我的目的是将逐个浮点数压入栈 然后每压入一个数就将栈中的全部元素打印出来
代码如下:
#define  OK           0x0001
#define ERROR 0x0000
#define TRUE 0x0001
#define FALSE 0x0000
#define INFEASIBLE 0xFFFF
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 10

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <malloc.h>

typedef int Status;
typedef float SElemType;

typedef struct
{
SElemType *base;
SElemType *top;
Status stacksize;
}SqStack;

void InitStack(SqStack S)
{
S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!S.base)
{
printf("Memory allocation failure!");
exit(OVERFLOW);
}
S.base = S.top;
S.stacksize = STACK_INIT_SIZE;
}

Status GetTop(SqStack S, SElemType *e)
{
if (S.base == S.top)
return (ERROR);
*e = *(S.top - 1);
return (OK);
}

Status Push(SqStack S, SElemType e)
{
if (S.top - S.base == S.stacksize) // Stack fulls, Add storage space
{
S.base = (SElemType *) realloc (S.base,
(S.stacksize + STACKINCREMENT * sizeof(SElemType)));
S.top = S.stacksize + S.base;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}

Status Pop(SqStack S, SElemType *e)
{
if (S.base == S.top)
{
printf("The Stack is Empty!");
return ERROR;
}
*e = *--S.top;
return OK;
}

int main()
{
SqStack S;
int i = 0, count = 0;
float fElem;
InitStack(S);
while (1)
{
scanf("%f", &fElem);
Push(S, fElem);
count++;
// for (i = 0; i < count; i++)
printf("%d\n", count);
printf("%f\n", *(S.top - 1));

}
return 0;
}


问题是
1.编译后提示warning C4700: local variable 'S' used without having been initialized
SqStack S;定义后难道还要自己给S赋值吗?
2.
如果把
typedef struct
{
SElemType *base;
SElemType *top;
Status stacksize;
}SqStack;中的SqStack改成*SqStack 并且把程序中的.换成->后提示更多错误 为什么
3

typedef struct
{
SElemType *base;
SElemType *top;
Status stacksize;
}SqStack, *SqStack;
和
typedef struct
{
SElemType *base;
SElemType *top;
Status stacksize;
}*SqStack, SqStack;
有区别吗 他们都在实现个什么功能?






...全文

相关推荐

用栈求解n皇后问题 ,经典的回溯算法问题

n 皇后问题是一道经典的回溯算法问题,其目标是在一个 � × � n×n 的棋盘上放置 � n 个皇后,使得任意两个皇后都不能在同一行、同一列或同一斜线上。 栈可以用来辅助实现回溯算法,本质上就是手动维护了递归过程中系统默认维护的函数调用栈。下面给出使用栈求解 n 皇后问题的思路: 定义一个栈,用于存储已摆放皇后的位置信息。 初始将第一个皇后放到第一行第一列,入栈。 重复以下操作,直到栈为空: 取出栈顶元素,表示当前正在处理的行。 在该行从左到右依次尝试放置皇后,并检查可不可以。 如果找到一个可行的位置,则将该位置入栈,并转到下一行(即当前行数 + 1 +1)。 如果找不到可行的位置,弹出栈顶元素重新开始循环。 当栈的长度等于 � n 时,表示找到了一组可行解,输出解法。

java入门选手

火车发车问题 - stack 出入栈问题

火车发车问题 - stack 出入栈问题 题目链接 - hdu1022 题目 As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius ...

H_On

迷宫问题实验报告用栈解决迷宫问题

二. 概要设计 为了实现上述操作,以栈为存储结构。 本程序包含三个模块: (1) 主程序模块:实现人机交互。 (2) 迷宫生产模块:随机产生一个12×12的迷宫。 (3) 路径查找模块:实现通路的查找。 (4) 求解迷宫中一条通路的伪代码:

wangcuixm

C++基于栈实现铁轨问题

主要介绍了C++基于栈实现铁轨问题,实例分析了C++使用栈实现铁轨问题的思路与解决方法,具有一定参考借鉴价值,需要的朋友可以参考下

weixin_38606202

用栈解决迷宫问题C语言描述

用栈的思想解决了迷宫问题,C语言编写,在VC环境下编译通过。

Sabrina0115
评论 2
收藏 已收藏
分享
返回顶部
xxj2012xxj
C语言 70,039 成员 243,237 内容