写栈的实现的过程中遇到的问题

chen8314 2006-05-28 02:48:37
我在写用栈实现二叉树的非递归算法,在编译的过程中提示一下两条语句有问题:
void Push(SqStack S, SElemType *e)
{
if(S.top - S.base >= S.stacksize)
{
S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(SElemType)); // 这里!!!!!!!!!!!!
if(!S.base)exit(ERROR);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = *e;
}


void InitStack(SqStack S)
{
S.base = ( SElemType * )malloc( STACK_INIT_SIZE * sizeof( SElemType ) );//这里!
if(!S.base)exit(ERROR);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
}

错误提示如下:
error C2143: syntax error : missing ')' before ';'
error C2059: syntax error : ')'
error C2100: illegal indirection
error C2143: syntax error : missing ')' before ';'
error C2143: syntax error : missing ')' before ';'
error C2059: syntax error : ')'
error C2059: syntax error : ')'
error C2100: illegal indirection

可能是昨天没有睡好,怎么都想不出哪里错了。望大家帮帮忙看看。下面是栈的实现部分的全部代码:
#define STACK_INIT_SIZE 100; // 存储空间初始分配量
#define STACKINCREMENT 10; // 存储空间分配增量

typedef BiTNode SElemType; // 树的节点类型

typedef struct Stack { // 定义栈结构
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

// 初始化栈S,分配存储空间并置为空
void InitStack(SqStack S)
{
S.base = ( SElemType * )malloc( STACK_INIT_SIZE * sizeof( SElemType ) );
if(!S.base)exit(ERROR);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
}

// 清空栈
void ClearStack(SqStack S)
{
S.top = S.base;
}

//检查栈是否为空
bool StackEmpty(SqStack S)
{
if(S.top == S.base)
{
return TRUE;
}
else
return FALSE;
}

//读取栈顶元素
int GetTop(SqStack S, SElemType *e)
{
if(S.top != S.base)
{
*e = *(S.top - 1);
return OK;
}
else
return ERROR;
}

//向栈中插入元素
void Push(SqStack S, SElemType *e)
{
if(S.top - S.base >= S.stacksize)
{
S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit(ERROR);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = *e;
}

//从栈中删除元素
int Pop(SqStack S, SElemType *e)
{
if(S.top != S.base)return ERROR;
*e = *--S.top;
return OK;
}
...全文
353 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen8314 2006-06-10
  • 打赏
  • 举报
回复
多谢多谢 结了
bluedreammer 2006-06-02
  • 打赏
  • 举报
回复
是的,在GetTop函数里面,LZ试图改变形参来改变实参,显然函数参数传递没有理解。

另外你的后序算法根本无法实现,没办法理解你的双栈排序的思路,只能使用递归了。
sharpdew 2006-06-02
  • 打赏
  • 举报
回复
很多错误,不过最大的错误还是你在实现栈的时候试图用S.top++,或者S.top++来改变当前栈顶指针,要知道你这不是数组,不能这样求栈中的上一个或者下一个元素,我不知道你是哪里学来这样怪异的东西的。
NuisT_NeO 2006-06-02
  • 打赏
  • 举报
回复
我刚学过2叉树
学的不好,来这再学下,呵呵
bluedreammer 2006-06-01
  • 打赏
  • 举报
回复 1
晕,乱码,只能把中文的注释都删了。
你的后序算法有问题,我用递归的写了遍,看看吧

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#define ERROR -1
#define OK 0
#define FALSE 0
#define TRUE 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef struct BiTNode
{
char data;
struct BiTNode *lchild, *rchild;
}BiTNode;

typedef int bool;

int top_count = 0;

BiTNode *CreatBinTree(void)
{
BiTNode *T;
char ch;
// int i = 0;
if((ch=getchar())=='#')
return(NULL);
else
{
printf("malloc BitNode: %c\n", ch);
T = (BiTNode *)malloc(sizeof(BiTNode));
T->data = ch;
printf("Creat lchild\n");
T->lchild = CreatBinTree();
printf("Creat rchild\n");
T->rchild = CreatBinTree();
return(T);
}
}

void ClearBinTree(BiTNode * tree)
{
if(tree == NULL) return;
ClearBinTree(tree->lchild);
ClearBinTree(tree->rchild);
free(tree);
}

PostOrder(BiTNode *root)
{
if(root)
{
PostOrder(root->lchild);
PostOrder(root->rchild);
printf(" %c \n",root->data);
}
}


void Traverse( BiTNode *root )
{
BiTNode *temp = NULL;
PostOrder(root);
}


void main()
{
BiTNode *root;
printf("\n");
printf("Creat Bin_Tree£» Input preorder:");

root = CreatBinTree();
if(!root) return;
Traverse( root );
ClearBinTree(root);
}
bluedreammer 2006-06-01
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#define ERROR -1
#define OK 0
#define FALSE 0
#define TRUE 1
#define STACK_INIT_SIZE 100 // ´æ´¢¿Õ¼ä³õʼ·ÖÅäÁ¿
#define STACKINCREMENT 10 // ´æ´¢¿Õ¼ä·ÖÅäÔöÁ¿

// -----------------------------------------------------------
// ¶þ²æÊ÷µÄʵÏÖ
// -----------------------------------------------------------
typedef struct BiTNode
{ // Ê÷½Úµã¶¨Òå
char data;
struct BiTNode *lchild, *rchild;
}BiTNode;

typedef int bool;

int top_count = 0;

// ´´½¨¶þ²æÊ÷
BiTNode *CreatBinTree(void)
{
BiTNode *T;
char ch;
// int i = 0;
if((ch=getchar())=='#')
return(NULL); // ¶ÁÈë#£¬·µ»Ø¿ÕÖ¸Õë
else
{
printf("malloc BitNode: %c\n", ch);
T = (BiTNode *)malloc(sizeof(BiTNode)); // Éú³É½áµã
T->data = ch;
printf("Creat lchild\n");
T->lchild = CreatBinTree(); // ¹¹Ôì×ó×ÓÊ÷
printf("Creat rchild\n");
T->rchild = CreatBinTree(); // ¹¹ÔìÓÒ×ÓÊ÷
return(T);
}
}

void ClearBinTree(BiTNode * tree)
{
if(tree == NULL) return;
ClearBinTree(tree->lchild);
ClearBinTree(tree->rchild);
free(tree);
}

PostOrder(BiTNode *root)
{
if(root)
{
PostOrder(root->lchild);
PostOrder(root->rchild);
printf(" %c \n",root->data);
}
}


// -----------------------------------------------------------
// ¶þ²æÊ÷ºóÐò±éÀúµÄʵÏÖ
//
// -----------------------------------------------------------
void Traverse( BiTNode *root )
{

BiTNode *temp = NULL;

PostOrder(root);

}


void main()
{
BiTNode *root;
printf("\n");
printf("Creat Bin_Tree£» Input preorder:"); //ÊäÈëÍêÈ«¶þ²æÊ÷µÄÏÈÐòÐòÁУ¬
// ÓÃ#´ú±íÐé½áµã£¬ÈçABD###CE##F##
root = CreatBinTree(); //´´½¨¶þ²æÊ÷£¬·µ»Ø¸ù½áµã
if(!root) return;
Traverse( root );
ClearBinTree(root);
}
chen8314 2006-05-28
  • 打赏
  • 举报
回复
自顶一哈
chen8314 2006-05-28
  • 打赏
  • 举报
回复
我把代码都发出来,大家帮忙看看吧: 小的才疏学浅 调了一天了 实在不行了
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#define ERROR -1
#define OK 0
#define FALSE 0
#define TRUE 1
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量

// -----------------------------------------------------------
// 二叉树的实现
// -----------------------------------------------------------
typedef struct BiTNode{ // 树节点定义
char data;
struct BiTNode *lchild, *rchild;
}BiTNode;

// 创建二叉树
BiTNode *CreatBinTree(void)
{
BiTNode *T;
char ch;
if((ch=getchar())=='#')
return(NULL); // 读入#,返回空指针
else
{
T = (BiTNode *)malloc(sizeof(BiTNode)); // 生成结点
T->data = ch;
T->lchild = CreatBinTree(); // 构造左子树
T->rchild = CreatBinTree(); // 构造右子树
return(T);
}
}


// -----------------------------------------------------------
// 栈的实现
// -----------------------------------------------------------
typedef BiTNode SElemType;

typedef struct Stack { // 定义栈结构
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

// 初始化栈S,分配存储空间并置为空
void InitStack(SqStack S)
{
S.base = ( SElemType * )malloc( STACK_INIT_SIZE * sizeof( SElemType ) );
if(!S.base)exit(ERROR);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
}

// 清空栈
void ClearStack(SqStack S)
{
S.top = S.base;
}

//检查栈是否为空
bool StackEmpty(SqStack S)
{
if(S.top == S.base)
{
return TRUE;
}
else
return FALSE;
}

//读取栈顶元素
int GetTop(SqStack S, SElemType *e)
{
if(S.top != S.base)
{
*e = *(S.top - 1);
return OK;
}
else
return ERROR;
}

//向栈中插入元素
void Push(SqStack S, SElemType *e)
{
if(S.top - S.base >= S.stacksize)
{
S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit(ERROR);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = *e;
}

//从栈中删除元素
int Pop(SqStack S, SElemType *e)
{
if(S.top != S.base)return ERROR;
*e = *--S.top;
return OK;
}





// -----------------------------------------------------------
// 二叉树后序遍历的实现
// Traverse_DoubleStack() 双栈实现
// -----------------------------------------------------------
void Traverse_DoubleStack( BiTNode *root )
{
SqStack stack_one, stack_tow;
BiTNode *temp;
InitStack( stack_one );
InitStack( stack_tow );

Push( stack_one, root );
GetTop( stack_one, temp );
if( temp->rchild == NULL && temp->lchild == NULL ) Pop(stack_one, temp);

while( StackEmpty( stack_one ) )
{
Pop( stack_one, temp );
if( temp->lchild != NULL )
{
Push( stack_one, temp->lchild );
}
if( temp->rchild != NULL )
{
Push( stack_one, temp->rchild );
}
Push( stack_tow, temp);
}
while( StackEmpty( stack_tow ) )
{
Pop( stack_tow, temp );
}
}


void main()
{
BiTNode *root;
printf("\n");
printf("Creat Bin_Tree; Input preorder:"); //输入完全二叉树的先序序列,
// 用#代表虚结点,如ABD###CE##F##
root = CreatBinTree(); //创建二叉树,返回根结点
Traverse_DoubleStack( root );
}
chen8314 2006-05-28
  • 打赏
  • 举报
回复
这样的:程序运行了,但是运行了树的建立的时候跳出了‘内存错误的BOX’
// -----------------------------------------------------------
// 二叉树的实现
// -----------------------------------------------------------
typedef struct BiTNode{ // 树节点定义
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BinTree;

// 创建二叉树
BinTree CreatBinTree(void)
{
BinTree T;
char ch;
if((ch=getchar())=='#')
return(NULL); // 读入#,返回空指针
else
{
T = (BiTNode *)malloc(sizeof(BiTNode)); // 生成结点
T->data = ch;
T->lchild = CreatBinTree(); // 构造左子树
T->rchild = CreatBinTree(); // 构造右子树
return(T);
}
}



void main()
{
BinTree *root;
printf("\n");
printf("Creat Bin_Tree; Input preorder:"); //输入完全二叉树的先序序列,
// 用#代表虚结点,如ABD###CE##F##
*root = CreatBinTree(); //创建二叉树,返回根结点。单步调试运行到这里出的问题!
Traverse_DoubleStack( *root ); // 遍历函数
}
sharpdew 2006-05-28
  • 打赏
  • 举报
回复
还有啥问题?说说
chen8314 2006-05-28
  • 打赏
  • 举报
回复
谢谢 sharpdew 问题解决了:)
但又出现了一些问题,暂时还不能结 希望大家继续帮我 呵呵
chen8314 2006-05-28
  • 打赏
  • 举报
回复
在树的节点定义的时候定义了的:
typedef struct BiTNode{ // 树节点定义
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BinTree;
sharpdew 2006-05-28
  • 打赏
  • 举报
回复
#define STACK_INIT_SIZE 100; // 存储空间初始分配量
#define STACKINCREMENT 10; // 存储空间分配增量
~~~~~~~~~~~~~~
宏后面不要分号!
sharpdew 2006-05-28
  • 打赏
  • 举报
回复
typedef BiTNode SElemType; // 树的节点类型
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BiTNode在哪里定义了?

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