线性表---顺序表

#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 8
#define ERROR 0
#define OK 1
typedef int status;
typedef int ElemType;
typedef struct 
{
	int length;
	ElemType *elem;
}sqlist;
status chuangjian(sqlist &L);
status charu(sqlist &L,ElemType e,int i);
status chazhao(sqlist L,ElemType e);
status show(sqlist L);
int  quzhi(sqlist L,int i);
status shanchu(sqlist &L,int i);
status sort(sqlist &L);
status sInlist(sqlist &L,ElemType e);
int main()
{
	sqlist L;
	ElemType e;
	int i,choice;
	while(1)
	{
		cout<<"1.创建    2.插入   3.展示   4.取值"<<endl;
		cout<<"5.查找    6.删除   7.排序   0.退出"<<endl;
		cout<<"8.有序输入"<<endl;
		cout<<"请输入选项:";
		cin>>choice; 
		switch(choice)
		{
			case 1:
				if(chuangjian(L)==OK)
				cout<<"创建成功"<<endl;
				else
				cout<<"创建失败"<<endl; 
				break;
			case 2:
				cout<<"插入第i个:"; 
				cin>>i;
				cout<<"输入数据e:";
				cin>>e; 
				int c;
				c=charu(L,e,i);
				if(c==OK)
				cout<<"插入成功"<<endl;
				else
				cout<<"插入失败"<<endl;
				show(L);
				break;
			case 3:	
				show(L);
				break;	
			case 4:
				cout<<"输入要取值的序号:";
				cin>>i;
				int a;
				a=quzhi(L,i);
				if(a==ERROR)
				cout<<"序号输入错误"<<endl;
				else
				cout<<"数值为:"<<a<<endl;
				break;
			case 5:		
				cout<<"请输入查找数据:";
				cin>>e;
				int j; 
				j=chazhao(L,e);
				if(j==0)
				cout<<"查找数据不存在";
				else cout<<"数据位置为:"<<j<<endl; 
				break;
		   	case 6:
				cout<<"删除的序号:";
				cin>>i;		
				if(shanchu(L,i)==ERROR)
				cout<<"删除位置无效";
				else cout<<"删除成功"<<endl;
				show(L);
				break;
			case 7:
				int s;
				s=sort(L);
				if(s==OK)
				cout<<"排序成功"<<endl;
				show(L); 
				break;
			case 8:
				cout<<"输入数据:";
				cin>>e; 
				int w;
				w=sInlist(L,e);
				if(w==OK)
				cout<<"插入成功"<<endl;
				show(L);
				break;	
			case 0:
			return 0;	
		}				
		system("pause");
		system("cls");
	}
	return 0;
}
status chuangjian(sqlist &L)
{
	L.elem=new ElemType [MAXSIZE];
	if(!L.elem) return ERROR;
	L.length=0;
	return OK;
}
status charu(sqlist &L,ElemType e,int i)
{
	int j;
	if(i<1||i>L.length+1) return ERROR;
	if(L.length==MAXSIZE) return ERROR;
	for(j=L.length-1;j>=i-1;j--)
		L.elem[j+1]=L.elem[j];
		L.elem[i-1]=e;
		++L.length;
		return OK;
}
status show(sqlist L)
{
	if(L.length==0)
	return ERROR;
	int j;
	for(j=0;j<L.length;j++)
	{
		cout<<L.elem[j]<<" "; 
	}
	cout<<endl;
}
int quzhi(sqlist L,int i)
{
	if(i<1||i>L.length)  return ERROR;
	return L.elem[i-1];
}
status chazhao(sqlist L,ElemType e)
{
	int j;
	for(j=0;j<L.length;j++)
	{
		if(e==L.elem[j])
		return j+1;
	}
	return 0;
}
status shanchu(sqlist &L,int i)
{
	if(i<1||i>L.length)
	return ERROR;
	for(int j=i;j<L.length;j++)
	{
		L.elem[j-1]=L.elem[j];
	}
	--L.length;
	return OK;
}
status sort(sqlist &L)
{
	int i,j;
	for(i=0;i<L.length;i++)
	{
		for(j=0;j<L.length-i;j++)
		{
			int t=0;
			if(L.elem[j]<L.elem[j+1])
			{
				t=L.elem[j];
				L.elem[j]=L.elem[j+1];
				L.elem[j+1]=t;
			}
		}
	}
	return OK;
}
status sInlist(sqlist &L,ElemType e)
{
	if(L.length==MAXSIZE) return ERROR;
	int i=0;
	while(i<L.length&&L.elem[i]<e)
	{
		i++;
	}
	charu(L,e,i+1);
	return OK;
}

创建

通过返回值判断是否创建成功。

插入

插入时应注意判断长度是否等于最大长度。

排序

排序中可采取选择排序和冒泡排序,冒泡排序略简。

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值