关闭

顺序表的实现

474人阅读 评论(0) 收藏 举报
分类:

 

复制代码

sqlist.h
 1 #ifndef SQ_LIST_H
 2 #define SQ_LIST_H
 3 
 4 #define OK 1
 5 #define ERROR 0
 6 #define TRUE 1
 7 #define FLASE 0
 8 
 9 typedef void TSqList;
10 typedef int Status;
11 
12 typedef int ElemType;
13 typedef struct sq_list
14 {
15     int length;
16     int MaxSize;
17     ElemType * data;
18 }SqList;
19 
20 TSqList * CreatSqList(int maxsize);
21 
22 Status SqListEmpty( TSqList *L);
23 
24 void ClearSqList(TSqList * L);
25 
26 void DeleteSqList(TSqList * L);
27 
28 ElemType GetElem(TSqList * L, int i);
29 
30 void  LocateElem(TSqList *L,ElemType e);
31 
32 ElemType SqListInsert(TSqList * L, int i, ElemType e);
33 
34 ElemType SqListDelete(TSqList * L, int i);
35 
36 int SqListLength(TSqList *L);
37 
38 void ForEachSqList(TSqList *L);
39 
40 void SortSqList(TSqList *L);
41 
42 
43 #endif
复制代码

sqlist.c

复制代码
  1 /******************************************
  2  *作者:王老二
  3  *程序名称:sqlist.c
  4  *功能描述:顺序存储的线性表
  5  *日期:2013.12.02
  6  ******************************************/
  7 
  8 #include "sqlist.h"
  9 #include <stdio.h>
 10 #include <stdlib.h>
 11 
 12 /*创建线性表*/
 13 TSqList * CreatSqList(int maxsize)
 14 {
 15     SqList * L = NULL ;
 16 
 17     L = (SqList *)malloc(sizeof(SqList ) + sizeof(ElemType) * maxsize );
 18 
 19     if(NULL != L)
 20     {
 21         L->MaxSize = maxsize;
 22         L->length = 0;
 23         L->data = (ElemType *)(L + 1);
 24     }
 25 
 26     return L;
 27 }
 28 
 29 /*判断线性表是否为空*/
 30 Status SqListEmpty( TSqList *L)
 31 {
 32     SqList * Lp = (SqList *)L;
 33     if(0 == SqListLength(Lp))
 34         return TRUE ;
 35     else
 36         return FLASE;
 37 }
 38 
 39 void  ClearSqList(TSqList * L)
 40 {
 41     SqList * Lp = (SqList *)L;
 42     if(NULL != Lp)
 43         Lp->length = 0;
 44 }
 45 
 46 void  DeleteSqList(TSqList * L)
 47 {
 48     SqList * Lp = (SqList *)L;
 49     if( NULL != Lp)
 50         free(Lp);
 51 }
 52 
 53 ElemType  GetElem(TSqList * L, int i)
 54 {
 55     SqList * Lp = (SqList * )L;
 56     ElemType e;
 57 
 58     if(NULL != Lp)
 59         e = Lp->data[i-1];
 60     return e;
 61 }
 62 
 63 void LocateElem(TSqList *L,ElemType e)
 64 {
 65     int i = 0;
 66     SqList * Lp = (SqList *)L;
 67 
 68     for(i = 0; i < Lp->length ; i++)
 69     {
 70         if(Lp->data[i] == e)
 71         printf("the location of the common elem is %d\n",i+1);//是用break还是return 如果有两个相同的数字时会先什么情况。
 72     }
 73 }
 74 
 75 ElemType SqListInsert(TSqList * L, int i, ElemType e)
 76 {
 77     int k;
 78     SqList * Lp = (SqList * )L;
 79 
 80     if(NULL == Lp|| i < 1 || i > Lp->length+1)
 81         return ERROR;
 82 
 83     for(k = Lp->length-1 ; k >= i-1 ; k-- )
 84     {
 85         Lp->data[k+1] = Lp->data[k];
 86     }
 87     Lp->length++;
 88 
 89     return Lp->data[i-1] = e;
 90 }
 91 
 92 ElemType SqListDelete(TSqList * L, int i )
 93 {
 94     int k;
 95     ElemType e;
 96     SqList * Lp = (SqList * )L;
 97 
 98     if(Lp==NULL || i < 1 || i > Lp->length)
 99     return ERROR;
100 
101     e = Lp->data[i-1];
102 
103     for(k = i ; k < Lp->length ; k++)
104     {
105         Lp->data[k-1] = Lp->data[k];
106     }
107 
108     Lp->length--;
109     return e;
110 }
111 
112 int SqListLength(TSqList *L)
113 {
114     SqList *Lp = (SqList *)L;
115     return Lp->length;
116 }
117 
118 void ForEachSqList(TSqList *L)
119 {
120     int i;
121     SqList *Lp = (SqList *)L;
122 
123     if(Lp == NULL||Lp->length == 0)
124     {
125         printf("the SqList Lenght is 0,cannot Foreach\n");
126         return ;
127     }
128 
129     for(i = 0 ;i< Lp->length ;i++)
130     {
131         printf("Lp->data[%d] = %d\n",i,Lp->data[i]);
132     }
133 }
134 
135 void SortSqList(TSqList *L)
136 {
137     int i, j, tmp ;
138     SqList * Lp = (SqList *)L;
139 
140     if(NULL == Lp)
141         return ;
142     for(i = 0 ; i < Lp->length ; i++)
143     {
144         for(j = Lp->length - 1; j > i; j--)
145         {
146             if(Lp->data[j] < Lp->data[j-1])
147             {
148                 tmp = Lp->data[j-1];
149                 Lp->data[j-1] = Lp->data[j];
150                 Lp->data[j] = tmp;
151             }
152         }
153     }
154 
155 }
复制代码

 

main.c

复制代码
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "sqlist.h"
 4 
 5 int main()
 6 {
 7     TSqList * pList = CreatSqList(10);
 8 
 9     SqListInsert(pList, 1 ,1);
10     SqListInsert(pList, 1 ,1);
11     SqListInsert(pList, 1 ,3);
12     SqListInsert(pList, 1 ,4);
13     SqListInsert(pList, 1 ,6);
14     SqListInsert(pList, 1 ,5);
15 
16     LocateElem(pList, 1);
17 
18     ForEachSqList(pList);
19 
20     SortSqList(pList);
21 
22     printf("\n\n");
23 
24     ForEachSqList(pList);
25 
26     SqListDelete(pList , 1);
27 
28     printf("\n");
29 
30     ForEachSqList(pList);
31 
32     printf("\n%d\n",GetElem(pList , 3));
33 
34     ClearSqList(pList);
35 
36     ForEachSqList(pList);
37 
38 
39     return 0;
40 }
复制代码

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19319次
    • 积分:719
    • 等级:
    • 排名:千里之外
    • 原创:53篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条