一.顺序存储结构线性表基本操作实现算法

来源:互联网 发布:ipcam监控软件 编辑:程序博客网 时间:2024/04/27 22:00

一.顺序存储结构线性表基本操作实现算法

C语言: 顺序存储结构线性表基本操作 C语言实现
001 ///////////////////////////////////////////////////////////

002 //---------------------------------------------------------
003 //            
顺序存储结构线性表基本操作 C语言实现
004 //
005 //            a simple example of Sq_List by C language
006 //
007 //                    by wangweinoo1[PG]
008 //---------------------------------------------------------
009 ///////////////////////////////////////////////////////////
010
011
012 #include <stdio.h>
013 #include <stdlib.h>
014
015 //
以下为函数运行结果状态代码
016
017 #define TRUE 1
018 #define FALSE 0
019 #define OK 1
020 #define ERROR 0
021 #define iNFEASiBLE -1
022 #define OVERFLOW -2
023
024 #define LiST_iNiT_SiZE 5  //
线性表存储空间的初始分配量
025 #define LiSTiNCREMENT 1  //
线性表存储空间分配增量
026
027 typedef int Status; //
函数类型,其值为为函数结果状态代码
028
029 typedef int ElemType; //
假设数据元素为整型
030
031 typedef struct
032 {
033     ElemType *elem; //
存储空间基址
034     int length; //
当前长度
035     int listsize; //
当前分配的存储容量
036 }Sqlist;
037 //
实现线性表的顺序存储结构的类型定义
038
039 Sqlist L;//
为了引用方便,定义为全局变量
040
041 ///////////////////////////////////////
042 //
函数名:initList()
043 //
参数SqList L
044 //
初始条件:无
045 //
功能:构造一个空线性表
046 //
返回值:存储分配失败:OVERFLOW
047 //        
存储分配成功:OK
048 ///////////////////////////////////////
049 Status initList(Sqlist L)
050 {
051     L.elem=(ElemType*)malloc(LiST_iNiT_SiZE*sizeof(ElemType));
052     if(!L.elem)
053         exit(OVERFLOW);
054     else
055         return OK;
056 }
057
058 ///////////////////////////////////////
059 //
函数名:DestroyList()
060 //
参数:SqList L
061 //
初始条件:线性表L已存在
062 //
功能:销毁线性表
063 //
返回值:L.elem==NULL:ERROR
064 //        L.elem!=NULL:OK
065 ///////////////////////////////////////
066 Status DestroyList(Sqlist L)
067 {
068     if(!L.elem)
069         return ERROR;
070     else
071         free(L.elem);
072     return OK;
073 }
074
075 ///////////////////////////////////////
076 //
函数名:ClearList()
077 //
参数:SqList L
078 //
初始条件:线性表L已存在
079 //
功能:清空线性表
080 //
返回值:L.elem==NULL:ERROR
081 //        L.elem!=NULL:OK
082 ///////////////////////////////////////
083 Status ClearList(Sqlist L)
084 {
085     if(!L.elem)
086         exit(ERROR);
087     int i;
088     ElemType *p_elem=L.elem;
089     for(i=0;i<L.length;i++)
090     {
091         *L.elem=NULL;
092         L.elem++;
093     }
094     L.elem=p_elem;
095     return OK;
096 }
097
098 ///////////////////////////////////////
099 //
函数名:ListEmpty()
100 //
参数:SqList L
101 //
初始条件:线性表L已存在
102 //
功能:判断线性表是否为空
103 //
返回值:空:TRUE
104 //        
非空:FALSE
105 ///////////////////////////////////////
106 Status ListEmpty(Sqlist L)
107 {
108     int i;
109     ElemType *p_elem=L.elem;
110     for(i=0;i<L.length;i++)
111     {
112         if(*L.elem!=0)
113         {
114             L.elem=p_elem;
115             return FALSE;
116         }
117         L.elem++;
118     }
119     return TRUE;
120 }
121 ///////////////////////////////////////
122 //
函数名:ListLength()
123 //
参数:SqList L
124 //
初始条件:线性表L已存在
125 //
功能:返回线性表长度
126 //
返回值:线性表长度(L.length)
127 ///////////////////////////////////////
128 int ListLength(Sqlist L)
129 {
130     return L.length;
131 }
132
133 ///////////////////////////////////////
134 //
函数名:GetElem()
135 //
参数:SqList L,int i,ElemType *element
136 //
初始条件:线性表L已存在,1<=i<=ListLength(L)
137 //
功能:用e返回线性表中第i个元素的值
138 //
返回值:(i<1)||(i>ListLength(L))OVERFLOW
139 //        1<=i<=ListLength(L)
OK
140 ///////////////////////////////////////
141 Status GetElem(Sqlist L,int i,ElemType *element)
142 {
143     int j;
144     ElemType *p_elem=L.elem;
145     if(i<1||i>L.length)
146         return OVERFLOW;
147     for(j=1;j<=i;j++)
148         L.elem++;
149     element=L.elem;
150     L.elem=p_elem;
151     return OK;
152 }


