定义:顺序表的顺序存储结构是把线性表中的所有元素按照其逻辑顺序依次存储到从计算机存储器中指定位置开始的一块连续的存储空间中。

 

 定义线性表结构体:

#include <iostream>
using namespace std;

#define MaxSize 50
typedef int ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int length;
}Sqlist;
View Code

1.建立顺序表

 1 #include <iostream>
 2 using namespace std;
 3 
 4 #define MaxSize 50
 5 typedef int ElemType;
 6 typedef struct
 7 {
 8     ElemType data[MaxSize];
 9     int length;
10 }Sqlist;
11 
12 //创建顺序表
13 void CreateList(Sqlist * &L, ElemType a[], int n)
14 {
15     int i = 0, k = 0;
16     L = (Sqlist *)malloc(sizeof(Sqlist));
17     while (i<n)
18     {
19         L->data[k] = a[i];
20         k++, i++;
21     }
22     L->length = k;
23 }
24 int main()
25 {
26     ElemType a[] = {1,3,5,7,9,12,89,100};
27     Sqlist * L = {};
28     CreateList(L, a, 5);
29     for (size_t i = 0; i < L->length; i++)
30     {
31         printf("%d\n", L->data[i]);
32     }
33     getchar();
34     return 0;
35 }
View Code

运行结果:

 

在书中使用顺序表指针方式来建立顺序表,其含义如下图:

 

posted on 2019-07-08 00:40  西风寞  阅读(663)  评论(0编辑  收藏  举报