二维数组转化成一维指针

二维数组转化为一维指针来使用本实例用到了随机数,链表生成,遍历,有待扩展

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<time.h>
  4 #include<math.h>
  5 //定义个结构体Emp用来存放员工信息
  6 typedef struct  Emp
  7 {
  8     int eno;
  9     char *ename;
 10     int dno;
 11     Emp *next;
 12
 13 }emp,*pemp;
 14 //定义个结构体Emp用来存放部门信息
 15 typedef struct Dept
 16 {
 17     int dno;
 18     char *dname;
 19     Dept *next;
 20 }dept,*pdept;
 21 /**
 22     Description: 每次调用,随机产生人名所在数组的地址,用来初始化员工信息 ,
 23
 24     Arguments:
 25
 26     Returns:返回数组下标地址
 27
 28 */
 29 char * rand_ename(char ename[12][15],int  rand_num)
 30 {
 31
 32     return (ename[0]+rand_num*15);
 33 }
 34 /**
 35     Description: 用来随机产生0-11之间的随机数
 36
 37     Arguments:
 38
 39     Returns: 0-11之间的随机数
 40
 41 */
 42 int rand_num()
 43 {
 44     return (rand()%12)*15;
 45 }
 46 /**
 47     Description: 用来产生0-2之间的随机数 ,用来标记部门号 使用的时候再加上11,
 48                     也就是部门号最终为11,12,13
 49
 50     Arguments:
 51
 52     Returns:0-2之间的随机数
 53
 54 */
 55 int rand_dno()
 56 {
 57
 58     return rand()%8;
 59 }
 60 /**
 61     Description: 初始化emp,随机产生一个emp表
 62
 63     Arguments:
 64
 65     Returns:返回pemp类型一个头指针
 66
 67 */
 68 pemp init_emp(char ename[][15])
 69 {
 70     int num_people=10;
 71     int i;
 72     pemp emp_head=(pemp)malloc(sizeof(pemp)+20);
 73     //申请空间失败自动退出
 74     if(emp_head==NULL)
 75     {
 76         exit(1);
 77     }
 78     pemp tem=emp_head;
 79     //srand放到for循环起不到效果,for循环所用时间非常短,随机数来不及产生
 80     srand((unsigned)time(0));
 81     for(i=0;i<num_people ;i++ )
 82     {
 83         pemp new_emp=(pemp)malloc(sizeof(pemp)+5);
 84         tem->next=new_emp;
 85         new_emp->eno=i+1;
 86         new_emp->ename=ename[0]+rand_num();
 87         new_emp->dno=rand_dno();
 88         tem=new_emp;
 89     }
 90     tem->next=NULL;
 91     return emp_head;
 92 }
 93 /**
 94     Description: 初始化部门信息
 95
 96     Arguments:
 97
 98     Returns:返回pdept类型的一个头指针
 99
100 */
101 pdept init_dept(char dpartname[][15])
102 {
103     pdept dept_head=(pdept)malloc(sizeof(pdept));
104     if(dept_head==NULL)
105     {
106         exit(1);
107     }
108     int i;
109     pdept tem=dept_head;
110     for(i=0;i<8;i++)
111     {
112         pdept new_dept=(pdept)malloc(sizeof(pdept));
113         tem->next=new_dept;
114         new_dept->dno=i+11;
115         new_dept->dname=(dpartname[0]+i*15);
116         tem=new_dept;
117     }
118     tem->next=NULL;
119     return dept_head;
120 }
121 int main()
122 {    int i;
123      //数组用来初始化信息所用
124     char ename[12][15]={
125         "范冰冰","贾玲","刘亦菲","凤姐","baby","曹敏","张馨予","林志玲"
126         ,"蒋勤勤","张静初","奶茶妹","搞基"
127     };
128     char dpartname[8][15]={
129         "蔡兴家","张家晨","崔熊华","王强","吴未","王寅","杜桂宇","彭清泉"
130     };
131     pemp head=(pemp)malloc(sizeof(pemp));
132     if(head==NULL)
133     {
134         exit(1);
135     }
136     head=init_emp(ename);
137     pemp temp ;
138     temp=head->next;
139
140     while(temp->next!=NULL)
141     {
142         printf("%d\t",temp->eno);
143         printf("%s\t",temp->ename);
144         printf("%s\t",dpartname[temp->dno]);
145         printf("\n");
146         temp=temp->next;
147     }
148        printf("\n");
149     return 0;
150
151 }

