照着书上敲的,运行结果应该是in数组元素的一个出栈序列,类似
Please enter stack size:5
2:Kiki Ishtar
1:Hank Gilgamesh
3:Betty Rocker
5:Wolfgang Kibble
4:Ian Flagranti
7:Joy Almondo
9:Juan Moore
8:Xaverie Paprika
6:Portia Koop
10:Misha Mache
这样
但是编译后运行,除非栈只有一个元素,无论输入多大的栈尺寸,输出都会出错:
Please enter stack size: 5
Kiki Ishtar
Kiki Ishtar
ki Ishtar
i Ishtar
iki Ishtar
Ishtar
: Kiki Ishtar
Ishtar
2: Kiki Ishtar
tar
Bye
是内存出错了吗?为什么会出现这种结果?求大神指点。
头文件
//stcktp1.h
#ifndef STCKPT1_H_
#define STCKPT1_H_
template <typename Type>
class Stack
{
private:
enum {SIZE=10}; //default size
int stacksize;
Type *items; //holds stack items
int top; //index for top stack item
public:
explicit Stack(int ss=SIZE);
Stack(const Stack &st);
~Stack() {delete [] items;}
bool isempty() {return top==0;}
bool isfull() {return top==stacksize;}
bool push(const Type &item); //add item to stack
bool pop(Type &item); //pop top into item
Stack &operator=(const Stack &st);
};
template <typename Type>
Stack<Type>::Stack(int ss):stacksize(ss),top(0)
{
items=new Type[stacksize];
}
template <typename Type>
Stack<Type>::Stack(const Stack &st)
{
stacksize=st.stacksize;
top=st.top;
items=new Type[stacksize];
for(int i=0;i<top;i++)
items[i]=st.items[i];
}
template <typename Type>
bool Stack<Type>::push(const Type &item)
{
if(top<stacksize)
{
items[top++]=item;
return true;
}
else
return false;
}
template <typename Type>
bool Stack<Type>::pop(Type &item)
{
if(top>0)
{
item=items[--top];
return true;
}
else
return false;
}
template <typename Type>
Stack<Type> &Stack<Type>::operator=(const Stack<Type> &st)
{
if(this==&st)
return *this;
delete [] items;
stacksize=st.stacksize;
top=st.top;
items=new Type[stacksize];
for(int i=0;i<top;i++)
items[i]=st.items[i];
return *this;
}
#endif
测试程序
//stkoptr1.cpp
#include <iostream>
#include <cstdlib> //for rand(),srand()
#include <ctime> //for time()
#include "stcktp1.h"
const int Num=10;
int main()
{
std::srand(std::time(0)); //randomize rand()
std::cout<<"Please enter stack size: ";
int stacksize;
std::cin>>stacksize;
//create an empty stack with stacksize slots
Stack<const char *> st(stacksize);
//in basket
const char *in[Num]={
"1: Hank Gilgamesh","2: Kiki Ishtar",
"3: Betty Rocker","4: Ian Flagranti",
"5: Wolfgang Kibble","6: Portia Koop",
"7: Joy Almondo","8: Xaverie Paprika",
"9: Juan Moore","10: Misha Mache"
};
//out basket
const char *out[Num];
int processed=0;
int nextin=0;
while(processed<Num)
{
if(st.isempty())
st.push(in[nextin++]);
else if(st.isfull())
st.pop(out[processed++]);
else if(std::rand()%2 && nextin<Num) //50-50 chance
st.push(in[nextin]++);
else
st.pop(out[processed++]);
}
for(int i=0;i<Num;i++)
std::cout<<out[i]<<std::endl;
std::cout<<"Bye\n";
return 0;
}