概述
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int status;
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
} SqList;
status InitList(SqList *L);
//构造空线性表
status ListInsert_Sq(SqList *L, int i, ElemType e); //线性表i位置前插入新元素e
status ListDelete_Sq(SqList *L, int i, ElemType *e);//线性表i位置元素删除,用e返回其值
status DestroyList(SqList *L);
//销毁线性表
status ClearList(SqList *L);
//制空线性表
status ListEmpty(SqList L);
//线性表判空,空为true
status ListLength(SqList L);
//线性表长度
status GetElem(SqList L, int i, ElemType *e);
//用e返回线性表i位置的元素值
status LocateElem(SqList L, ElemType e,status *compare(ElemType a,ElemType b));
//返回第一个和e满足compare关系的元素位置
status PriorElem(SqList L, ElemType cur_e, ElemType *pre_e);
//用pre_e返回e 的前驱
status NextElem(SqList L, ElemType cur_e, ElemType *next_e);
//用next_e返回e的后继
status ListTraverse(SqList L,status visit(ElemType a));
//使用visit遍历线性表
status Equal(ElemType a , ElemType b);
status visit(ElemType a);
int main()
{
int i = 0;
ElemType e;
SqList list;
InitList(&list);
printf("After carry out InitList(&list) the length of list: %dn",ListLength(list));
for(i = 1 ; i < 9 ; i++)
ListInsert_Sq(&list , 1 , i);
printf("After carry out nfor(i = 0 ; i < 8 ; i++)ntListInsert(&list , 1 ,i) nthe length of list: %dn",ListLength(list));
if(ListEmpty(list))
printf("At present the list is emptyn");
else printf("At present the list is't emptyn");
GetElem(list,1,&e);
printf("After carry out GetElem(list,1,&e); the value of e is : %dn", e);
printf("The Equal() is used to determine the equality of the functionnAfter carry out LocateElem(list , 3 , Equal)");
printf("The value it returned is %dn",LocateElem(list , 3 , Equal));
printf("After carry out ListTraverse(list , visit)n");
ListTraverse(list , visit);
return 0;
}
status DestroyList(SqList *L)
{
free(L->elem);
L->length=0;
L->listsize=0;
L->elem= NULL;
return OK;
}
status ClearList(SqList *L)
{
L->length=0;
return OK;
}
status ListEmpty(SqList L)
{
if(0 == L.length)
return TRUE;
return FALSE;
}
status ListLength(SqList L)
{
return L.length;
}
status GetElem(SqList L, int i, ElemType *e)
{
if(i < 1 || i > L.length)
return ERROR;
*e = *(L.elem + i -1);
return OK;
}
status LocateElem(SqList L, ElemType e,status *compare(ElemType a,ElemType b))
{
int i=0;
ElemType *p = L.elem;
while(i < L.length && !compare(e, *p++))
i++;
if(i <= L.length)
return i;
return 0;
}
status PriorElem(SqList L, ElemType cur_e, ElemType *pre_e)
{
ElemType *p = L.elem;
int i = 1;
while(1)
if(*++p == cur_e && ++i <= L.length)
{
*pre_e = *--p;
return OK;
}
return ERROR;
}
status NextElem(SqList L, ElemType cur_e, ElemType *next_e)
{
ElemType *p = L.elem;
int i = 1;
while(1)
if(*p++ == cur_e && i++ <= L.length)
{
*next_e = *++p;
return OK;
}
return ERROR;
}
status ListTraverse(SqList L, status visit(ElemType a))
{
int i = 0;
ElemType *p = L.elem;
while(i++ < L.length)
{
visit(*p++);
}
}
status InitList(SqList *L)
{
L->elem = (ElemType *) malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L->elem)
exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
status ListInsert_Sq(SqList *L, int i, ElemType e)
{
if (i < 1 || i > L->length + 1)
return ERROR;
if (L->length >= L->listsize)
{
ElemType *newbase;
newbase = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType)) ;
if
(!newbase)
exit (OVERFLOW);
L->elem = newbase;
L->listsize = L->listsize + LISTINCREMENT;
}
ElemType *q,*p;
q = &(L->elem[i-1]);
for (p = &(L->elem[L->length-1]); p >= q; --p) *(p + 1) = *p;
*q = e;
L->length++;
return OK;
}
status ListDelete_Sq(SqList *L, int i, ElemType *e)
{
ElemType *q,*p;
if (i < 1 || i > L->length)
return ERROR;
p = &L->elem[i-1];
e = *p;
q = L->elem + L->length - 1;
for (++p ; p <= q ; ++p)
*(p - 1) = *p;
--L->length;
return OK;
}
status Equal(ElemType a, ElemType b)
{
if(a == b)
return 1;
return 0;
}
status visit(ElemType a)
{
printf("visit : %d n",a);
}
最后
以上就是悦耳飞机为你收集整理的数据结构-顺序表的全部内容,希望文章能够帮你解决数据结构-顺序表所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复