数据结构线性表---顺序表

 
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int ElemType;
const int SIZE =20;           /*线性表的起始长度*/
const int Add_Capacity = 10;  /*线性表的每次扩容大小*/

struct SqList {
    int length;              /*线性表当前长度*/
	ElemType *data;          /*线性表长度为SIZE*/
	int capacity;
}sql;

					/*
					初始化线性表表
					格式化线性表
					线性表的长度
					线性表的容量
					线性表的首元素
					线性表的尾元素
					根据索引寻找元素
					根据元素寻找索引
					插入元素
					根据索引删除元素
					根据元素删除
					*/
void InitList(SqList &L);
void clear(SqList &L);
int GetLength(SqList &L);
int GetCapacity(SqList &L);
ElemType GetFirstElem(SqList &L);
ElemType GetLastElem(SqList &L);
ElemType GetElem(SqList &L, int i);
int GetIndex(SqList &L, ElemType e);
bool ListInsert(SqList &L, int i, ElemType e);
bool ListDelectIndex(SqList &L, int i);
bool ListDelectElem(SqList &L, ElemType e);
void InitList(SqList &L)
{
     L.length = 0;
	 L.data =(ElemType *)malloc(sizeof(ElemType)*SIZE);
	 L.capacity = SIZE;
}
void clear(SqList &L)
{
	L.length = 0;
}
int GetLength(SqList &L)
{
	printf("%d\n", L.length);
	return  L.length;
}
int GetCapacity(SqList &L)
{
	printf("%d\n", L.capacity);
	return  L.capacity;
}
ElemType GetFirstElem(SqList &L)
{
	if (L.length==0)
	{
		printf("no exsit\n");
		return NULL;
	}
	else
	{
		printf("%d\n", L.data[0]);
		return L.data[0];
	}
}
ElemType GetLastElem(SqList &L)
{
	if (L.length == 0)
	{
		printf("no exsit\n");
		return NULL;
	}
	else
	{
		printf("%d\n", L.data[L.length - 1]);
		return L.data[L.length-1];
	}
}
ElemType GetElem(SqList &L, int i)
{
	if (L.length==0||i<1||i>L.length)
	{
		printf("no exsit\n");
		return NULL ;
	}
	else
	{
		ElemType e = L.data[i-1];
		printf("%d\n",e);
		return e;
	}
}
int GetIndex(SqList &L, ElemType e)
{
	int pos = -1;
	if (L.length==0)
	{
		return pos;
	}

	for (int i=0;i<L.length;++i)
	{
		if (L.data[i]==e)
		{
			pos = i + 1;
		}
	}
	return pos;
}
bool ListInsert(SqList &L, int i, ElemType e)
{
	
	if ( i<1 || i>L.length+1 ) /*顺序线性表已经满*//*当i不在范圈内*/
	{
		return false;
	}
	
	if (i <L.length)        /*若插入数据位置不在表尾*/
	{
		for (int k = L.length; k > i - 1; --k)
		{
			L.data[k + 1] = L.data[k];
		}
	}
		L.data[i-1] = e;
		++L.length;
		if (L.length == L.capacity)
		{
			L.data = (ElemType *)realloc(L.data, sizeof(ElemType)*L.capacity + sizeof(ElemType)*Add_Capacity);

			L.capacity += Add_Capacity;
		}
	
	return true;
}
bool ListDelectIndex(SqList &L, int i)
{
	if (L.length == 0 || i<1 || i>L.length)
	{
		return false;
	}
	
	if (i < L.length)       
	{
		for (int k = i; k <L.length; ++k)
		{
			L.data[k-1] = L.data[k];
		}
	}
	L.length--;
	return true;
}
bool ListDelectElem(SqList &L, ElemType e)
{
	if (L.length == 0)
	{
		return false;
	}
	
	for (int i = 1; i<=L.length; ++i)
	{
		if (L.data[i] == e)
		{
			if (i < L.length)
			{
				for (int k = i; k <L.length; ++k)
				{
					L.data[k - 1] = L.data[k];
				}
			}
			L.length--;
			return true;
		}
	}
	return false;
}
int main()
{ 
	InitList(sql);
	for (int i=1;i<=158;++i) 
	{
		ListInsert(sql, i, i);
	}
	GetElem(sql,1);
	GetElem(sql, sql.length);
	GetFirstElem(sql);
	GetLastElem(sql);
	GetLength(sql);
	GetCapacity(sql);
	ListDelectElem(sql, 110);
	GetFirstElem(sql);
	GetLastElem(sql);
	GetLength(sql);
	GetCapacity(sql);
	free(sql.data);
	return 0;

}

猜你喜欢

转载自blog.csdn.net/ox0080/article/details/80291746