數據結構與算法(1)--順序存儲結構

順序存儲結構

#include<cstdio>
#define Elemtype int
#define MaxSize 50

typedef struct {
	Elemtype data[MaxSize];
	int length;
}SqList;

//插入操作
bool Insert(SqList &L,int i,Elemtype e)
{
	if(i<1 || i>L.length+1)
	{
		return false;
	}
	if(L.length >= MaxSize)
	{
		return false;
	}
	for(int j=L.length;j>=i;j--)
	{
		L.data[j] = L.data[j-1];
	}
	L.data[i] = e;
	L.length++;
	return true; 
}

//刪除操作 
bool Delete(SqList &L,int i,Elemtype e)
{
	if(i<1||i>L.length)
	{
		return false;
	}
	e = L.data[i-1];
	for(int j=i;j<L.length;j++)
	{
		L.data[j-1] = L.data[j];
	}
	L.length--;
	return true;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章