代码如下,好好看看,有问题追问:
#include "stdafx.h"
#include
using namespace std;
typedef int DataType;
const int MaxStatckSize = 50; //栈大小
class Stack
{
private:
DataType stacklist[MaxStatckSize];
int top;//栈顶
public:
//构造函数
Stack(void);
~Stack(void);
public:
//压栈出栈操作
void Push(const DataType &item);
DataType Pop(void);
void ClearStack(void);
//访问栈顶
DataType Peek(void)const;
//检测椎栈
bool isEmpty(void)const;
bool isFull(void)const;
};
Stack::Stack(void)
{
this->top = -1;
}
Stack::~Stack(void)
{
this->top = -1;
}
void Stack::Push(const DataType &item)
{
//栈是否已满
if(!isFull())
{ top += 1;
this->stacklist[top] = item;
}
else
std::cout << "Out of the Stack!" << std::endl;
}
DataType Stack::Pop(void)
{
if(!isEmpty())
{
int ebp = top;
top -= 1;
return stacklist[ebp];
}
else
return -1;
}
DataType Stack::Peek(void)const
{
return top;
}
void Stack::ClearStack()
{
for(int i = top;i >=0 ;i--)
stacklist[i] = 0;
top = -1;
std::cout << "Clear stack done!" << std::endl;
}
bool Stack::isFull(void)const
{
return top > MaxStatckSize?true:false;
}
bool Stack::isEmpty(void)const
{
return top < 0 ?true:false;
}
int main(int argc, _TCHAR* argv[])
{
Stack stack;
for(int i = 0; i < 5; ++i)
stack.Push(i);
if(stack.isFull())
cout<<"full"<else
cout<<"not full"<for(int i = 0; i < 5; ++i)
stack.Pop();
if(stack.isEmpty())
cout<<"empty"<else
cout<<"not empty"<system("pause");
return 0;
}
/*
请输入一个整数 : 36
请输入一个整数 : 25
请输入一个整数 : 14
请输入一个整数 : 85
请输入一个整数 : 65
请输入一个整数 : 45
Stack is full.
65 85 14 25 36
Press any key to continue
*/
#include
#include
#define M 5
int bookbox[M];
int top = 0;
bool isempty(void) {
return (top == 0);
}
bool isfull(void) {
return (top == M);
}
bool push(int e) {
if (!isfull()) {
bookbox[top] = e;
top++;
return true;
}
printf("Stack is full.\n\n");
return false;
}
bool pop(int *elem) {
if(!isempty()) {
top--;
*elem = bookbox[top];
return true;
}
printf("Stack is empty.\n\n");
return false;
}
int main(int argc, char *argv[]) {
int num,elem;
while(1) {
printf("请输入一个整数 : ");
scanf("%d",&num);
if(push(num)) continue;
else break;
}
while(!isempty()) {
pop(&elem);
printf("%d ",elem);
}
printf("\n\n");
return 0;
}
代码如下51好好看看dhl有问题追问:#include &quot;stdafx.h&quot;#include &lt;iostream&gt;using namespace std;typedef int DataType;const int MaxStatckSize = 50; &#47;&#47;栈大小class Stack{private:DataType stacklist[MaxStatckSize];int top;&#47;&#47;栈顶public:&#47;&#47;构造函数Stack(void);~Stack(void);public:&#47;&#47;压栈出栈操作void Push(const DataType &amp;item);DataType Pop(void);void ClearStack(void);&#47;&#47;访问栈顶DataType Peek(void)const;&#47;&#47;检测椎栈bool isEmpty(void)const;bool isFull(void)const;};Stack::Stack(void){ this-&gt;top = -1;} Stack::~Stack(void){ this-&gt;top = -1;} void Stack::Push(const DataType &amp;item){ &#47;&#47;栈是否已满 if(!isFull()) { top += 1; this-&gt;stacklist[top] = item; } else std::cout &lt;&lt; &quot;Out of the Stack!&quot; &lt;&lt; std::endl;} DataType Stack::Pop(void){ if(!isEmpty()) { int ebp = top; top -= 1; return stacklist[ebp]; } else return -1;} DataType Stack::Peek(void)const{ return top;} void Stack::ClearStack(){for(int i = top;i &gt;=0 ;i--)stacklist[i] = 0;top = -1;std::cout &lt;&lt; &quot;Clear stack done!&quot; &lt;&lt; std::endl;}bool Stack::isFull(void)const{return top &gt; MaxStatckSize?true:false;}bool Stack::isEmpty(void)const{ return top &lt; 0 ?true:false;}int main(int argc73 _TCHAR* argv[]){Stack stack;for(int i = 0; i &lt; 5; ++i)stack.Push(i);if(stack.isFull())cout&lt;&lt;&quot;full&quot;&lt;&lt;endl;elsecout&lt;&lt;&quot;not full&quot;&lt;&amp;......余下全文>>