这是一个相当简单的 Stack 实现程序。push()
但是,pop()
和display()
函数的声明存在问题。错误说明如下。请帮我找出问题所在。
错误:
error C2664: 'push' : cannot convert parameter 1 from 'int [10]' to 'int'
error C2664: 'pop' : cannot convert parameter 1 from 'int [10]' to 'int'
error C2664: 'display' : cannot convert parameter 1 from 'int [10]' to 'int'
程序:
#include<iostream>
#define STACKSIZE 10
using namespace std;
void push(int,int,int);
void pop(int,int);
void display(int,int);
int main()
{
int stack[STACKSIZE],n,data,stackptr=-1;
while(1)
{
cout<<"1. Push\n2. Pop\n3. Display\n4. Exit\n";
cin>>n;
switch(n)
{
case 1:
cout<<"\nEnter a data to push: ";
cin>>data;
push(stack,stackptr,data);
break;
case 2:
pop(stack, stackptr);
break;
case 3:
display(stack, stackptr);
break;
case 4:
exit(1);
break;
default:
cout<<"\nEnter correct choice...\n\n";
}
}
system("pause");
return 0;
}
void display(int stack[STACKSIZE], int &stackptr)
{
cout<<"\n\n";
for(int i=0;i<stackptr;i++)
cout<<stack[i]<<"\n";
cout<<"\n\n";
}
void push(int stack[STACKSIZE],int &stackptr, int data)
{
if(stackptr == STACKSIZE - 1)
{
cout<<"\n\nStack full\n\n";
return ;
}
stackptr++;
stack[stackptr] = data;
}
void pop(int stack[STACKSIZE], int &stackptr)
{
if(stackptr == 0)
{
cout<<"\n\nStack Empty\n\n";
return ;
}
stackptr = stackptr-1;
}