⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 双向栈.cpp

📁 我们的作业题
💻 CPP
字号:
#include<iostream>
#include <stdio.h>
using namespace std;
void main()
{   
	int *array,*p;    //定义数组
	int i=0,j=0;
	int m,k;
	cout<<"定义数组长度m"<<endl;
	cin>>m;                   //设定栈长
	array=(int*)malloc(m*10);
	p=(int*)malloc(m*10);;
	int *top1,*top2;
	k=m-1;
	top1=&array[0];            
	top2=&array[m-1];
	cout<<"输入一串整数"<<endl;
	for(i=0;i<m;i++)
	{   
		if(top1==top2+1)  break;
		cin>>array[i];
		if(array[i]%2!=0)       //奇数从栈头存入
		{
			p[j]=array[i];
		    j++;
			top1++;
		}
		else                  //偶数从栈尾存入
		{
			p[k]=array[i];
		    k--;
			top2--;
		}
			
	}
	cout<<"栈中元素为:"<<endl;
	for(i=0;i<m;i++)
	{	
	cout<<p[i]<<" ";
	}
	cout<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -