小言_互联网的博客

数据结构——c++顺序表

355人阅读  评论(0)

数据结构——c++顺序表

顺序表菜单

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINTCREMENT 10
using namespace std;

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

int success=0;

void CreatArray(SqList *L);//初始化 
void DestroyArray(SqList *L);//销毁 
void ClearArray(SqList &L);//清空 
int ArrayEmpty(SqList *L);//判断是否为空 
int ArrayLength(SqList *L);//长度 
int GetElem(SqList *L,int i,int &n);//指定位置元素 
int LocateElem(SqList *L,int e);//元素位置 
int FormerElem(SqList *L,int e,int &f);//前驱 
int LaterElem(SqList *L,int e,int &l);//后继 
void ArrayInsert(SqList *L,int i,int e);//插入元素 
void ArrayDelete(SqList *L,int i);//删除元素 
void ArrayCout(SqList *L);// 输出 
int IfEmptyList(SqList *L);//是否空表 
void CreatArray1(SqList *L);
void MergeArray(SqList *L1,SqList *L2,SqList*L3); //合并 

int main()
{
int choose;
SqList L;
SqList L1,L2,L3;
CreatArray1(&L1);
ArrayInsert(&L1,1,2);ArrayInsert(&L1,2,3);ArrayInsert(&L1,3,4);ArrayInsert(&L1,4,5);
CreatArray1(&L2);
ArrayInsert(&L2,1,1);ArrayInsert(&L2,2,4);ArrayInsert(&L2,3,6);ArrayInsert(&L2,4,7);ArrayInsert(&L2,5,8);ArrayInsert(&L2,6,9);
CreatArray1(&L3);
while(1)
{
	cout<<endl;
	cout<<"1----初始化一个线性表"<<endl;
	cout<<"2----销毁线性表"<<endl;
	cout<<"3----清空线性表"<<endl;
	cout<<"4----判断线性是否为空"<<endl;
	cout<<"5----求线性表长度"<<endl;
	cout<<"6----获取线性表中指定位置的元素"<<endl;
	cout<<"7----获取线性表元素的位置"<<endl;
	cout<<"8----求前驱"<<endl;
	cout<<"9----求后继"<<endl;
	cout<<"10----在线性表指定位置插入元素"<<endl;
	cout<<"11----删除线性表指定位置的元素"<<endl;
	cout<<"12----显示线性表"<<endl;
	cout<<"13----合并两个非递减有序的线性表"<<endl;
	cout<<"0----退出"<<endl;
	cout<<"请输入操作代码:";
	cin>>choose; 
	
switch(choose)
{
case 0:
	{
		return 0;
	}
case 1:
{
	CreatArray(&L); 
	break;
}
case 2:
{
	if(success==0)
	{
		cout<<"请先进行初始化!!!"<<endl;
		break;
	}
	DestroyArray(&L);
	
	break;
}
case 3:
{
	if(success==0)
	{
		cout<<"请先进行初始化!!!"<<endl;
		break;
	}
	ClearArray(L);
	break;
}
case 4:
{
	if(success==0)
	{
	cout<<"请先进行初始化!!!"<<endl;
	break;
	}
	int b;
	b=IfEmptyList(&L);
	if(b==0)
	{
		cout<<"此表为空表!"<<endl;
	}
	else
	{
		cout<<"此表为非空表!"<<endl;
	}
		break;
	}
	case 5:
	{
		if(success==0)
		{
			cout<<"请先进行初始化!!!"<<endl;
			break;
		}
			int length;
			length= ArrayLength(&L);
			cout<<"线性表的长度为:"<<length<<endl;
			break;
	}
	case 6:
	{
		if(success==0){
			cout<<"请先进行初始化!!!"<<endl;
			break;
		}
			cout<<"请输入指定的位置:";
			int c;
			cin>>c; 
			cout<<"此位置的数为:"<<L.elem[c-1]<<endl;
			break;
	}
	case 7:
	{
		if(success==0)
		{
			cout<<"请先进行初始化!!!"<<endl;
			break;
		}
		int e;
		cout<<"请输入一个表中元素:";
		cin>>e; 
		int d;
		d= LocateElem(&L,e); 
		if(d==0)
		{
			break;
		} 
		cout<<"此元素的位置为:"<<d<<endl;
		break;
	}
	case 8:
	{
		if(success==0)
		{
			cout<<"请先进行初始化!!!"<<endl;
			break;
		}
			cout<<"请输入要查找前驱的元素:";
			int d1;
			cin>>d1; 
			int f;
			FormerElem(&L,d1,f);
			cout<<"此元素的前驱为:"<<f<<endl;
			break;
	}
	case 9:
	{
		if(success==0)
		{
			cout<<"请先进行初始化!!!"<<endl;
			break;
		}
		cout<<"请输入要查找后继的元素:";
		int d3;
		cin>>d3; 
		int l;
		LaterElem(&L,d3,l);
		cout<<"此元素的后继为:"<<l<<endl;
		break;
	}
	case 10:
	{
		if(success==0)
		{
			cout<<"请先进行初始化!!!"<<endl;
			break;
		}
		int d5,d6;
		cout<<"请输入要插入的位置:";
		cin>>d5;
		cout<<"请输入要插入的数:";
		cin>>d6; 
		ArrayInsert(&L,d5,d6);
		break;
	}
	case 11:
	{
		if(success==0)
		{
			cout<<"请先进行初始化!!!"<<endl;
			break;
		}
			int d7;
			cout<<"请输入要删除的位置:";
			cin>>d7;
			ArrayDelete(&L,d7);
			break;
	}
	case 12:
	{
		if(success==0)
		{
			cout<<"请先进行初始化!!!"<<endl;
			break;
		}
		ArrayCout(&L); 
		break;
	}
	case 13:
	{
		MergeArray(&L1,&L2,&L3);
		ArrayCout(&L3);			
		break;
	}
}
}
}

