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

news/2024/5/20 9:50:49 标签: 顺序表, 数据结构, 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; 
				if(c<1||c>L.length)
	{
		cout<<"Position is incorrect!"<<endl;
		break;
	}
				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->length==0)
	{
		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){
	if(!L->elem)
	{
		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


http://www.niftyadmin.cn/n/947355.html

相关文章

extends-super例题解析

extends-super(1)定义一个“点”类Point,包含数据成员x,y(坐标点)&#xff0c;定义带参数的构造方法初始化数据成员 x,y 。(2)由点类派生出“圆”类Circle, 增加新的数据成员radious(double类型)&#xff0c;定义带参数的构造方法初始化radious。(3)由圆类派生出“圆柱体”类Cy…

初学Java之干货理论篇_对象和简单数据结构,运算符和表达式

初学Java之干货理论篇初始对象和简单数据类型运算符&#xff0c;表达式和语句初始对象和简单数据类型 1.Java允许在一个Java源文件中编写多个类&#xff0c;但至多只能有一个类使用public修饰 2.如果源文件中有多个类&#xff0c;但没有public类&#xff0c;那么源文件的名字只…

初学Java之干货理论篇_类与对象

初学Java之干货理论篇类与对象类与对象 1.类体的内容由两部分构成&#xff1a;一部分是变量的声明&#xff0c;用来刻画属性&#xff1b;另一部分是方法的定义&#xff0c;用来刻画行为。 2.变量声明部分所声明的变量被称作域变量或成员变量&#xff0c;成员变量在整个类内都有…

初学Java之干货理论篇_子类与继承

初学Java之干货理论篇子类与继承子类与继承 1.继承的好处&#xff1a;提高了代码的复用性&#xff0c;多个类相同的成员可以放到同一个类中。提高了代码的维护性&#xff0c;如果功能的代码需要修改&#xff0c;则修改一处即可。让类与类之间产生了关系&#xff0c;是多态的前…

浅析Java堆,Java方法区内存,Java栈

浅析Java堆和Java栈Java堆&#xff08;存放对象本身&#xff09;Java方法区内存Java栈&#xff08;存放基本数据类型和对象的引用&#xff09;举个栗子方法区内存&#xff1a;一个 堆内存&#xff1a;一个 栈内存&#xff1a;一个线程一个Java堆&#xff08;存放对象本身&#…

Java基本数据类型和引用数据类型的区别和关联

Java基本数据类型和引用数据类型的区别基本数据类型基类特点基类存储原理引用数据类型引类特点引类存储原理区别联系基本数据类型 byte short int long float double char boolean &#xff08;详细属性请自行百度&#xff09;基类特点 简单数据类型是不能简化的、内置…

Java成员变量默认赋值

成员变量默认赋值成员变量没有手动赋值的话&#xff0c;系统赋默认值数据类型&#xff1a; 默认值byte,short,int,long&#xff1a;0 float,double&#xff1a;0.0 (double型比float型存储范围更大&#xff0c;精度更高&#xff0c;所以通常的浮点型的数据在不声明的 情况下都…

Java abstract类

abstractabstract类 &#xff08;1&#xff09;凡是用abstract修饰符修饰的类被称为抽象类。 &#xff08;2&#xff09;抽象类不能被实例化abstract方法 &#xff08;1&#xff09;被abstract所修饰的方法叫做抽象方法&#xff0c;抽象方法的作用在为所有子类定义一个 统一的接…