WEB 开发 开发语言 Java 技术 .Net 技术 数 据 库 硬件使用
图象媒体 Linux/Unix 移动平台 嵌入开发 Windows 专区 软件工程
Ajax 技术 | ASP | PHP | ASP.NET | C# | JSP | Access | Oracle | 网页设计 | Flash | 安全专题 | IIS | VC/MFC | Delphi | C/C++
 你的位置:首页 > 开发语言 > C/C++

关于堆栈的问题!!!!!
[ 来源: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));


 相关文章
·virtual继承 与 sizeof 问题
·sizeof的返回值
·函数返回值时为什么要把值放在临时变量里面呢?
·extern变量的问题
·一个矩阵的问题
·关于vector

 最新更新
请问:看看程序是什么错误?
《核心编程》中的几个问题
关于 Visual C++ 在 AMD 平台上 无法使用的问题
怎样阻止程序访问栈中的内容?
类与结构体混合使用
二维数组动态添加数据
bioskey检测不到:Win键、右键菜单键、抓屏键、F11、F12键,请大家帮忙
为什么我的vb6。0(SP6)没有自动参数信息功能?
求多元函数极小值的算法?
字符串free()问题,请教
如何才能solaris studio11编程效率?
A* pA = (A*)new char[100];delete pA;对么?
 版权所有:中国IT问库
copyright © 2006 www.itwenku.com all rights reserved.