2021-10-12 数据结构 线性表语言版 186行

#include<stdio.h>
#include<stdlib.h>


#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define ERROR -1
#define OVERFLOW -1
#define ENDFLAG 0
typedef int Status;
typedef int ElemType;  //将int重新命名为ElemType


typedef struct
{
	ElemType *elem;
	int length;
	int listsize;
}SqList;

Status InitList(SqList *L)   //构造一个空的线性表L
{
	printf("Begin InitList\t");
	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 DestroyList(SqList *L)  //销毁线性表L
{
    printf("Begin DestroyList\t");
	if(L->elem)
	{
		free(L->elem);
		(L->elem)=NULL;
	}
	return OK;
}

Status ClearList(SqList *L)    //置空线性表L
{
	if(!L->elem)
	{
		return ERROR;
	}
	else
	{
		L->length=0;
		return OK;
	}	
}

Status ListEmpty(SqList *L)    //判断L是否为空表
{
	printf("Begin ListEmpty\t");
	if(!L) return ERROR;
	return (L->elem!=0);
}

Status ListLength(SqList L)        //返回L表中元素的个数
{
	if(!L.elem) return ERROR;
	return L.length;
}

Status GetElem(SqList L,int i,int &e)
{
	if(!L.elem) return ERROR;
	if(i>L.length) return ERROR;
	return e=*L.elem+i-1;
}

Status CreatList(SqList *L,ElemType a[],int n)   //将线性表赋值
{
	
	if(!L->elem) return ERROR;
	for(int i=0;i<n;i++)
	{
		L->elem[i]=a[i];
		
	}
	L->length=n;
	return OK;
}

Status LocateElem(SqList L,ElemType e,ElemType (*compare)(int,int))  
{                          //返回L中第一个与e满足关系(*compare)(int,int)的数据元素的位序,若这样的元素不存在则返回0
	int z=0;
	int i=0;
	if(!L.elem) return ERROR;
	while(*L.elem+i)
	{
	    z=compare(L.elem[i],e);
		++i;
		if(z==1) break;
		if(i+1==L.length) return 0;
	}

	return i;
}

Status equal(int x,int y)                    //判断两个元素是否相等
{
	if(x==y) return 1;
    return 0;
}

Status ListInsert(SqList *L,int i,ElemType &e) //在L中的第i个位置插入e元素,L的长度+1
{
    ElemType *p,*q;
    ElemType *newbase;
	if(!L->elem) return ERROR; 
	if(i<1||i>L->length+1) return ERROR; //i值不合法
	if(L->length>=L->listsize)
	{
		newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
	    if(!newbase)exit(OVERFLOW);
		L->elem=newbase;                 //新基址
	    L->listsize+=LISTINCREMENT;     //增加内存容量
	}
    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 prt(SqList L)          //将线性表打印
{
    if(!L.elem) return ERROR;	
	for(int i=0;i<L.length;i++)
	{
		printf("%5d",L.elem[i]);
	}
	return OK;
}

Status ListDelete(SqList &L,int i,ElemType &e)    //将第i个元素删除,并用e值返回,L的长度-1
{
	if(i<1||i>L.length+1) return ERROR;
	ElemType *p,*q;
	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 ListTraverse(SqList &L,ElemType (*visit)(ElemType &x))
{
	if(!L.elem) return ERROR;
	int z;
	for(int i=0;i<L.length;++i)
	{
		z=visit(L.elem[i]);
	}
	return z;
}

Status zero(ElemType &x)
{
	x=0;
	return OK;
}
	
void main()
{
	Status InitList(SqList *L);
	Status DestroyList(SqList *L);
	Status ClearList(SqList *L);
	Status ListEmpty(SqList *L);
	Status ListLength(SqList L);
	Status GetElem(SqList L,ElemType i,ElemType &e);
	Status CreatList(SqList *L,ElemType a[],int n);
	Status LocateElem(SqList L,ElemType e,ElemType (*compare)(int,int));
	Status equal(int x,int y);
	Status ListInsert(SqList *L,int i,ElemType &e);
	Status prt(SqList L);
	Status ListDelete(SqList &L,int i,ElemType &e);
	Status ListTraverse(SqList &L,ElemType (*visit)(ElemType &x));
	Status zero(ElemType &x);

	
    int e;
	
    int n=4;

	SqList a;
	SqList *p;
	p=&a;

	ElemType b[5]={1,2,3,4,5};

	if(InitList(p)) printf("OK\n");
	CreatList(p,b,5);

	//if(DestroyList(p)) printf("OK\n");
	if(ListEmpty(p)) printf("OK\n");
	//printf("%d",ListLength(a));
	GetElem(a,n,e);
	printf("%d\n",e);

	//ListInsert(p,2,100);
	//ListDelete(a,2,e);
	//printf("%d\n",e);
	ListTraverse(a,zero);

	prt(a);

	//printf("%d\n",LocateElem(a,6,equal));
}

数据结构
线性表基础内容,以后会多接触这种类型的代码,加深对引用和指针的运用。还有指针函数的运用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值