//堆栈的实现,顺序表的实现
//编写一个C源程序,其中包含顺序表示的空栈的创建、判断栈是否为空、进栈、出栈、取栈顶元素等操作
}
//编写一个C源程序,其中包含顺序表示的空栈的创建、判断栈是否为空、进栈、出栈、取栈顶元素等操作
int main(){
SLNode *phead;
phead=InitiateHead(phead);
cout<<"首地址2.1:"<<phead<<endl;//此为头指针的地址
menu();
operation(phead);
return 0;
}
//操作实现代码:
//初始化
int initiate(Stack **s){
*s=(Stack *)malloc(sizeof(Stack));
(*s)->top= 0;
// cout<< s->top;
return 1;
}
//判断栈是否为空
int Empty_stack(Stack *s){
if(s->top<=0){
cout <<"栈为空!"<<endl;
return 0;
}else if(s->top>=MaxSize-1){
cout<<"栈满了,无法进行压栈操作!"<<endl;
return 3;
}else{
cout<<"栈不为空!"<<endl;
return 1;
}
}
//创建栈
int Create_Stack(Stack *s){
int i=0;
cout<<"请创建栈:"<<endl;
cout<<"______________________________top:"<<endl;
cout<<"期待有人能教我解决这个输入的问题,希望简单明了!"<<endl;
for(int i = 0;i < 10;i++){
if(s->top>=MaxSize){
break;
}
s->stack[s->top]=i+1;
s->top++;
}
cout<<"________________________________top:"<<endl;
/*cout<<"________________________________top:"<<endl;
for(int i = 0;;i++){
cin>>i;
if(s->top<MaxSize&&getchar()=='\n'){
break;
}
s->stack[s->top]=i+1;
s->top++;
}
cout<<"________________________________top:"<<s->top<<endl; *///这里有个问题希望有人能够指点,不知如何操作。。。求解,谢谢
return 1;
}
//输出栈内元素
int out_Stack(Stack *s){
for(int i = 0;i<s->top;i++){
cout<<s->stack[i]<<endl;
}
}
//压栈
int Push_Stack(Stack *s,DataType x){
if(Empty_stack(s)==3){
return 1;
}else{
s->stack[s->top] = x;
s->top++;
cout<<x<<endl;
return 1;
}
}
//出栈
int Stack_Pop(Stack *s){
Empty_stack(s);
cout<<"________________________________top:"<<s->top<<endl;
while(s->top!=NULL){
s->top--;
cout<<s->stack[s->top]<<endl;
}
cout<<"________________________________top:"<<s->top<<endl;
return 1;
}
//取栈顶元素
int Stack_Top(Stack *s,DataType *d){
if(s->top<= 0){
Empty_stack(s);
}else{
*d=s->stack[s->top-1];
}
return 1;
}
//主函数
int main(){
Stack *p;
int x;
initiate(&p);
Empty_stack(p);
Create_Stack(p);
cout<<"创建好后的栈中的元素:"<<endl;
out_Stack(p);
cout<<"压栈:"<<endl;
Push_Stack(p,100);
cout<<"出栈:"<<endl;
Stack_Pop(p);
cout<<"取栈顶元素:"<<endl;
Stack_Top(p,&x);
cout<<x<<endl;
return 0;
}
}