码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构线性表初接触1:线性表的顺序存储结构下基本运算的实现

时间:2018-07-26 11:36:42      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:spl   temp   ide   malloc   理解   def   false   for   功能   

刚刚接触数据结构这学问,通过听课看书后自行练习了用C语言实现线性表的基本运算,其中有许多点值得注意。这里存储各个功能分块的代码,以便自身的理解及以后的复习;

技术分享图片
 1 typedef int ElemType;
 2 #define MaxSize 50
 3 typedef struct {
 4     ElemType data[MaxSize];
 5     int length;
 6 }SqList;
 7 void CreateList(SqList *&L,ElemType a[],int n){
 8     int i=0,k=0;
 9     L=(SqList *)malloc(sizeof(ElemType));
10     while(i<n){
11         L->data[i]=a[i];
12         i++;k++;
13     }
14     L->length=k;
15 }
16 
17 
18 void InitList(SqList *&L){
19     L=(SqList * )malloc(sizeof(SqList)); 
20     L->length=0;
21 } 
22 
23 void DestroyList(SqList * &L){
24     free(L);
25 }
26 
27 bool ListEmpty(SqList *L){
28     return L->length;
29 }
30 
31 int ListLength(SqList *L){
32     return L->length;
33 }
34 
35 void DispList(SqList *L){
36     for(int i=0;i<L->length;i++){
37         printf("%d ",L->data[i]);
38     }
39     printf("\n");
40 } 
41 
42 bool GetElem(SqList *L,int i,Elemtype &e){
43     if(i<1||i>L->length)
44         return false;
45     e=L->data[i-1];
46     return true;
47 }
48 
49 
50 int LocateElem(SqList *L,Elemtype e){
51     int i=0;
52     while(i<L->length&&L->data[i]!=e)
53         i++;
54     if(i>=L->length)
55         return 0;
56     else
57         return i+1; 
58     }
59 }
60 
61 bool ListInsert(SqList *&L,int i,Elemtype e){
62     if(i<1||i>L->length)
63         return false;
64         i--;
65         for(int j=L->length;j>i;j--)
66             L->data[j]=L->data[j-1];
67         a[j]=e;
68         L->length++;
69         return true;
70 }
71 
72 
73 bool ListDelete(SqList *&L,int i,Elemtype e){
74     if(i<1||i>L->length)
75         return false;
76     i--;
77     e=L->data[i];
78         for(int j=i;j<L->length-1;j++)
79             L->data[j]=L->data[j+1];
80         L->length--;
81         return true;
82 }
View Code

 

数据结构线性表初接触1:线性表的顺序存储结构下基本运算的实现

标签:spl   temp   ide   malloc   理解   def   false   for   功能   

原文地址:https://www.cnblogs.com/openmymind/p/9370669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!