顺序表的实现-数据结构

顺序表的实现
#include "iostream"
#include "stdlib.h"
#include "windows.h"
using namespace std;
#define OK 1
#define ERROR 0
const int M = 100;
typedef int ElemType;

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

int InitList(SqList &L);//ok
void CreateList(SqList &L, int n);//建立n个元素的线性表//ok
void TraverseList(SqList L);//遍历线性表//ok
void SaveList(SqList L);//保存顺序表的中数据到文件中 //ok
void LoadList(SqList &L);//从文件中加载数据到顺序表 //ok
int ListEmpty(SqList L);//判线性表是否为空 //ok
int ListFull(SqList L);//判线性表是否满 //ok
int ListLength(SqList L);//求线性表长度 //ok
int GetElem(SqList L, int i, ElemType &e);//求线性表中第i个元素的值 //ok
int LocateElem(SqList L, ElemType e);//求值为e的元素在线性表中的位置 //ok
int ListInsert(SqList &L, int i, ElemType e);//在第i个元素前插入值为e的元素 //ok
int ListDelete(SqList &L, int i, ElemType &e); //删除第i个元素,并用e取出其值
void DeleteElem(SqList &L, ElemType item);//删除表中所有值为item的数据元素,要求时间复杂度为O(n)
void DestroyList(SqList &L);//销毁线性 //ok
void ClearList(SqList &L);//清空线性表 //ok
int PriorElem(SqList L, ElemType cur_e, ElemType &prior_e);//求当前元素的前驱 //ok
int NextElem(SqList L, ElemType cur_e, ElemType &next_e);//求当前元素的后继 //ok

void scan(){
	SqList L;
	if (InitList(L)) cout << "顺序表初始化成功!" << endl;
	Sleep(1000);
	system("cls");
	/*int i;
	cout << "请输入即将录入的元素个数:" << endl;
	cin >> i;
	CreateList(L, i);*/
	LoadList(L);
	cout << "顺序表如下:" << endl;
	TraverseList(L);
	cout << "在第四个元素前插入100后如下:" << endl;
	ListInsert(L,4,100);
	TraverseList(L);
	/*SaveList(L);
	cout << "正在存储,请稍候..."<<endl;
	Sleep(1000);
	system("cls");*/
	/*int d, h;//初始化将要读取的元素
	cout << "请输入即将读取的元素的位置" << endl;
	cin >> d;
	GetElem(L, d, h);
	cout << endl << h << endl;*/
}
int main(){
	scan();
	return 0;
}

int InitList(SqList &L){
	L.elem = new ElemType[M];
	if (!L.elem) exit(OVERFLOW);
	L.length = 0;
	L.ListSize = M;
	return OK;
}

void CreateList(SqList &L, int n){
	cout << "请输入元素:" << endl;
	for (int j = 0; j<n; j++)
		cin >> L.elem[j];
	L.length = n;
}

void TraverseList(SqList L){
	for (int i = 0; i<L.length; i++){
		if (i == (L.length - 1))
			cout << L.elem[i] << endl;
		else
			cout << L.elem[i] << " ";
	}
}

int GetElem(SqList L, int wh, ElemType &e){
	if (wh<1 || wh>L.length)
		return ERROR;
	e = L.elem [wh-1];
	return OK;
}

void SaveList(SqList L){
	FILE *fp;
	while ((fp = fopen("D:\\顺序表数据.dat", "wb")) == NULL){
		cout << "Open the file failed!" << endl;
		return;
	}
	fwrite((char*)L.elem ,sizeof(ElemType),L.length ,fp);
	fclose(fp);
	return;
}

void LoadList(SqList &L){
	FILE *fp;
	while ((fp = fopen("D:\\顺序表数据.dat", "rb")) == NULL){
		cout << "Open the file failed!" << endl;
		return;
	}
	while (fread((char*)&L.elem[L.length], sizeof(ElemType), 1, fp) >= 1){
		L.length++;
	}
	fclose(fp);
	return;
}

int ListEmpty(SqList L){
	if (L.length >= L.ListSize)
		return OK;
	return ERROR;
}

int ListFull(SqList L){
	if (L.length >= L.ListSize)
		return OK;
	return ERROR;
}

int ListLength(SqList L){
	return L.length;
}

int LocateElem(SqList L,ElemType e){
	for (int i = 0; i < L.length; i++){
		if (e == L.elem[i])
			return i+1;
	}
	return ERROR;
}

//在第i个元素前插入值为e的元素
int ListInsert(SqList &L, int i, ElemType e){
	if (ListFull(L) || i < 1 || i>L.length + 1)
		return ERROR;
	if (i == L.length + 1){
		L.elem[L.length] = e;
	L.length++;
	return OK;
	}
	int j = L.length - 1; //j抽象为数组元素的下标
	while (j >= i - 1){
		L.elem[j + 1] = L.elem[j]; //从表尾元素开始,数组元素值依次向后移
		j--;
	}
	L.elem[i - 1] = e;
	L.length + 1;
	return OK;
}

int ListDelete(SqList & L, int i, ElemType & e)
{
	return 0;
}

void ClearList(SqList &L){
	L.length = 0;
}

void DeleteElem(SqList & L, ElemType item)
{

}

void DestroyList(SqList &L){
	if (L.elem)
		delete []L.elem;
}

int PriorElem(SqList L, ElemType cur_e, ElemType &prior_e){
	int i = LocateElem(L, cur_e);
	if (i > 1){
		GetElem(L,i-1,prior_e);
		return OK;
	}
	return ERROR;
}

int NextElem(SqList L, ElemType cur_e, ElemType &next_e){
	int i = LocateElem(L, cur_e);
	if (i<L.length){
		GetElem(L, i, next_e);
		return OK;
	}
	return ERROR;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值