栈求助
  • 板块学术版
  • 楼主2008shitou
  • 当前回复1
  • 已保存回复1
  • 发布时间2020/5/30 22:09
  • 上次更新2023/11/7 01:25:03
查看原帖
栈求助
263701
2008shitou楼主2020/5/30 22:09
//有问题
#include <bits/stdc++.h>
using namespace std;
int a[]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int le=sizeof(a) / sizeof(int)-2;
int boom(int y){
	while (a[(y-1)]==0){
		y=y-1;
	}
	return y;
}

void come(int x){
	if (x==0){
		cout<<"栈存在错误,无法加入\n";
	}else{
		a[le] = x;
        int z = boom(le);
        a[z] = x;
        a[le] = 0;
        cout<<"栈操作完成!\n";
	}
}

int out(){
	int x=le;
    if (a[0] == 0){
    	cout<<"栈错误:无数据\n";
	}else{
    	while (a[(x - 1)] == 0)
            x = (x - 1);
        int z = a[(x - 1)];
        a[(x - 1)] = 0;
        cout<<"栈操作完成!\n";
        return z;
	}
        
}   

int main() {
cout<<"c++栈,本栈由Python改编而来\n";
cout<<"c++栈1.0 -改\n";
while (1){
	cout<<"1:放入数据(0除外)   2:取出数据   3:退出   其他:读栈  请输入:";
    int z;
	cin>>z;
    if (z==1){
    	cout<<"数据?";
    	int y;
    	cin>>y;
        come(y);
	}else if (z==2){
		cout<<out();
	}else if (z==3){
		break;
	}
    else{
    	for (int i=0;i<=le;i++){
    		cout<<a[1]<<" ";
		}
		cout<<"\n";
	} 
}
	  
        
cout<<"栈已退出";   

	return 0;
}


#很正常
a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
le=len(a)
def boom(y):
#栈前置
    while (a[(y - 1)] == 0):
        y = (y - 1)
    return y

def come(x):
#入栈    
    if (x==0):
        print("栈存在错误,无法加入")
    else:    
        a[le] = x
        z = boom(le)
        a[z] = x
        a[le] = 0
        print('栈操作完成!')

def out():
#出栈    
    x=le;
    if (a[0] == 0):
        return '栈错误:无数据'
    else:
        while (a[(x - 1)] == 0):
            x = (x - 1)
        z = a[(x - 1)]
        a[(x - 1)] = 0
        print('栈操作完成!')
        return z

print('python栈1.0')
while 1:
    z=input('1:放入数据(0除外)   2:取出数据   3:退出   其他:读栈  请输入:')
    if (z=='1'):
        x=int(input('数据:'))
        come(x)
    elif (z=='2'):
        print (out())
    elif (z=='3'):
        break
    else:    
        print("栈内容:",a)
print('栈已退出')
2020/5/30 22:09
加载中...