数据结构二 顺序表的创建

纯属自学内容

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 /****
 5 ****顺序表的操作
 6 ******/
 7 typedef int ElemType;
 8 #define LIST_SIZE 100 
 9 #define LIST_SPACE 10
10 typedef struct
11 {
12     int len;
13     int size;
14     ElemType *elem;
15 }SqList;
16 //初始化顺序表
17 SqList *InitList(){
18     SqList *L=(SqList *)malloc(sizeof(SqList));
19     if(!L){
20         printf("存储空间分配失败,程序退出!");
21         return NULL;
22     }
23     L->elem=(ElemType *)malloc(sizeof(ElemType));
24     if(!L->elem){
25        printf("存储空间分配失败,程序退出!");
26         return NULL;
27     }
28     L->len=0;
29     L->size=LIST_SIZE;
30     return L;
31 }
32 SqList *CreateList(SqList *L){
33     int num,i;
34     printf("请输入顺序表的长度
");
35     scanf("%d",&num);
36     if(num>LIST_SIZE)
37     {
38         printf("请输入个数小于表的长度的数
");
39         return ;
40     }
41     for(i=0;i<num;i++){
42         printf("请输入第%d个数:",i+1);
43         scanf("%d",L->elem+i);
44         L->len++;
45         L->size+=LIST_SPACE;
46     }
47     return L;
48 }
49 void printList(SqList *L){
50     int j;
51     for(j=0;j<L->len;j++){
52         printf("顺序表的第%d个元素是:",j+1);
53         printf("%d
",L->elem[j]);
54     }
55 }
56 void main(){
57     SqList *List=InitList();
58     List=CreateList(List);
59     printList(List);
60     system("pause");
61 }

运行结果:

  

原文地址:https://www.cnblogs.com/huangc/p/5713150.html