#include<stdio.h>
#include<malloc.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
#define OK 1
#define NO 0
#define ERROR 0
typedef int Status;
typedef int Boolean;
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList; //create Linear.
Status InitList(SqList *L) //initilize the Linear.
{
L->elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L->elem){
printf("OVERFLOW.");
return ERROR;
}
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
Status LocateElem(SqList *L,ElemType e) //select the elements.
{
ElemType *p;
int i = 1;
p = L->elem;
while(i<=L->length && *(p++)!=e){
i++;
}
if(i<=L->length)
return i;
else
return NO;
}
Status ListInsert(SqList *L,int i,ElemType e) //insert the elements.
{
ElemType *newbase,*q,*p;
int j;
if(i<1||i>L->length+1)
return ERROR;
if(L->length>=L->listsize){
newbase = (ElemType*)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase){
printf("The freedom is full.");
return ERROR;
}
L->elem = newbase;
L->listsize += LISTINCREMENT;
}
for(j=(L->length)-1;j>=i-1;j--){
L->elem[j+1] = L->elem[j];
}
L->elem[i-1] = e;
(L->length)++;
return OK;
}
ElemType ListDelete(SqList *L,int i,ElemType e) //Delete the elmements;
{
ElemType *p;
int j;
if(i<1||i>L->length){
printf("The elements is not exist.");
return ERROR;
}
e = L->elem[i-1];
for(j=i-1;j<=L->length-1;j++){
L->elem[j] = L->elem[j+1];
}
L->length += 1;
return e;
}
int main()
{
SqList L;
InitList(&L);
int i;
ElemType e;
for(i=1;i<=5;i++){
ListInsert(&L,i,i);
}
for(i=1;i<=5;i++){
printf("%d\n",LocateElem(&L,i));
}
ListDelete(&L,2,e);
}
数据结构-线性表
最新推荐文章于 2024-06-07 16:00:07 发布