这个程序运行结果实际上是为随即产生的女明星匹配一个男神!程序可扩展空间很大!

时间: 2024-08-05 13:26:47

二维数组转化成一维指针的相关文章

HDU 1024 Max Sum Plus Plus(二维数组转化为一维数组)

Problem Description: Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. Given a consecutive number seq

怎么把大数据的二维数组转化为一维数组????

今天做了一场比赛发现一个题卡死在数据上,原本要开一个二维数组a[2e5][2e5]但是这样肯定要re.我一直卡死在这个地方,比赛中想到了一个方法. 那就是用string数组去计数,那样就可以开一个st[2e5]的数组就可以了...然而这样伴随着一些问题.我发现直接用st[i][j]=t;这样是不对的,输出发现没有输出数据!!! 后来我发现必须先把string数组的每一个元素先赋值一个东西,因为我表示的是一个矩阵,所以我把string数组的每一个元素,也是一个string,这里定义为ss了,那么如

PHP二维数组转换成一维数组,一个变量多个二维数组转换成一维数组,PHP二维数组(或任意维数组)转换成一维数组的方法汇总,array_reduce(); array_walk_recursive(); array_map();

方法汇总: 1. array_reduce函数法 //用array_reduce()函数是较为快捷的方法: $result = array_reduce($user, function ($result, $value) { return array_merge($result, array_values($value)); }, array()) 2. array_walk_recursive函数法 //用array_walk_recursive()函数就非常灵活,可以把任意维度的数组转换成一

PHP 将二维数组转成一维数组

$authArrs = array();  //待转的数组 $authIds = array(); //声明一个空数组 array_walk_recursive($authArrs, function($value) use (&$authIds) { array_push($authIds, $value); }); 原文地址:https://www.cnblogs.com/soiq-1123/p/9450993.html

将二维数组转换成一维数组(基于reduce)

reduce:不改变原数组,返回一个新的数组.就是遍历数组元素,从头开始,依次往下,第一个参数是上一次的返回值,第二个参数是下一个数组元素,首次的时候第一个和第二个参数分别是 array[0],  array[1] : let flat=[[1,2,3],[4,5,6],[6,7,8]].reduce(function(prev,next){ return prev.concat(next);//循环将数组进行拼接 }); console.log(flat); 原文地址:https://www.

将二维数组转换成一维,键值互换

1 $arr_Array = array_reduce($rankLevel,function(&$arr_Array,$val) { 2 $arr_Array[$val['level']] = $val['name']; 3 return $arr_Array; 4 }); 原文地址:https://www.cnblogs.com/ghjbk/p/9689802.html

reduce实现计算数组中每个元素出现的次数 数组去重 将多维数组转化为一维

// js计算数组中每个元素出现的次数 // var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']; // var countedNames = names.reduce(function (allNames, name) { // if (name in allNames) { // allNames[name]++; // } // else { // allNames[name] = 1; // } // return allName

C# 二维数组 转换成 DataTable

C# 数据转换 Overview C# 窗体操作中,有些比较特别的操作.但是为了方便我们不得不使用一些比较特别的手段. C#中二维数组转DataTable 首先,我们看一下我对二维数组的数据处理.这次我是将Excel表格中的数据,读成二维数组的格式的.看一下代码: 从Excel中读取数据并转换成二维数组 string FileNmae = System.IO.Directory.GetCurrentDirectory(); Excel.Application Excel_Reader = new

二维数组和它的指针

这片文章介绍二维数组int a[ i ] [ j ]中的符号:a,&a[ i ],&a[ i ][ j ],a[ i ][ j ],&a[ i ] [ j ]的含义,重点在后面第二部分的分析,但是前面第一部分的复习数组指针,指针数组和二维指针是前提,前面的不会,后面的没法理解.这片文章写的有些费劲,个人能力不够,可能会有错误,希望各位朋友能够指正,共同进步.//握手 一:先回顾一下数组指针,指针数组,二维指针 先根据逐层分析分方法分析下面的代码 (1)int (*p)[5]; (2