⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 顺序.cpp

📁 是一个数据结构中关于连续的分配的线形分配的数据结构及一些基本操作
💻 CPP
字号:
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define CRMENT 2
typedef int ElemType;
typedef  int status;
typedef struct{
	ElemType  *elem;
	int length;
	int listsize;
}Sqlist;
status Creat(Sqlist &L,int n,int m){
	cout<<"输入 "<< m  <<"数"<<endl;
	int e,i;
	L.elem=(ElemType*)malloc( n*sizeof(ElemType));
	if(!L.elem) exit(OVERFLOW);
	L.length=m;
	L.listsize=n;
	for(i=0;i<m;i++){cin>>e;L.elem[i]=e;}
	return OK;
    
}

status Locat(Sqlist L,int i,ElemType &e){
	
	if((i<=L.length)&&(i>1)) {e=L.elem[i-1];return OK;}
	else return ERROR;}

status Insert(Sqlist &L,int i,ElemType e){
	ElemType * newspace,*p,*q;
	if(i<1||i>L.length+1) return ERROR;
	if(L.length>=L.listsize) {
		newspace=(ElemType*)realloc(L.elem,(L.listsize+CRMENT)*sizeof(ElemType));
		if(!newspace) exit(OVERFLOW);
        L.elem=newspace;
		L.listsize+=CRMENT;
	}
	q=&L.elem[i-1];
	for(p=&(L.elem[L.length-1]);p>=q;p--){
		*(p+1)=*p;}
	*q=e;
	L.length++;
    return OK;}

status Delete(Sqlist &L,int i,ElemType &e)
{
	ElemType *p,*q;
	if((i<1)||(i>L.length)) return ERROR;
	p=&L.elem[i-1];
	e=*p;
	q=L.elem+L.length-1;
	for(++p;p<=q;++p){*(p-1)=*p;}
	--L.length;
	return OK;
}

 void printf(Sqlist L){
	int w;
	for(w=0;w<L.length;w++)cout<<L.elem[w]<<" ";
	cout<<endl;
	}
main()
{
Sqlist L1;
char x;
int n,m,i;
ElemType e;
cout<<"请输入你的操作"<<endl;
cout<<"a插入 b查找 c删除 退出q"<<endl;
cout<<"输入你要建立顺序表的初始个数n和里面包含的元素个数m"<<endl;
cin>>n>>m;
Creat(L1,n,m);
printf(L1);
cout<<endl;
while((x=getchar())!='q')
{
	switch(x){
	case'a':cout<<"输入你要插入的位置第i个之前和值e"<<endl;cin>>i>>e;Insert(L1,i,e);cout<<"此顺序表现在为:";
		printf(L1);break;
	case'b':cout<<"输入你查找的元素位置第i个"<<endl;cin>>i;Locat(L1,i,e);cout<<"此数为:";cout<<e<<endl;
		break;
    case'c':cout<<"输入你要删除的元素位置第i个"<<endl;cin>>i;Delete(L1,i,e);cout<<"此顺序表现在为:";
		printf(L1);break;
	}
}
}
	
				
			















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -