数据结构:线性结构

1:线性表

线性表基本操作有:

(1)List MakeEmpty():初始化一个新的线性表

(2)ElementType FindKth(List L,int i):根据指定的位序i,返回L中相应元素ai(i是下标)

(3)Position Find(List L,ElementType X):已知X,返回线性表L中与X相同的第一个元素位置;若不存在则返回则返回错误信息;

(4)bool Insert(List L,ElementType X,int i):在L的制定位序i前插入一个元素X;成功则返回true,否则返回false

(5)bool Delete(List L,int i):从L中删除制定序列i;成功返回ture,否则返回false

(6)int Length(List L):返回线性表L的长度

顺序线性表

typedef int position;
typedef struct LNode * PtrToLNode;
struct LNode
{
	ElementType Data[MAXSIZE];
	Position Last;
};
typedef PtrToLNode List;

初始化表

List MakeEmpty(){
	List L;
	L=(List)malloc(sizeof(struct LNode));
	L->Last=-1;
	return L;
}

查找

#define ERROR -1<!-- 将ERROR定义为任意负数 -->
Position Find(List L,ElementType X){
	Position i=0;
	while (i <= L->last && L->Data[i]!=X)
	{
		i++;
	}
	if (i > L->last)
	{
		return ERROR;
	}else
		return i;
}

插入(在L的指定位序i前插入一个新元素x;位序i元素的数组位置下标是i-1)

bool Insert(List L,ElementType X,int i){
	Position j;

	if (L->last == MAXSIZE-1)<!--表空间已满,不能插入-->
	{
		printf("表满");
		return fales;
	}
	if (i<1 || i> L->last+2)<!-- 检查插入位序的合法性,1~n+1.当前n为last+1 -->
	{
		printf("位序不合法");
		return fales;
	}
	for (j=L->last; j>=i-1; j--)
	{
		L->Data[j+1]=L->Data[j]
	}
	L->Data[i-1]=X;
	L->last++;
	retuen true;
}

删除(从L中删除制定位序i的元素,该元素下标为i-1)

bool Delete(List L,int i){
	Position j;
	if (i<1 || L->last+1)
	{
		printf("位序%d不存在元素",i);
		return fales;
	}
	for (j=i; j<=L-last;j++ )
	{
		L->Data[j-1]=L->Data[j];
	}
	L->last--;
	return true;
}

链式线性表

typedef struct LNode * PtrToLNode;
struct LNode
{
	ElementType Data;
	Position Next;
};
typedef PtrToLNode Position;/*这里的位置是结点的地址*/
typedef ptrToLNode List;

求表长

int Length(List L){
	position p;
	int count = 0;/*初始化一个计数器*/
	p = L;/*p指向表的下一个节点*/
	while (p)
	{
		p=p->Next;
		count++;
	}
	return count;
}

查找:

按序号查找

#define ERROR -1<!-- 将ERROR定义为任意负数 -->
ElementType FindKth(List L,int K){
	Position p;
	int count=1;
	p=L;
	while (p&&count<K)
	{
		p=p->Next;
		count++;
	}
	if ((count==K)&&p)
	{
		return p->Data;
	}else
		return ERROR;
}

按值查找

#define ERROR NULL<!-- 将ERROR定义为任意负数 -->
Position Find(List L,ElementType X){
	Position p=L;
	while (p&&p->Data!=X)
	{
		p=p->Next;
	}
	if (p)
	{
		return p;
	}else
		return ERROR;
}

插入

List Insert(List L,ElementType X,int i){
	Position tmp,pre;
	tmp = (Position)malloc(sizeof(struct LNode));/*申请、装填节点*/
	tmp->Data=X;
	if (i==1)
	{
		tmp->Next = L;
		return tmp;
	}else{
		int count = 1;
		pre = L;
		while (pre&&count<i-1)
		{
			pre=pre->Next;
			count++;
		}
		if (pre==NULL||count!=i-1)
		{
			printf("输入参数未知错误");
			free(tmp);
			return ERROT;
		}else{
			tmp->Next=pre->Next;
			pre->Next=tmp;
			return L;
		}
	}
}

带头结点的链式

bool Insert(List l,ElementType X,int i){
	poistion tmp,pre;
	int count=0;
	pre=L;
	while (pre&&count<i-1)
	{
		pre=pre->Next;
		count++;
	}
	if (pre==NULL||count!=i-1)
	{
		printf("输出位置参数错误");
		return ERROR;
	}else{
		tmp=(position)malloc(sizeof(struct LNode));
		tmp->Data=X;
		tmp->Data=pre->Data;
		pre->Next=tmp;
		return true;
	}
}

删除

bool Delete(List L,int i){
	Position pre,tmp;
	int count=0;
	pre=L;
	while (pre&&count!=i-1)
	{
		pre=pre->Next;
		count++;

	}
	if (pre==NULL||count!=i-1||pre->Next=NULL)
	{
		printf("输出位置参数错误");
		return ERROR;
	}else{
		tmp=tmp->Next;
		pre->Next=tmp->Next;
		free(tmp);
		return ture;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_39248122/article/details/80916924