建立顺序存储的栈,并对之进行入栈、出栈、取栈顶元素操作的c语言算法
2个回答
展开全部
#include "process.h"
#include "stdio.h"
#include "assert.h"
const int stackIncreament=20; //栈溢出时扩展空间的增量
class SeqStack
{
private:
int top; //栈顶指针
int MAX; //栈的最大可容纳个数
int *elements; //存放栈中元素的栈数组
void overflowProcess(); //栈的溢出处理
public:
SeqStack(int sz=50); //构造函数
~SeqStack() { delete[]elements; } //析构函数
bool pop1(int & x); //元素出栈
void push1(const int & x); //新元素进栈
bool IsEmpty1()const; //判断栈空与否
bool IsFull1()const; //判断栈满与否
void output1(); //输出元素进栈顺序
void output(int x); //输出x
};
SeqStack::SeqStack(int sz):top(-1),MAX(sz)
{
elements=new int[MAX]; //创建栈的数组空间
assert(elements!=NULL); //断言:动态存储分配成功与否
}
bool SeqStack::pop1(int & x) //栈顶元素出栈
{
if(IsEmpty1()==true) return false;//判栈空否,若栈空则函数返回
x=elements[top--]; //栈顶指针退1
return true; //退栈成功
}
void SeqStack::push1(const int & x) //新元素进栈
{
if(IsFull1()==true) overflowProcess(); //栈满则溢出处理
elements[++top]=x; //栈顶指针先加1,再进栈
}
bool SeqStack::IsEmpty1() const //判断栈空与否
{
return (top==-1)?true:false;
}
bool SeqStack::IsFull1()const //判断栈满与否
{
return (top==MAX-1)?true:false;
}
void SeqStack::overflowProcess() //栈的溢出处理
{
//私有函数,扩充栈的存储空间。
int *Array=new int[MAX+stackIncreament]; //和课本不一样 ??????????
if(Array==NULL)
{
printf("存贮分配失败 ! \n");
exit(1);
}
for(int i=0;i<=top;i++) Array[i]=elements[i];
MAX=MAX+stackIncreament;
delete []elements;
//elements=Array;
}
void SeqStack::output1() //元素入栈顺序输出
{
int n=0;
int t=top;
for(int i=0;i<top;i++)
{
printf(" %d",elements[i]);
n++;
if(n%10==0)
printf("\n");
}
}
void SeqStack::output(int x) //栈内元素输出
{
printf(" %d",x);
}
//----------------------顺序栈函数--------------------------//
void SeqStack1( SeqStack A)
{
int x=-1;
int X;
printf("请输入要入栈A的元素值,以0结束:\n");
while(x!=0){ //新元素进栈
scanf("%d",&x);
A.push1(x);
}
printf("\n元素进栈顺序是 :");
A.output1();
printf("\n\n");
A.pop1(X); //元素出栈
if(!A.pop1(X))
printf("元素出栈失败 !\n");
else
{
printf("\n栈顶元素是: ");
A.output(X);
printf("\n");
printf("\n元素出栈的结果是 : ");
A.output(X);
while(A.pop1(X))
A.output(X);
}
}
void main()
{
printf("----------顺序栈的调试----------\n");
printf("\n \n");
SeqStack A;
SeqStack1(A);
printf("\n \n");
}
#include "stdio.h"
#include "assert.h"
const int stackIncreament=20; //栈溢出时扩展空间的增量
class SeqStack
{
private:
int top; //栈顶指针
int MAX; //栈的最大可容纳个数
int *elements; //存放栈中元素的栈数组
void overflowProcess(); //栈的溢出处理
public:
SeqStack(int sz=50); //构造函数
~SeqStack() { delete[]elements; } //析构函数
bool pop1(int & x); //元素出栈
void push1(const int & x); //新元素进栈
bool IsEmpty1()const; //判断栈空与否
bool IsFull1()const; //判断栈满与否
void output1(); //输出元素进栈顺序
void output(int x); //输出x
};
SeqStack::SeqStack(int sz):top(-1),MAX(sz)
{
elements=new int[MAX]; //创建栈的数组空间
assert(elements!=NULL); //断言:动态存储分配成功与否
}
bool SeqStack::pop1(int & x) //栈顶元素出栈
{
if(IsEmpty1()==true) return false;//判栈空否,若栈空则函数返回
x=elements[top--]; //栈顶指针退1
return true; //退栈成功
}
void SeqStack::push1(const int & x) //新元素进栈
{
if(IsFull1()==true) overflowProcess(); //栈满则溢出处理
elements[++top]=x; //栈顶指针先加1,再进栈
}
bool SeqStack::IsEmpty1() const //判断栈空与否
{
return (top==-1)?true:false;
}
bool SeqStack::IsFull1()const //判断栈满与否
{
return (top==MAX-1)?true:false;
}
void SeqStack::overflowProcess() //栈的溢出处理
{
//私有函数,扩充栈的存储空间。
int *Array=new int[MAX+stackIncreament]; //和课本不一样 ??????????
if(Array==NULL)
{
printf("存贮分配失败 ! \n");
exit(1);
}
for(int i=0;i<=top;i++) Array[i]=elements[i];
MAX=MAX+stackIncreament;
delete []elements;
//elements=Array;
}
void SeqStack::output1() //元素入栈顺序输出
{
int n=0;
int t=top;
for(int i=0;i<top;i++)
{
printf(" %d",elements[i]);
n++;
if(n%10==0)
printf("\n");
}
}
void SeqStack::output(int x) //栈内元素输出
{
printf(" %d",x);
}
//----------------------顺序栈函数--------------------------//
void SeqStack1( SeqStack A)
{
int x=-1;
int X;
printf("请输入要入栈A的元素值,以0结束:\n");
while(x!=0){ //新元素进栈
scanf("%d",&x);
A.push1(x);
}
printf("\n元素进栈顺序是 :");
A.output1();
printf("\n\n");
A.pop1(X); //元素出栈
if(!A.pop1(X))
printf("元素出栈失败 !\n");
else
{
printf("\n栈顶元素是: ");
A.output(X);
printf("\n");
printf("\n元素出栈的结果是 : ");
A.output(X);
while(A.pop1(X))
A.output(X);
}
}
void main()
{
printf("----------顺序栈的调试----------\n");
printf("\n \n");
SeqStack A;
SeqStack1(A);
printf("\n \n");
}
展开全部
看看还可以吧,我运行过了的
#include "stdio.h"
#define MAXN 26
char stack[MAXN];
int top=0;
int push(char x)
{if (top >= MAXN)
return(1);
stack[top++]=x;
return(0);
}
int pop(char *p_y)
{if (top == 0)
return(1);
*p_y = stack[--top];
return(0);
}
main()
{ int i;
char ch_x,ch_y;
printf("input the char you want to push\n");
scanf("%c",&ch_x);
while(ch_x!='0')
if (push(ch_x)==1) printf("failure!\n");
else
{printf("success!\n");
printf("input a char for ch_x to push\nch_x=");
getchar();
scanf("%c",&ch_x);}
i=0;
while(stack[i]!='\0')
{printf("%c ", stack[i]);
i++;} /*把输入的字符按次序输出*/
if (pop(&ch_y)==1) printf("failure!\n");
else
{printf("success!\n");
printf("The pop char is %c\n",ch_y);}
for (i=top-1; i>=0; i--)
printf("%c ", stack[i]);
}
#include "stdio.h"
#define MAXN 26
char stack[MAXN];
int top=0;
int push(char x)
{if (top >= MAXN)
return(1);
stack[top++]=x;
return(0);
}
int pop(char *p_y)
{if (top == 0)
return(1);
*p_y = stack[--top];
return(0);
}
main()
{ int i;
char ch_x,ch_y;
printf("input the char you want to push\n");
scanf("%c",&ch_x);
while(ch_x!='0')
if (push(ch_x)==1) printf("failure!\n");
else
{printf("success!\n");
printf("input a char for ch_x to push\nch_x=");
getchar();
scanf("%c",&ch_x);}
i=0;
while(stack[i]!='\0')
{printf("%c ", stack[i]);
i++;} /*把输入的字符按次序输出*/
if (pop(&ch_y)==1) printf("failure!\n");
else
{printf("success!\n");
printf("The pop char is %c\n",ch_y);}
for (i=top-1; i>=0; i--)
printf("%c ", stack[i]);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询