#include <stdio.h> /*此标准库用于输入输出*/
#include <string.h> /*与字符串相关的标准库;字符的复制等*/
#include <stdlib.h> /*这个与动态申请存储空间相关*/
/* 顺序表以及初始化---这里我们先使用数组来表示顺序表
1 定义数组
1.1 一个未不确定大小的数组
1.2 添加元素之后的数组长度
1.3 数组的大小(静态数组,数组的大小是固定的)
2 数组初始化
3 向数组中添加元素
4 打印数组元素
*/
#define Size 5
typedef int EleType;
//定义数组
typedef struct Table{
EleType *head;/*数组*/
EleType len;
EleType size;
}Table_define;
/*数组初始化
1 这个Init_Table函数的类型是Table_define;这意味着这个函数Init_Table的返回值是更新之后的Table_define
2 换句话讲,函数Init_Table是结构体Table_define的扩充;这个好像不好理解,还是看第一点吧
3 返回值
3.1 确定的数组(静态数组)
3.2 数组的大小
3.3 元素长度初始化为0
*/
Table_define Init_Table(){
Table_define Tab_def;
Tab_def.head = (EleType*)malloc(sizeof(Size));
if(!Tab_def.head){
printf("初始化失败!");
}
Tab_def.len = 0;
Tab_def.size = Size;
return Tab_def;
}
/*添加元素---3个参数
1 需要添加的元素ele
2 添加到数组的位置下标i(从0开始)
3 需要添加的数组
*/
//void Add_Ele(EleType ele,EleType i,Table_define t){
// t.head[i]=ele;
// t.len++;
//}
int main(){
Table_define t = Init_Table();
/*添加元素*/
for(int i=0;i<=Size;i++){
t.head[i]=i;
t.len++;
}
/*打印元素*/
for(int i=1;i<t.len;i++){
printf("%d",t.head[i]);
printf("\n");
}
return 0;
}
实际上,更简便的方式:
#include <stdio.h> /*此标准库用于输入输出*/
#include <string.h> /*与字符串相关的标准库;字符的复制等*/
#include <stdlib.h> /*这个与动态申请存储空间相关*/
int main(){
int nums[10];
int i;
//控制台输入
printf("---Input---\n");
for(i=0;i<10;i++){
scanf("%d",&nums[i]);
}
//输出数组元素
printf("Output\n");
for(i=0;i<10;i++){
printf("%d",nums[i]);
printf("----\n");
}
return 0;
}
--------
---Input---
1
2
3
4
5
6
7
8
9
0
Output
1----
2----
3----
4----
5----
6----
7----
8----
9----
0----
Program ended with exit code: 0