153
154 ///////////////////////////////////////
155 //
函数名:LocateElem()
156 //
参数:Sqlist L,ElemType element
157 //
初始条件:线性表L已存在
158 //
功能:返回顺序表L中第1个与element相等的元素
159 //
返回值:若在L中存在于element相等的元素:其位序
160 //        
若在L中不存在与element相等的元素:0
161 ///////////////////////////////////////
162 int LocationElem(Sqlist L,ElemType element)
163 {
164     int i;
165     ElemType *p_elem=L.elem;
166     for(i=1;i<L.length;i++)
167     {
168         if(*L.elem==element)
169         {
170             L.elem=p_elem;
171             return i;
172         }
173         else
174             L.elem++;
175     }
176     return 0;
177 }
178
179 ///////////////////////////////////////
180 //
函数名:PriorElem()
181 //
参数:Sqlist L,ElemType cur_e,ElemType *pre_e
182 //
初始条件:线性表L已存在,i>1&&i<=L.lengthLocationElem()存在
183 //
功能:用pre_e返回线性表中cur_e的前驱
184 //
返回值:i<=1||i>L.lengthOVERFLOW
185 //        i>1&&i<=L.length
OK
186 ///////////////////////////////////////
187 Status PriorElem(Sqlist L,ElemType cur_e,ElemType *pre_e)
188 {
189     ElemType *p_elem=L.elem;
190     int i,j;
191     i=LocationElem(L,cur_e);
192     if(i<=1||i>L.length)
193         exit(OVERFLOW);
194     for(j=1;j<i;j++)
195     {
196         if(j==(i-1))
197         {
198             pre_e=L.elem;
199             L.elem=p_elem;
200             return OK;
201         }
202         else
203             L.elem++;
204     }
205 }
206
207 ///////////////////////////////////////
208 //
函数名:NextElem()
209 //
参数:Sqlist L,ElemType cur_e,ElemType *next_e
210 //
初始条件:线性表L已存在,i>=1&&i<L.lengthLocationElem()存在
211 //
功能:用next_e返回线性表中cur_e的后继
212 //
返回值:i<1||i>=L.lengthOVERFLOW
213 //        i>=1&&i<L.length
OK
214 ///////////////////////////////////////
215 Status NextElem(Sqlist L,ElemType cur_e,ElemType *next_e)
216 {
217     ElemType *p_elem=L.elem;
218     int i,j;
219     i=LocationElem(L,cur_e);
220     if(i<1||i>=L.length)
221         exit(OVERFLOW);
222     for(j=1;j<=i+1;j++)
223     {
224         if(j==(i+1))
225         {
226             next_e=L.elem;
227             L.elem=p_elem;
228             return OK;
229         }
230         else
231             L.elem++;
232     }
233 }
234
235 ///////////////////////////////////////
236 //
函数名:Listinsert()
237 //
参数:SqList L,int i,ElemType e
238 //
初始条件:线性表L已存在,1<=i<=ListLength(L)+1
239 //
功能:在线性表中第i个数据元素之前插入数据元素e
240 //
返回值:失败:ERROR
241 //        
成功:OK
242 ///////////////////////////////////////
243 Status Listinsert(Sqlist L,int i,ElemType e)
244 {
245     int *q=&(L.elem[i-1]);
246     ElemType *newbase,*p;
247     if(i<1||i>(L.length+1))
248         return ERROR;
249     if(L.length>=L.listsize)
250     {
251         newbase=(ElemType*)realloc(L.elem,L.listsize+LiSTiNCREMENT*sizeof(ElemType));
252         if(newbase==NULL)
253             exit(OVERFLOW);
254         L.elem=newbase;
255         L.listsize+=LiSTiNCREMENT;
256     }
257     for(p=&(L.elem[L.length-1]);p>=q;--p)
258         *(p+1)=*p;
259     *q=e;
260     ++L.length;
261     return OK;
262 }
263
264 ///////////////////////////////////////
265 //
函数名:ListDelete()
266 //
参数:SqList L,int i,Elemtype e
267 //
初始条件:线性表L已存在,1<=i<=ListLength(L)
268 //
功能:将线性表L中第i个数据元素删除
269 //
返回值:失败:ERROR
270 //        
成功:OK
271 ///////////////////////////////////////
272 Status ListDelet(Sqlist L,int i,ElemType e)
273 {
274     if(i<1||(i>L.length))
275         return ERROR;
276     ElemType *p,*q;
277     p=&(L.elem[i-1]);
278     e=*p;
279     q=L.elem+L.length-1;
280     for(p=p+1;p<=q;++p)
281         *(p-1)=*p;
282     --L.length;
283     return OK;
284 }

 

原创粉丝点击