双向栈.cpp

来自「我们的作业题」· C++ 代码 · 共 43 行

CPP
43
字号
#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 + =
减小字号Ctrl + -
显示快捷键?