void CreatArray(SqList *L)//申请100个单元,length=0 
{
L->elem =(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L->elem)
{
	cout<<"Failed to initialize!"<<endl;
}
L->length=0;
L->listsize=LIST_INIT_SIZE;
cout<<"Succeed in initializing!"<<endl;
success=1; 

}

void DestroyArray(SqList *L){

	free(L->elem);//释放指向空间 
	L->elem=NULL;//使指针指向为0 
	L->length=0;
	L->listsize=0;
if(!L->elem)
{
	cout<<"Destroyed successfully!"<<endl;
	success=0; 
}
}

void ClearArray(SqList &L)//清空线性表 
{

L.length=0;
cout<<"Cleared succeessfully!"<<endl;
success=0;
}

int ArrayEmpty(SqList *L)//判断线性表是否为空 
{
if(!L->elem)
{
	return 0;
}
else
{
	return 1;
}
}

int IfEmptyList(SqList *L)
{

if(L->length>0)
{
	return 1;
}
else
{
	return 0;
}
}

int ArrayLength(SqList *L)
{
	return L->length;	
}

int GetElem(SqList *L,int i,int &n)
{
if(i<1||i>L->length)
{
	cout<<"Position is incorrect!"<<endl;
	return 0;
}
else
{
	n=L->elem[i-1];
	return 1;
}
}

int LocateElem(SqList *L,int e)
{
for(int i=0;i<L->length;i++)
{
	if(e==L->elem[i])
	{
		return (i+1);
	}


}
cout<<"Element does not exist"<<endl;
return 0;
}

int FormerElem(SqList *L,int e,int &f)
{
int a=LocateElem(L,e);
	if(a==0)
	{
		cout<<"Location does not exist!"<<endl;
	}
	else if(a==1)
	{
		cout<<"Had no previous value!"<<endl;
	}
	else if(a>1&&a<=L->length)
	{
		f=L->elem[a-2];
		return 0;
	}
}

int LaterElem(SqList *L,int e,int &l)
{
int a=LocateElem(L,e);
if(a==0)
{
	cout<<"Location does not exist!"<<endl;
}
else if(a==L->length)
{
	cout<<"Had no later data!"<<endl;
}
else if(a>=1&&a<L->length)
{
	l=L->elem[a];
	return 1;
}
}

void ArrayInsert(SqList *L,int i,int e){
int a= ArrayEmpty(L);
if(a==0)
{
	cout<<"List does not exist!"<<endl;
	return;
}
else
{
	if(i<1||i>L->length+1)
	{
		cout<<"Wrong insertion position!"<<endl; 
	}
	else
	{
		for(int j=L->length-1;j>=i-1;j--)
		{
			L->elem[j+1]=L->elem[j];
		}
		L->elem[i-1]=e;
		L->length++;
	}
}
} 

void ArrayDelete(SqList *L,int i)
{
int a= ArrayEmpty(L);
if(a==0)
{
	cout<<"List does not exist!"<<endl;
}
else
{
	int b=IfEmptyList(L);
	if(b==0)
	{
		cout<<"Empty list!"<<endl;
	}
}
if(i<1||i>L->length)
{
	cout<<"Incorrect deletion position!"<<endl; 
}
else
{
	for(int j=i-1;j<L->length;j++)
	{
		L->elem[j]=L->elem[j+1];
	}
		L->elem[L->length]=0;
		L->length--;
}	
}

void ArrayCout(SqList *L)
{
int a=ArrayEmpty(L);
if(a==0)
{
	cout<<"List does not exist!"<<endl;
}
else
{
	for(int i=0;i<(L->length);i++)
	{
		cout<<L->elem[i]<<" ";
	}
	cout<<endl;
}	
}

void MergeArray(SqList *L1,SqList *L2,SqList *L3)
{
int i=1,j=1,k=0;
		int p,q;
int L1_len=ArrayLength(L1); 
int L2_len=ArrayLength(L2); 
while((i<=L1_len)&&(j<=L2_len))
{

	int L1_data=GetElem(L1,i,p);
	int L2_data=GetElem(L2,j,q);
	if(p<=q)
	{
		ArrayInsert(L3,++k,p);
		i++;
	}
	else
	{
		ArrayInsert(L3,++k,q);
		j++;
	}
}
while(i<=L1_len)
{
	int L1_data=GetElem(L1,i++,p);
	ArrayInsert(L3,++k,p);
}
while(j<=L2_len)
{
	int L2_data=GetElem(L2,j++,q);
	ArrayInsert(L3,++k,q);	
}
}

void CreatArray1(SqList *L)
{
L->elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
L->length=0;
L->listsize=LIST_INIT_SIZE;
}

特别注意:
仅供参考学习,转载请附上原文链接
分享学习心得,如有侵权,望联系本人处理
还在读大学的程序员,项目经验少,如有纰漏,感谢指正
需要源代码请联系本人
谢谢配合

如果这篇文章对您有帮助,小小的点个赞,算是给小学弟的鼓励吧!谢谢大佬!!/呱呱.jpg


转载:https://blog.csdn.net/qq_44749053/article/details/102489529
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场