【数据结构】二维数组
来源:互联网 发布:淘宝客越来越难做 编辑:程序博客网 时间:2024/06/09 18:58
#include"stdafx.h"
//二维数组的操作
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ElemType;
//定义数组结构体类型
typedef struct{
ElemType *base;
inth;
intl;
intelem_count;
ElemType *bounds;
}Array;
//输出数组
int Print_Array(Array &A){
int*p;
inti=1;
printf("当前的数组为:\n");
while(i<=A.h)
{
for(p=A.bounds;p<A.bounds+A.l;p++)
printf("%d ",*p);
printf("\n");
A.bounds=A.bounds+A.l;
i++;
}
A.bounds=A.base;
returnOK;
}
//建立数组
int InitArray(Array &A,intx,int y){
int*p;
inti=1;
A.h=x;
A.l=y;
A.elem_count=x*y;
A.base=(ElemType*)malloc(A.elem_count*sizeof(ElemType));
A.bounds=A.base;
while(i<=A.h)
{
printf("请输入第%d行元素:\n",i);
for(p=A.bounds;p<A.bounds+A.l;p++)
scanf("%d",p);
i++;
A.bounds=A.bounds+A.l;
}
A.bounds=A.base;
Print_Array(A);
returnOK;
}
//查找元素
int Seek_Elem(Array &A,intx,int y){
printf("要查找的元素的值为:\n%d",*(A.base+A.l*(x-1)+(y-1)));
returnOK;
}
//修改元素
int Revise_Elem(Array &A,intx,int y){
inta;
printf("请输入新数据:\n");
scanf("%d",&a);
*(A.base+A.l*(x-1)+(y-1))=a;
Print_Array(A);
returnOK;
}
//二维数组转置
int Transpose_Array(Array &A){
Array B;
int*p,*q;
inti=1;
B.base=A.base;
B.bounds=A.bounds;
B.h=A.h;
B.l=A.l;
B.elem_count=A.elem_count;
A.base=(ElemType*)malloc(A.elem_count*sizeof(ElemType));
A.bounds=A.base;
A.h=B.l;
A.l=B.h;
A.elem_count=A.h*A.l;
while(i<=A.h)
{
q=B.bounds;
for(p=A.bounds;p<A.bounds+A.l;p++)
{*p=*q;
q=q+B.l;}
B.bounds++;
A.bounds=A.bounds+A.l;
i++;
}
A.bounds=A.base;
Print_Array(A);
returnOK;
}
//销毁数组
int Destroy_Array(Array &A){
free(A.base);A.bounds=A.base=NULL;
A.h=A.l=A.elem_count=0;
printf("二维数组已销毁,请中止对它的操作!\n");
returnOK;
}
//主函数
void main()
{
Array A;
intx,y;
printf("请输入数组的行数:\n");
scanf("%d",&x);
printf("请输入数组的列数:\n");
scanf("%d",&y);
InitArray(A,x,y);
intflag=1,select;
printf("====================菜单============================\n");
printf("= 1.查找元素 =\n");
printf("= 2.修改元素 =\n");
printf("= 3.二维数组转置 =\n");
printf("= 4.销毁数组 =\n");
printf("= 5.退出操作 =\n");
printf("================支持乱序选择========================\n");
while(flag)
{
printf("\n请选择菜单中的操作选项:\n");
scanf("%d",&select);
switch(select)
{
case1:
printf("请输入要查找的元素所在行:\n");
scanf("%d",&x);
printf("请输入要查找的元素所在列:\n");
scanf("%d",&y);
Seek_Elem(A,x,y);break;
case2:
printf("请输入要修改的元素所在行:\n");
scanf("%d",&x);
printf("请输入要修改的元素所在列:\n");
scanf("%d",&y);
Revise_Elem(A,x,y);break;
case3:
Transpose_Array(A);break;
case4:
Destroy_Array(A);break;
case5:
flag=0;break;
default:
printf("您输入的数据有误!\n");
}
}
}
- 【数据结构】二维数组
- 【数据结构】二维数组中的查找
- 数据结构动态申请二维数组
- 数据结构-二维数组-存储结构
- 数据结构题集--数组(二维数组)
- 数据结构 二维数组表示一维数组
- 数据结构-用二维数组构造列表
- 数据结构与二维数组 json、xml
- HDU 2642 Stars [二维树状数组]【数据结构】
- 数据结构-二维数组-特殊矩阵压缩存储
- 数据结构-二维数组-三角矩阵压缩存储
- 数据结构-二维数组-对称矩阵压缩存储
- 【数据结构】C++中一维数组与二维数组的手动调用
- 数据结构-C++描述:动态创建和释放一个二维数组
- 一步一步学数据结构之(动态申请二维数组)
- 剑指offer 2.3 数据结构2-二维数组中的查找
- 剑指offer 数据结构与算法 二维数组查找 java 实现
- python数据结构学习笔记-2016-10-14-03-二维数组
- 25.Oracle数据库SQL开发之 SQLPlus使用——保存、检索并运行文件
- 黑马程序员-----线程
- 物联网操作系统HelloX已成功移植到MinnowBoard MAX开发板上
- netty rpc 框架
- Android分享到微信好友、朋友圈
- 【数据结构】二维数组
- rsync同步工具
- Android工程出现错误Unable to resolve target 'android-19'
- 26.Oracle数据库SQL开发之 SQLPlus使用——格式化列
- Android相关文章的规划
- 【数据结构】哈夫曼编码
- C#学习笔记 反射
- Qt改变tabWidget标题框背景
- 集体智慧和协同过滤