顺序表的操作

实验代码:

#include <iostream>
#include<algorithm>
#define MAXSIZE 10
#define OK 1
#define OVERFLOW -2
#define ERROR 0
typedef int ElemType;
typedef int Status;
using namespace std;

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

Status InitList(SqList &L)//初始化 
{
    L.elem=new ElemType[MAXSIZE];
    if(L.elem==NULL)
    {
        cout<<"存储空间分配失败"<<endl;
        exit(-2);

    }
    L.length=0;
    cout<<"顺序表初始化完成"<<endl;
    return 0;
}

void DefineList(SqList &L,int n)//定义 
{
    cout<<"请输入顺序表的元素:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>L.elem[i];
        L.length++;
    }

}
Status DeleteElem(SqList &L,int i)//删除元素 
{
    if(i<1||i>L.length) 
	{
		cout<<"不存在"<<endl;
		return ERROR;
	} 
    for(int j=i;j<L.length;j++)
    {
        L.elem[j-1]=L.elem[j];
    }
    L.length--;
    cout<<"已删除"<<endl;
    return OK;
}

Status IsNULL(SqList &L)//判断空 
{
    if(L.length==0) return OK;
    return ERROR;
}

Status Len(SqList &L)//求长度 
{
 return L.length;
}

Status GetElem(SqList &L,int i)//获取元素 
{
    if(i<1||i>L.length) 
	{
		cout<<"不存在"<<endl;return ERROR;
	} 
	cout<<L.elem[i-1]<<endl;
	return OK;
}

Status BeforeElem(SqList &L,int i)//前驱 
{
    if(i<2||i>L.length) 
	{
		cout<<"不存在"<<endl; 
		return ERROR;
	} 
	cout<<"前驱:"<<L.elem[i-2]<<endl;
    return OK;
}

Status AfterElem(SqList &L,int i)//后继 
{
    if(i<1||i>L.length-1) 
    {
		cout<<"不存在"<<endl; 
		return ERROR;
	} 
	cout<<"后继:"<<L.elem[i]<<endl;
    return OK;
}

void ShowList(SqList &L)//显示 
{
	cout<<"线性表:"; 
    for(int i=0;i<L.length;i++)
    {
        cout<<L.elem[i]<<" ";
    }
    cout<<endl;
}

void ClearList(SqList &L)//清空 
{
for(int i;i<L.length;i++)
{
	L.elem[i]=0;
}	
L.length=0;
cout<<"已清空"<<endl;
} 

void DestoryList(SqList &L)//销毁 
{
	free(L.elem);
	cout<<"已销毁"<<endl;
	exit(0);
}

int InsertList(SqList &L,int i,int c)//插入 
{
	 
	if(i<1||i>L.length+1) {cout<<"插入位置不合法"<<endl;return ERROR;};
	int *a,*b;
	a=&(L.elem[i-1]);
	for(b=&(L.elem[L.length-1]);b>=a;b--)
	{
		*(b+1)=*b;
	}
	*a=c;
	L.length++;
	return OK;
}

Status siteElem(SqList &L,int n)//查找 
{
	for(int i=0;i<L.length;i++)
	{
		if(L.elem[i]==n)
		{
			cout<<"位置:"<<i+1<<endl;
			return OK;
		}
	}
	cout<<"不存在"<<endl;
	return ERROR;
}

