注册 登录
编程论坛 数据结构与算法

栈的基本操作

pauvael 发布于 2016-11-03 22:14, 2700 次点击
问题是:S.base=(ElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType))非法的间接寻址 ;语法错误 : “)”
求大神帮忙看一下,感激不尽~


 #include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include<iostream>

using namespace std;

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

typedef char ElemType;
typedef int Status;

typedef struct Node{
    ElemType *base;
    ElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack &S)
{
   
    S.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
    if(!S.base) exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}

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

bool StackEmpty(SqStack S){
    if(S.top==S.base)
        return TRUE;
    return FALSE;
}

Status Push(SqStack &S,ElemType e){
    ElemType* newbase;
    if(S.top-S.base>=S.stacksize){
        S.base=(ElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));   
    if(!S.base) exit(OVERFLOW);
    S.top=S.base+S.stacksize;
    S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return OK;
}

Status Pop(SqStack &S,ElemType &e){
    if(S.top==S.base)return ERROR;
    e=*--S.top;
    return OK;
}


void conversion (int Num) {  // 算法3.1
    // 对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数
    ElemType e;  
    SqStack S;
    InitStack(S);      // 构造空栈
    while (Num) {
        Push(S, Num % 8);
        Num = Num/8;
    }
    while (!StackEmpty(S)) {
        Pop(S,e);
        printf ("%d", e);
    }
    printf("\n");
} // conversion
4 回复
#2
书生牛犊2016-11-04 17:23
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include<iostream>

using namespace std;

#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
#define TRUE 1;
#define FALSE 0;
#define OVERFLOW -1
#define OK 1
#define ERROR -2
#define 语句的后面不能跟分号,否则分号也会被当做一部分替换到代码当中去


#3
陈CDG2016-11-04 22:58
我改了一下,加了main()函数,可以运行,希望对你有用
只有本站会员才能查看附件,请 登录

程序代码:

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include<iostream>

using namespace std;

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

typedef char ElemType;
typedef int Status;

typedef struct Node{
    ElemType *base;
    ElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack &S)
{
   
    S.base=(ElemType*)malloc((STACK_INIT_SIZE)*sizeof(ElemType));
    if(!S.base) exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}

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

bool StackEmpty(SqStack S){
    if(S.top==S.base)
        return TRUE;
    return FALSE;
}

Status Push(SqStack &S,ElemType e){
    ElemType* newbase=NULL;
    if(S.top-S.base>=S.stacksize){
        S.base=(ElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));   
    if(!S.base) exit(OVERFLOW);
    S.top=S.base+S.stacksize;
    S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return OK;
}

Status Pop(SqStack &S,ElemType &e){
    if(S.top==S.base)return ERROR;
    e=*--S.top;
    return OK;
}


void conversion (int Num) {  // 算法3.1
   
// 对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数
    ElemType e;  
    SqStack S;
    InitStack(S);      // 构造空栈
    while (Num) {
        Push(S, Num % 8);
        Num = Num/8;
    }
    while (!StackEmpty(S)) {
        Pop(S,e);
        printf ("%d", e);
    }
    printf("\n");
} // conversion

int main(void)
{

    return 0;
}
#4
pauvael2016-12-16 18:57
回复 2楼 书生牛犊
感谢感谢!
#5
pauvael2016-12-16 18:58
回复 3楼 陈CDG
感谢感谢!
1