C语言实验——数组逆序
来源:互联网 发布:2017网络热曲 编辑:程序博客网 时间:2024/05/22 09:49
题目描述
输入
输出
示例输入
5 1 2 3 4 52
示例输出
4 5 1 2 3
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int maxsize = 10000;
typedef int elemtype;
typedef struct Stack
{
int Size;
elemtype *top,*base;
} Stack;
bool Empty(Stack &s)//判断是否为空栈;
{
if (s.top == s.base)
{
return 1;
}
return 0;
}
void Creat(Stack &s)//栈的初始化;
{
s.base=new elemtype[maxsize];///
s.top=s.base;
// s.top++;
// s.base++;
s.Size=maxsize;
}
void push(Stack &s,elemtype e)//进栈;
{
if (s.top-s.base >= s.Size)
{
s.base=(elemtype *)realloc(s.base,2*maxsize*sizeof(Stack));
s.Size+=maxsize;
///s.top=s.base+s.Size;
}
s.top++;
*s.top=e;
}
void pop(Stack &s)//出栈
{
if (s.top != s.base)
{
s.top--;
}
}
void print(Stack &s)//栈内所有元素的输出;
{
while (!Empty(s))
{
cout<<*s.top;
pop(s);
}
cout<<endl;
}
void Clear(Stack &s)//清空栈;
{
while (!Empty(s))
{
pop(s);
}
}
void exch(Stack &s,int a[],int m,int n)//数组前每m个元素和过后m个元素互换位置;
{
int i;
for (i=n; i>n-m; i--)
{
push(s,a[i]);
}
while(!Empty(s))
{
cout<<*s.top<<" ";
s.top--;
}
for (i=n-m; i>=1; i--)
{
push(s,a[i]);
}
while(!Empty(s))
{
cout<<*s.top;
s.top--;
if (!Empty(s))
{
cout<<" ";
}
}
cout<<endl;
}
int main()
{
int a[1000];
int m,i,n;
Stack s;
Creat(s);
cin>>n;
for (i=1; i<=n; i++)
{
cin>>a[i];
}
cin>>m;
exch(s,a,m,n);//数组前每m个元素和过后m个元素互换位置;
print(s);
return 0;
}
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验——数组逆序
- C语言实验题——数组逆序
- 1186C语言实验——数组逆序
- C语言实验题——数组逆序
- Problem H: C语言实验——数组逆序
- 第十三周 C语言实验——数组逆序
- 第十四周-C语言实验——数组逆序
- C语言实验——数组逆序 (sdut oj)
- AOS 自动生成代码(六) 生成JSP文件
- 【杭电】[5256]序列变换
- java-----NIO总结(四)
- Android拦截电话
- JS获取文本框焦点光标位置、选中起始位置、终止位置、选择内容
- C语言实验——数组逆序
- ACDream 1735 输油管道
- POJ2151——Check the difficulty of problems
- hdu 5826 physics (物理数学)
- 线程小球
- 常见小错误补坑系列
- MySql安装后无法启动服务
- AngularJS RootScope 源码分析
- HDU----2955