int main()
{
SqList L;
InitList(L);
int state=1;
int n;
cout<<"请输入线性表长度:"<<endl;
cin>>n;
DefineList(L,n);
while(state)
{
	cout<<"学号:2012080010\n";
    cout<<"姓名:张玮\n";
    cout<<"====================线性表操作====================\n";
    cout<<"1.显示线性表 2.清空 3.销毁 4.求长度 \n";
    cout<<"5.判断是否为空 6.获取元素 7.插入元素 \n";
    cout<<"8.删除元素 9.求前驱 10.求后继 11.查找元素\n";
    cout<<"==================================================\n";
    cout<<"请输入选项:";
    cin>>state;
    if(state==1) {ShowList(L);} 
    if(state==2) ClearList(L);
    if(state==3) DestoryList(L);
    if(state==4) cout<<"顺序表长度为:"<<Len(L)<<endl;
    if(state==5) 
	{
		if(IsNULL(L)) cout<<"NULL"<<endl;
		else cout<<"NOT NULL"<<endl;
	} 
    if(state==6) 
    {
    	int num;
    	cout<<"请输入要获取的元素位置:";
    	cin>>num;
    	GetElem(L,num);
	}
    if(state==7)
    {
    	
    	int num;
    	cout<<"请输入要插入的元素位置:";
    	cin>>num;
    	int s;
		cout<<"请输入要插入的元素的值:";
    	cin>>s;
    	InsertList(L,num,s);	
	
	}
    if(state==8) 
    {
    	int num;
    	cout<<"请输入要删除的元素位置:";
    	cin>>num;
    	DeleteElem(L,num);
    	
	}
    if(state==9)
    {
    	int num;
    	cout<<"请输入元素位置:";
    	cin>>num;
    	BeforeElem(L,num);
    	 
	}
    if(state==10)
    {
    	int num;
    	cout<<"请输入元素位置:";
    	cin>>num;
    	AfterElem(L,num);
	}
	if(state==11)
	{
		int num;
		cout<<"请输入要查找的元素:";
		cin>>num;
		siteElem(L,num);
		
	}
    

} 
return 0;
}

总结:顺序表是线性表的一种,顺序表在存储数据时,会申请一块足够大小的物理空间将数据依次存储起来,存储空间连续,定义顺序表时,要定义顺序表的元素类型和长度,初始化顺序表时,给顺序表申请一块足够大小的存储空间,并给顺序表长度赋值为0。在对顺序表进行操作时,特别注意操作的元素位置是否合法,是否超出当前顺序表长度。顺序表的清空是将元素删除,顺序表长度赋值为0,顺序表的销毁是将该顺序表的元素删除,再将其申请的存储空间释放。
刚接触顺序表,很多地方理解不够透彻,借这个实验加深理解

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
顺序表是一种常见的数据结构,它可以用数组实现。下面是一些常见的顺序表操作代码: 1. 初始化顺序表: ```c++ #define MAX_SIZE 100 // 定义顺序表的最大长度 typedef struct { int data[MAX_SIZE]; // 用数组存储数据元素 int length; // 当前顺序表长度 } SeqList; void InitList(SeqList &L) { L.length = 0; // 初始化长度为0 } ``` 2. 插入元素到顺序表中: ```c++ bool Insert(SeqList &L, int pos, int elem) { if (pos < 1 || pos > L.length + 1) { return false; // 插入位置不合法 } if (L.length >= MAX_SIZE) { return false; // 顺序表已满,无法插入 } for (int i = L.length; i >= pos; i--) { L.data[i] = L.data[i - 1]; // 将插入位置及之后的元素后移一位 } L.data[pos - 1] = elem; // 在插入位置插入新元素 L.length++; // 长度加1 return true; } ``` 3. 删除顺序表中指定位置的元素: ```c++ bool Delete(SeqList &L, int pos) { if (pos < 1 || pos > L.length) { return false; // 删除位置不合法 } for (int i = pos; i < L.length; i++) { L.data[i - 1] = L.data[i]; // 将删除位置之后的元素前移一位 } L.length--; // 长度减1 return true; } ``` 4. 查找顺序表中指定元素的位置: ```c++ int Find(SeqList L, int elem) { for (int i = 0; i < L.length; i++) { if (L.data[i] == elem) { return i + 1; // 返回元素在顺序表中的位置 } } return -1; // 未找到元素 } ``` 5. 获取顺序表中指定位置的元素: ```c++ bool GetElem(SeqList L, int pos, int &elem) { if (pos < 1 || pos > L.length) { return false; // 获取位置不合法 } elem = L.data[pos - 1]; // 获取指定位置的元素 return true; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值