博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
201SqList
阅读量:6969 次
发布时间:2019-06-27

本文共 4152 字,大约阅读时间需要 13 分钟。

hot3.png

注意:要严格按照后缀名新建文件。

如果按.h创建文件,后来简单重命名为.cpp文件,编译会出错。

顺序表的实现 包含4个文件:

    c1.h 是预处理指令;

    c2-1.h 是SqList的数据结构;

    bo2-1.cpp 是SqList的基本操作函数(basic operations 缩写为 bo);

    main2-1.cpp 是测试函数。

//c1.h#include
#include
#include
#define OK 1#define ERROR 0#define INFEASIBLE -1typedef int Status;
//c2-1.h#ifndef C2_1_H#define C2_1_Htypedef int ElemType;#define INIT_SIZE 10#define INCREMENT 2struct SqList{	ElemType *elem;	int length;	int listsize;};void InitList(SqList &L);int ListLength(SqList L);Status ListEmpty(SqList L);void ListTraverse(SqList L);Status ListInsert(SqList &L, int index, ElemType e);Status ListDelete(SqList &L, int index, ElemType &e);void ClearList(SqList &L);Status GetElem(SqList L, int index, ElemType &e);int LocateElem(SqList L, ElemType e);Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e);Status NextElem(SqList L, ElemType cur_e, ElemType &next_e);void DestroyList(SqList &L);#endif
//bo2-1.cpp#include "c1.h"#include "c2-1.h"using namespace std;void InitList(SqList &L){	if (!(L.elem = (ElemType *)malloc(INIT_SIZE*sizeof(ElemType))))		exit(OVERFLOW);	L.length = 0;	L.listsize = INIT_SIZE;}int ListLength(SqList L){	return L.length;}Status ListEmpty(SqList L){	if (L.length == 0)		return OK;	else		return ERROR;}void ListTraverse(SqList L){	for (int i = 0; i < L.length; i++)	{		if (i % 10 == 0 && i != 0)			cout << endl;		cout << L.elem[i] << "   ";	}	cout << endl;}Status ListInsert(SqList &L, int index, ElemType e){		if (index >= 1 && index <= L.length + 1)	{		if (L.listsize == L.length)		{			if (!(L.elem = (ElemType *)realloc(L.elem, (L.length + INCREMENT)*sizeof(ElemType))))				exit(OVERFLOW);			L.listsize = L.length + INCREMENT;		}				//insert by index		/*		if (index == L.length + 1)		{			L.elem[index - 1] = e;		}		else		{			for (int i = L.length - 1; i >= index - 1; i--)			{				L.elem[i + 1] = L.elem[i];			}			L.elem[index - 1] = e;		}		*/		//insert by pointer		ElemType *p, *q;		p = L.elem + index - 1;		for (q = L.elem + L.length - 1; q >= p; q--)			*(q + 1) = *q;		*p = e;				L.length++;		return OK;	}	else		return ERROR;}Status ListDelete(SqList &L, int index, ElemType &e){	if (index >= 1 && index <= L.length)	{		ElemType *p, *q;		p = L.elem + index - 1;		e = *p;		q = L.elem + L.length - 1;		while (p < q)		{			*p = *(p + 1);			p++;		}			L.length--;		return OK;	}	else		return ERROR;}void ClearList(SqList &L){	L.length = 0;}Status GetElem(SqList L, int index, ElemType &e){	if (index < 1 || index > L.length)		return ERROR;	e = *(L.elem + index - 1);	return OK;}int LocateElem(SqList L, ElemType e){	if (L.length == 0)		return 0;	else	{		for (int i = 0; i < L.length; i++)		{			if (e == *(L.elem + i))				return i + 1;		}		return 0;	}	}Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e){	int location = LocateElem(L, cur_e);	if (location == -1 || location == 1)		return ERROR;	pre_e = *(L.elem + location - 2);	return OK;}Status NextElem(SqList L, ElemType cur_e, ElemType &next_e){	int location = LocateElem(L, cur_e);	if (location == -1 || location == L.length)		return ERROR;	next_e = *(L.elem + location);	return OK;}void DestroyList(SqList &L){	free(L.elem);	L.elem = nullptr;	L.length = 0;	L.listsize = 0;}
//main2-1.cpp#include "c1.h"#include "c2-1.h"using namespace std;int main(){	SqList L;	InitList(L);	cout << "ListLength: " << ListLength(L) << endl;	cout << "ListEmpty: " << ListEmpty(L) << endl;	for (int i = 1; i <= 100; i++)		ListInsert(L, i, i);	cout << "ListLength: " << ListLength(L) << endl;	ListTraverse(L);	ElemType e;	ListDelete(L, 1, e);	cout << "ListLength: " << ListLength(L) << endl;	ListTraverse(L);	cout << "deleted element: " << e << endl;	GetElem(L, 33, e);	cout << "GetElem: " << e << endl;	cout << "LocateElem: " << LocateElem(L, 2) << endl;	PriorElem(L, 67, e);	cout << "PriorElem: " << e << endl;	NextElem(L, 67, e);	cout << "NextElem: " << e << endl;	ClearList(L);	ListTraverse(L);	cout << "ListLength: " << ListLength(L) << endl;	DestroyList(L);	cout << "ListLength: " << ListLength(L) << endl;	cout << "ListSize: " << L.listsize << endl;	cin.get();	return 0;}

转载于:https://my.oschina.net/kuailechengxuyuan/blog/671111

你可能感兴趣的文章
memcached编译安装
查看>>
根文件系统基础
查看>>
学习Linux决心书
查看>>
2017年 JavaScript 框架回顾 -- React生态系统
查看>>
不停机发布策略
查看>>
OpenStack各组件介绍
查看>>
J.U.C工具类中的CountDownLatch和CyclicBarrier
查看>>
dovecot+mysql
查看>>
Mysql密码管理及授权
查看>>
JAVA线程安全之synchronized关键字的正确用法
查看>>
springmvc+mybatis+dubbo分布式平台-maven构建根项目
查看>>
一个小常识
查看>>
Nginx防盗链 Nginx访问控制 Nginx解析php相关配置 Nginx代理
查看>>
解决虚拟机中使用ntpdate报错:ntpdate[46700]: no server suitab
查看>>
Docker 快速删除所有容器
查看>>
【OCP认证12c题库】CUUG 071题库考试原题及答案(27)
查看>>
OSS支持IPV6/IPV4双栈访问域名
查看>>
阿里云应用实时监控 ARMS 再升级,支持 Prometheus 开源生态
查看>>
最全面的IGMP协议总结!
查看>>
你还在 Select * 吗?
查看>>