数据结构--顺序表

SqList.h

#ifndef ALGO2_1_H
#define ALGO2_1_H

#define Maxsize 50
typedef char ElemType;
typedef struct
{
	
	ElemType data[Maxsize];
	int length;
}SqList;//顺序表类型定义

/*初始化顺序表*/
void InitList(SqList *&L);//&L是以应用方式调用L,可以修改指针L

/*判断顺序表是否为空*/
int ListEmpty(SqList *L);

/*返回顺序表的个数*/
int ListLength(SqList *L);

/*打印顺序表*/
void PrintList(SqList *L);

/*获取顺序表中第i个元素*/
int GetElem(SqList *L,int i,ElemType &e);

/*在顺序表中查找元素e,返回它的序号*/
int LocateElem(SqList *L,ElemType e);

/*在顺序表的第i个位置插入元素e*/
int InsertList(SqList *&L,int i,ElemType e);//&L是以应用方式调用L,可以修改指针L

/*在顺序表中删除第i个元素*/
int ListDelete(SqList *&L,int i,ElemType &e);//&L是以应用方式调用L,可以修改指针L

/*释放顺序表*/
void DestoryList(SqList *L);

#endif

SqList.cpp

#include<iostream>
#include<stdlib.h>
#include<malloc.h>
#include"SqList.h"

using namespace std;

/*初始化顺序表*/
void InitList(SqList *&L)//&L是以应用方式调用L,可以修改指针L,初始化链表当然需要对参数做修改
{
	L=(SqList*)malloc(sizeof(SqList));
	L->length=0;		
}


/*判断顺序表是否为空*/
int ListEmpty(SqList *L)
{
	return (L->length==0);	
	
}

/*返回顺序表的个数*/
int ListLength(SqList *L)
{
	return(L->length);	
}

/*打印顺序表*/
void PrintList(SqList *L)
{
	if(ListEmpty(L))
	{
		cout<<"The SqList is empty!"<<endl;
		return ;
	}
	for(int i=0;i<L->length;i++)
		cout<<L->data[i]<<'\t';
	cout<<endl;
}

/*获取顺序表中第i个元素*/
int GetElem(SqList *L,int i,ElemType &e)
{
	if(i<0||i>L->length)
	{
		return 0;
	}
	e=L->data[i];
	return 1;
}

/*在顺序表中查找元素e,返回它的下标*/
int LocateElem(SqList *L,ElemType e)
{
	int i;
	for(i=0;i<L->length;i++)
	{
		if(L->data[i]!=e)
			continue;
		else
			break;	
	}	
	if(i>=L->length)
		return 0;
	else
		return i;
}

/*在顺序表的第i个位置插入元素e*/
int InsertList(SqList *&L,int i,ElemType e)//&L是以应用方式调用L,可以修改指针L
{
	if(i<0||i>L->length)
		return 0;
	for(int j=L->length;j>i;j--)
	{
		L->data[j]=L->data[j-1];//从第i个元素开始依次向后移动
	}
	L->data[i]=e;
	L->length++;
	return 1;
}

/*在顺序表中删除第i个元素*/
int ListDelete(SqList *&L,int i,ElemType &e)//&L是以应用方式调用L,可以修改指针L
{
	if(i<0||i>=L->length)
		return 0;
	e=L->data[i];//保存删除的元素
	for(int j=i;j<L->length-1;j++)
	{
		L->data[j]=L->data[j+1];
	}	
	L->length--;
	return 1;
}

/*释放顺序表*/
void DestoryList(SqList *L)
{
	free(L);
}

main.cpp

#include<iostream>
#include"SqList.h"

using namespace std;

int main()
{
	SqList *L;
	ElemType e;
	cout<<"(1)初始化顺序表L"<<endl;
	InitList(L);

	cout<<"(2)依次采用尾插入法插入a,b,c,d,e,g,h元素"<<endl;
	InsertList(L,0,'a');
	InsertList(L,1,'b');
	InsertList(L,2,'c');
	InsertList(L,3,'d');
	InsertList(L,4,'e');
	InsertList(L,5,'g');
	InsertList(L,6,'h');

	cout<<"(3)输出顺序表L:"<<endl;
	PrintList(L);

	cout<<"(4)顺序表L的长度为L->length="<<ListLength(L)<<endl;
	
	cout<<"(5)判断顺序表是否为空"<<endl;
	if (ListEmpty(L))
	{
		cout<<"顺序表为空"<<endl;
	} 
	else
	{
		cout<<"顺序表不为空"<<endl;
	}
	GetElem(L,2,e);
	cout<<"(6)顺序表L的第2个元素为="<<e<<endl;

	cout<<"(7)顺序表中元素a的位置i="<<LocateElem(L,'a')<<endl;

	cout<<"(8)在第3个元素位置上插入f元素"<<endl;
	InsertList(L,3,'f');
	cout<<"(9)输出顺序表"<<endl;
	PrintList(L);

	cout<<"(10)删除顺序表中的第二个元素"<<endl;
	ListDelete(L,2,e);																	
	cout<<"(11)输出顺序表L"<<endl;
	PrintList(L);

	cout<<"(12)释放顺序表L"<<endl;
	DestoryList(L);

	
	return 0;
}

实验结果:



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值