数据结构—顺序表的基本功能实现

顺序表的基本功能实现

  • 线性表是数据结构的基础结构。数据结构+算法=程序,今天结束了C++的书本自学,扎进数据结构和算法的世界里。
  • 线性表分为顺序存储(顺序表),链式存储(链表),索引存储,散列存储等。
  • 顺序表:用一组地址连续的存储单元依次存储线性表的数据元素。即逻辑关系上相邻的两个元素在物理位置上也相邻,因此可以随机存取表中任一元素,结构简单。但缺点也显然:在作插入或删除操作时,需要移动大量元素。
  • 关于数据结构,最重要的是会画图,明白逻辑结构和物理结构。不急不燥,代码不是一气呵成的,需要长久的练习和耐心。
  • 下面是顺序表的实现代码:
#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

//数据结构线性表之顺序表基本功能的实现 

#define LIST_INIT_SIZE 100  //线性表存储空间的初始分配量 
#define LIST_INC_SIZE 10    //线性表存储空间的分配增量 

//这里线性表中的数据元素,使用的是int整型。也可以传递复杂的结构体元素。 

//线性表表结构 
typedef struct Sqlist{ 
	int* ptr; 		//位序指针 
	int length; 	//线性表的当前长度 
	int listSize;	//线性表的总容量 
}sqlist;

void init_sqlist(sqlist& LA);						//创建初始化线性表 
void setElem(sqlist& LA);    						//线性表元素赋值
void print_sqlist(sqlist& LA);						//打印线性表 
void insert_sqlist(sqlist& LA,int index,int elem);	//插入 
void deleteElem_sqlist(sqlist& LA,int index);		//删除 
void updateElem_sqlist(sqlist& LA,int index);		//更新 
void searchElem_sqlist(sqlist& LA,int index);		//查找 

 
void init_sqlist(sqlist& LA)
{
	LA.ptr=(int*)malloc(sizeof(int)*LIST_INIT_SIZE);
	if(!LA.ptr)  exit(1);
	LA.length=0;
	LA.listSize=LIST_INIT_SIZE;
}

void setElem(sqlist& LA)
{
	int m,n;
	cout<<"请输入要录入的数据个数:(0~100以内)"<<endl;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cout<<"请输入第"<<i+1<<"数据:";
		cin>>m;
		*(LA.ptr+i)=m;
		LA.length++;  //录入数据后元素个数加1 
	}
}
 
void print_sqlist(sqlist& LA) 
{
	for(int i=0;i<LA.length;i++)
	{
		cout<<*(LA.ptr+i)<<'\t';
	}
	cout<<endl;
}
 
void insert_sqlist(sqlist& LA,int index,int elem) 
{
	//我们先作判断index是否合法
	if(index<0||index>LA.length)
	{
		cout<<"非法的index!"<<endl;
		exit(1);
	}
	//插入后判断线性表容量是否溢出
	if(LA.length>=LIST_INIT_SIZE)
	{
		cout<<"线性表溢出需要扩展容量!"<<endl;
		int* newBase=(int*)realloc(LA.ptr,(LA.listSize+LIST_INC_SIZE)*sizeof(int));
		if(!newBase)  exit(1);
		LA.ptr=newBase;
		LA.listSize+=LIST_INC_SIZE;
	} 
	//若是在表头插入 
	if(index==0)  
	{
		for(int i=0;i<LA.length;i++) 
		{
			//从最后一个数开始,前一个数赋值给后一个数 
			*(LA.ptr+LA.length-i)=*(LA.ptr+LA.length-i-1);
		}
		*(LA.ptr)=elem;
		LA.length++;
	}
	//若是在表尾插入 
	else if(index==LA.length)
	{
		for(int i=index;i<LA.length;i++) 
		{
			*(LA.ptr+LA.length)=elem;
		}
		*(LA.ptr+LA.length)=elem;
		LA.length++;
	}
	//若在中间任一位置插入 
	else
	{
		for(int i=index,j=0;i<LA.length;i++,j++)
		{
			//这里需要一个j变量辅助 
			*(LA.ptr+LA.length-j)=*(LA.ptr+LA.length-1-j);
		}
		*(LA.ptr+index)=elem;
		LA.length++;
	} 
}
 
void deleteElem_sqlist(sqlist& LA,int index)
{
	//我们先作判断index是否合法
	if(index<0||index>LA.length)
	{
		cout<<"非法的index!"<<endl;
		exit(1);
	} 
	//若是删除表头的数据元素 
	if(index==0)  
	{
		for(int i=index;i<LA.length;i++) 
		{
			//从第二个数开始,后一个数赋给前一个数 
			*(LA.ptr+i)=*(LA.ptr+i+1);
		}
		LA.length--;
	}
	//若是删除表尾的数据元素 
	else if(index==LA.length)
	{
		LA.length--;
	}
	//若是删除中间任一位置的数据元素 
	else
	{
		for(int i=index,j=0;i<LA.length;i++,j++)
		{
			//这里需要一个j变量辅助 
			*(LA.ptr+index+j)=*(LA.ptr+index+j+1);
		}
		LA.length--;
	} 
}
 
void searchElem_sqlist(sqlist& LA,int index)
{
	if(index<0||index>LA.length-1)
	{
		cout<<"非法的index!"<<endl;
		exit(1);
	}
	cout<<"下标"<<index<<"的元素值为:"<<*(LA.ptr+index)<<endl;
} 

void updateElem_sqlist(sqlist& LA,int index)
{
	int upNum;
	searchElem_sqlist(LA,index);
	cout<<"请输入你要更新的值:";
	cin>>upNum;
	*(LA.ptr+index)=upNum;
}

int main()
{
	int index,elem;
	sqlist LA;        //定义线性表LA 
	init_sqlist(LA);  //初始化线性表 
	setElem(LA);      //线性表赋值 
	print_sqlist(LA); //打印线性表  
	for(;;)
	{
		cout<<"请输入插入的下标和元素值:"; 
		cin>>index>>elem;
		insert_sqlist(LA,index,elem);   
		print_sqlist(LA);
		
		cout<<"请输入删除元素的下标:"; 
		cin>>index;
		deleteElem_sqlist(LA,index);  
		print_sqlist(LA);
		
		cout<<"请输入要更新的元素下标:";
		cin>>index;
		updateElem_sqlist(LA,index);
		print_sqlist(LA);
		
		cout<<"请输入要查找的元素下标:";
		cin>>index;
		searchElem_sqlist(LA,index);
		print_sqlist(LA);
	}
	return 0;
}

/

发布了77 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42932834/article/details/90947489