数据结构——顺序表算法及完整操作

本文详细介绍了线性表的顺序存储结构实现,包括初始化、销毁、清空、获取长度、判断空表、取值、查找、插入、删除等基本操作函数,以及创建、插入、删除、查找和输出功能函数的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <malloc.h>
#include <iostream> 
using namespace std;

#define LIST_INIT_SIZE 100	//线性表存储空间初始分配量
#define LISTINCREMENT  10 	//线性表存储空间的分配增量
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define MAX_SIZE 100
#define List int

typedef int Status;
typedef int ElemType;
typedef struct
{
    ElemType *elem;		//存储空间基址
    int length;    		//当前长度
    int listsize;  		//当前分配的存储容量(以sizeof(ElemType)为单位)
} SqList;

/****************基本操作函数*****************/ 
//1.线性表L的初始化
Status InitList_Sq(SqList &L)			//构建一个空的线性表 
{
	L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
	if ( !L.elem )						//存储分配失败	
		exit(OVERFLOW);
	L.length = 0;						//空表长度为0 
	L.listsize = LIST_INIT_SIZE;		//初始化存储容量 
	return OK;
} 

//2.销毁线性表L
void Destroy(SqList &L)
{
	if (L.elem)
		free(L.elem);					//释放存储空间 
} 

//3.清空线性表L 
void ClearList(SqList &L)
{
	L.length = 0;						//将线性表长度置为0 
}

//4.求线性表的长度
int GetLength(SqList L)
{
	return(L.length);
} 

//5.判断线性表是否为空
int IsEmpty(SqList L)
{
	if (L.length == 0)
		return OK;
	else
		return ERROR;
} 

//6.顺序表的取值,根据位置i获取内容
int GetElem(SqList L, int i, ElemType &e)
{
	if (i < 1 || i > L.length)		//判断i的位置是否合理 
		return ERROR;
	else
		return (L.elem[i-1]); 		//第i-1个单元存储着第i个数据 
} 

//7.顺序表的查找
//算法思想:
//	1.在线性表L中查找与指定元素e相同的数据元素的位置
//	2.从表的一端开始,逐个进行比较 
int LocateElem(SqList L, ElemType e)
{
	for (int i = 0; i < L.length; i++)
	{
		if (L.elem[i] == e)
			return (i+1);		
	}
	return ERROR;
}

//8.顺序表的插入
//算法思想:
//	1.判断插入位置i是否合法
//	2.判断顺序表的存储空间是否已满,已满增加存储空间
//	3.将第n至第i位的元素一次向后移动一个位置,空出第i个位置
//	4.将要插入的新元素放入e放入第i个位置
//	5.表长加1,插入成功返回OK 
Status ListInsert_Sq(SqList &L , int i, ElemType e)
{
	ElemType *newbase, *p, *q;
	
	if (i < 1 || i > L.length+1)		//i值不合法 
		return ERROR;
		
	if (L.length >= L.listsize)			//当前存储空间已满,增加分配 
	{
		newbase = (ElemType *)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
		if ( !newbase )					//分配失败 
			exit(OVERFLOW);
		L.elem = newbase;				//新基址 
		L.listsize += LISTINCREMENT;	//增加存储容量 
	}
	
	q = &(L.elem[i-1]);					//q为插入位置的地址 
	for (p = &L.elem[L.length-1]; p >= q; --p)
	{
		*(p+1) = *p;					//插入位置及之后的元素后移一个位置 
	}
	*q = e;								//全部移动完后插入e 
	++L.length;							//表长加1 
	return OK;
} 

//9.顺序表的删除
//算法思想:
//	1.判断删除位置i是否合法
//	2.将欲删除的元素保存在e中
//	3.将第i+1至n位元素依次向前移动一个位置
//	4.表长减1,删除成功返回OK
Status ListDelete_Sq(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;	
}

//10.创建顺序表函数 初始化前n个数据
int CreatList(SqList &L, int n)
{
	if (n < 0 || n > LIST_INIT_SIZE)
		return ERROR;				//n非法
	for (int i = 0; i<n; i++)
	{
		scanf("%d", &L.elem[i]);
		L.length++;
	}
	return OK;	
}


 
//********************************功能函数*****************************************//
//输出功能函数 按位置从小到大输出顺序表所有元素
void PrintList(SqList L)
{
	printf("当前顺序表所有元素:");
	for (int i = 0; i<L.length; i++)
	{
		printf("%d ", L.elem[i]);
	}
	printf("\n");
}

//创建顺序表函数
void Create(SqList &L)
{
	int n;
	L.length = 0;
	printf("请输入要创建的顺序表长度(>1):");
	scanf("%d", &n);
	printf("请输入%d个数(空格隔开): ", n);
	
	if ( CreatList(L, n) ) 
	{
		printf("创建成功!\n");
		PrintList(L);
	}
	else 
		printf("输入长度非法!\n");
 
}

//插入功能函数 调用InsertList完成顺序表元素插入 调用PrintList函数显示插入成功后的结果
void Insert(SqList &L)
{
	int place; 
	ElemType e; 
	bool flag;
	printf("请输入要插入的位置(从1开始)及元素:\n");
	scanf("%d%d", &place, &e);
	flag = ListInsert_Sq(L, place, e);
	if (flag)
	{
		printf("插入成功,插入的元素是 %d !!!\n", e);
		PrintList(L);
	}
	else
	{
		printf("插入位置非法,插入失败!!!\n");
	}
}

//删除功能函数 调用ListDelete函数完成顺序表的删除 调用PrintList函数显示插入成功后的结果
void Delete(SqList &L)
{
	int place;
	ElemType e; 
	bool flag;
	printf("请输入要删除的位置(从1开始):\n");
	scanf("%d", &place);
	flag = ListDelete_Sq(L, place, e);
	if (flag)
	{
		printf("删除成功,删除的元素是 %d !!!\n", e);
		PrintList(L);
	}
	else
	{
		printf("删除位置非法,删除失败!!!\n");
	}
}

//查找功能函数 调用LocateElem查找元素
void Search(SqList L)
{
	ElemType e; 
	int flag;
	printf("请输入要查找的值:\n");
	scanf("%d", &e);
	flag = LocateElem(L, e);
	if (flag)
	{
		printf("该元素位置为:%d\n", flag);
	}
	else
		printf("未找到该元素!\n");
}

//菜单	
void menu()
{
	printf("********1.创建    2.插入*********\n");
	printf("********3.删除    4.查找*********\n");
	printf("********5.输出    6.退出*********\n");
}

int main()
{
	SqList L; 
	int choice;
	InitList_Sq(L);
	while (1)
	{
		menu();
		printf("请输入菜单序号:\n");
		scanf("%d", &choice);
		if (6 == choice) break;
		switch (choice)
		{
			case 1:Create(L); break;
			case 2:Insert(L); break;
			case 3:Delete(L); break;
			case 4:Search(L); break;
			case 5:PrintList(L); break;
			default:printf("输入错误!!!\n");
		}
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值