| 关于堆栈的问题!!!!! |
| [ 来源:ITWENKU 时间:2006-10-26 18:53:25 | 浏览:183人次
] |
| |
各位大哥看一下这个程序有什么问题, 当入栈的元m大于20的时候, 出栈的时候就出现问题了.先谢了.
#include<iostream.h> #include<stdlib.h> #include<iomanip.h> #include<malloc.h>
#ifndef NULL const int NULL = 0; #endif
#define INIT_SIZE 10 #define INCREMENT 10
typedef int Element; typedef int Position;
typedef struct stack{ Element *base; Position top; int size; }Stack; enum StackError{None,StackIsEmpty,StackUnderflow,StackOverflow,StackAssignmentError,}; enum StackError SErr;
void Create(Stack &S) { S.base = (Element *)malloc(INIT_SIZE*sizeof(Element)); if(S.base == NULL) { SErr = StackAssignmentError; exit(0); }
S.top = -1; S.size = INIT_SIZE; SErr = None;
}
void Push(Element e,Stack &S) { Element *newbase; if(S.top >= S.size-1) { newbase = (Element *)realloc(S.base,S.size+INCREMENT*sizeof(Element)); if(newbase == NULL) { SErr = StackOverflow; exit(0); } else { S.base = newbase; S.size+= INCREMENT; S.top++; S.base[S.top] = e; } } else { S.top++; S.base[S.top] = e; } }
Element Pop(Stack &S) { if(S.top == -1) { SErr = StackUnderflow; return 0; } else return S.base[S.top--]; } void main() { cout<<"*************************************************"<<endl; int i,j,m; Stack SS; Create(SS); cout<<"input the value of m:"<<endl; cin>>m;
for(i=0;i<m;i++) Push(i,SS); j=1; while(SS.top != -1) { if(j%10 == 0) cout<<setw(4)<<Pop(SS)<<endl; else cout<<setw(4)<<Pop(SS); j++; }
free (SS.base); cout<<endl; }
是free(base)时出现的问题
问题解决: -------- int size=0;//这里定义一个临时变量存储原来stack的size if(S.top >= S.size-1) { size = _msize(S.base);//获取stack的size newbase = (Element *)realloc(S.base,size+INCREMENT*sizeof(Element)); if(newbase == NULL) -------------
问题解决: -------- int size=0;//这里定义一个临时变量存储原来stack的size if(S.top >= S.size-1) { size = _msize(S.base);//获取stack的size newbase = (Element *)realloc(S.base,size+INCREMENT*sizeof(Element)); if(newbase == NULL) -------------
#include<iostream.h> #include<stdlib.h> #include<iomanip.h> #include<malloc.h>
#ifndef NULL const int NULL = 0; #endif
#define INIT_SIZE 10 #define INCREMENT 10
typedef int Element; typedef int Position;
typedef struct stack{ Element *base; Position top; int size; }Stack; enum StackError{None,StackIsEmpty,StackUnderflow,StackOverflow,StackAssignmentError,}; enum StackError SErr;
void Create(Stack &S) { S.base = (Element *)malloc(INIT_SIZE*sizeof(Element)); if(S.base == NULL) { SErr = StackAssignmentError; exit(0); }
S.top = -1; S.size = INIT_SIZE; SErr = None;
}
void Push(Element e,Stack &S) { Element *newbase; int size =0; if(S.top >= S.size-1) {
size = _msize(S.base);//获取stack的size newbase = (Element *)realloc(S.base,S.size+INCREMENT*sizeof(Element)); if(newbase == NULL) { SErr = StackOverflow; exit(0); } else { S.base = newbase; S.size+= INCREMENT; S.top++; S.base[S.top] = e; } } else { S.top++; S.base[S.top] = e; } }
Element Pop(Stack &S) { if(S.top == -1) { SErr = StackUnderflow; return 0; } else return S.base[S.top--]; } void main() { cout<<"*************************************************"<<endl; int i,j,m; Stack SS; Create(SS); cout<<"input the value of m:"<<endl; cin>>m;
for(i=0;i<m;i++) Push(i,SS); j=1; while(SS.top != -1) { if(j%10 == 0) cout<<setw(4)<<Pop(SS)<<endl; else cout<<setw(4)<<Pop(SS); j++; }
free (SS.base); cout<<endl; }
还是有问题,m 的值超过30后,中间有些值Pop出的是一些乱七八糟的数据.
问题解决: -------- int size=0;//这里定义一个临时变量存储原来stack的size if(S.top >= S.size-1) { size = _msize(S.base);//获取stack的size newbase = (Element *)realloc(S.base,size+INCREMENT*sizeof(Element)); if(newbase == NULL) ------------- 是我搞错了.为什么是free(base)时出现的问题?????? 哪位老大能解决一下?
newbase = (Element *)realloc(S.base,size+INCREMENT*sizeof(Element)); 你这里定义的size 是int的 应改为 newbase = (Element *)realloc(S.base,(size+INCREMENT)*sizeof(Element));
|
|
 |
最新更新 |
|