数组实现线性表的顺序存储

课本第一个习题吧

一、线性表的概念:
    1、线性表是一种最简单、最常用的数据结构,通常一个线性表是由n(n>=0)个性质相同的数据元素组成的有限序列,长度即为元素的个数n,当n=0时,称该表为空表。
    2、非空的线性结构的特点:它具有惟一的第一个数据元素和最后一个数据元素;并且除了第一个元素以外,其他数据元素都只有一个前驱和一个后继。
    3、线性表的主要存储结构:顺序存储结构 和 链式存储结构。(本篇主要总结顺序存储结构)
 
二、线性表的顺序表示和实现
   顺序存储结构的存储可以用一维数组表示,具有固定长度。
   也可以用动态分配的存储结构,可以合理地利用空间。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;

#define OK 1
#define OVERFLOW -1
#define ERROR -2
#define MAXSIZE 100

typedef int ElemType;

typedef struct List
{
	ElemType elem[MAXSIZE];
	int length;
}List;

List InitList()
{
	List L;
	memset(L.elem, -1, sizeof(L.elem));
	L.length = 0;
	cout << "顺序表已初始化" << endl << endl;
	return L;
}

void ClearList(List *L)
{
	memset(L->elem, -1, sizeof(L->elem));
	L->length = 0;
	cout << "顺序表已清空" << endl << endl;
}

void EmptyList(List *L)
{
	if(L->length == 0)
	{
		cout << "顺序表为空" << endl << endl;
	}
	else
	{
		cout << "顺序表不为空" << endl << endl;
	}
}

void FullList(List *L)
{
	if(L->length == MAXSIZE)
	{
		cout << "顺序表已满" << endl << endl;
	}
	else
	{
		cout << "顺序表未满" << endl << endl;
	}
}

int ListLength(List *L)
{
	return L->length;
}

void TraverseList(List *L)
{
	if(L->length == 0)
	{
		cout << "顺序表为空" << endl;
	}
	else
	{
		cout << "当前顺序表中的元素为:" << endl;
		for(int i = 1; i <= L->length; i++)
		{
			cout << L->elem[i-1] << " ";
		}
		cout << endl << endl;
	}
}

ElemType GetList(List *L, int i)
{
	if(i < 1 || i > L->length)
	{
		cout << "要查询的位置出错" << endl << endl;
	}
	else
	{
		return L->elem[i-1];
	}
}

int LocateList(List *L, ElemType e)
{
	int i;
	for(i = 0; i < L->length; i++)
	{
		if(e == L->elem[i])
		{
			return i + 1;
		}
	}
	if(i >= L->length)
	{
		return ERROR;
	}
}

void InsertList(List *L, int i, ElemType e)
{
	if(L->length == MAXSIZE)
	{
		cout << "顺序表满,不能插入元素" << endl;
	}
	else if(i < 1 || i > L->length)
	{
		cout << "插入位置不正确" << endl;
	}
	else
	{
		for(int j = L->length - 1; j >= i - 1; j--)
		{
			L->elem[j+1] = L->elem[j];
		}
		L->elem[i-1] = e;
		L->length++;
	}
}

void DeleteList(List *L, int i)
{
	ElemType e;
	if(i < 1 || i > L->length)
	{
		cout << "删除位置不正确" << endl << endl;
	}
	else
	{
		e = L->elem[i-1];
		for(int j = i; j <= L->length; j++)
		{
			L->elem[j-1] = L->elem[j];
		}
		L->length--;
		cout << e << "元素已被删除" << endl << endl;
	}
}

void PreList(List *L, ElemType e)
{
	int i = 0;
	while(i < L->length && L->elem[i] != e)
	{
		i++;
	}
	if(i == 0)
	{
		cout << "第一个元素没有前驱" << endl << endl;
	}
	else if(i <= L->length - 1)
	{
		cout << e << " 元素的前驱为 " << L->elem[i-1] << endl << endl;
	}
	else
	{
		cout << "不存在元素" << e << endl << endl;
	}
}

void NextList(List *L, ElemType e)
{
	int i = 0;
	while(i < L->length && L->elem[i] != e)
	{
		i++;
	}
	if(i == 0)
	{
		cout << "第一个元素没有前驱" << endl << endl;
	}
	else if(i <= L->length - 1)
	{
		cout << e << " 元素的后继为 " << L->elem[i+1] << endl << endl;
	}
	else
	{
		cout << "不存在元素" << e << endl << endl;
	}
}

void RandomList(List *L)
{
	ClearList(L);
	srand((unsigned)time(NULL));
	int length;
	length= rand() % 100;
	L->length = length;
	for(int j = 0 ; j < length; j++)
	{
		L->elem[j] = rand() % 100;
	}
	cout << "随机数据已生成" << endl << endl;
}

int scan()
{
	int n;
	cout << "-----------------++++++++++++++++++-------------------" << endl;
	cout << "                请选择所要进行的操作                  " << endl;
	cout << "1.初始化  2.清空  3.求顺序表长度  4.检查顺序表是否为空" << endl;
	cout << "5.检查顺序表是否为满 6.遍历顺序表 7.从顺序表中查找元素" << endl;
	cout << "8.从顺序表中查找与给定元素值相同的元素在顺序表中的位置" << endl;
	cout << "9.向顺序表中插入元素            10. 从顺序表中删除元素" << endl;
	cout << "11.求元素的前驱     12.求元素的后继    13.生成随机数据" << endl;
	cout << "-----------------++++++++++++++++++-------------------" << endl;
	cin >> n;
	return n;
}

int main()
{
	int quit = 0;
	int i;
	List L;
	ElemType e;
	while(!quit)
	{
		switch (scan())
		{
			case 1 : L = InitList();break;
			case 2 : ClearList(&L);break;
			case 3 : cout << "顺序表的长度为:" << ListLength(&L) << endl << endl;break;
			case 4 : EmptyList(&L);break;
			case 5 : FullList(&L);break;
			case 6 : TraverseList(&L);break;
			case 7 : cout << " 请输入要查询的元素的位置" << endl; cin >> i; cout << GetList(&L,i) << endl << endl;break;
			case 8 : cout << "请输入要查询的元素的值" << endl; cin >> e; cout << LocateList(&L,e) << endl << endl;break;
			case 9 : cout << "请输入要插入的元素和位置" << endl; cin >> e >> i; InsertList(&L,i,e);break;
			case 10: cout << "请输入要删除的元素的位置" << endl; cin >> i; DeleteList(&L,i);break;
			case 11: cin >> e; PreList(&L,e);break;
			case 12: cin >> e; NextList(&L,e);break;
			case 13: RandomList(&L);break;
			default:quit = 1;
		}
	}
	return 0;
}
----------------------------------------------------------------------------------------------------------------

写一算法,从顺序表中删除自第i个元素开始的k个元素。

void DeleteListFromI(List *L, int i, int k)
{
	ElemType e;
	if(i < 1 || i > L->length || k > L->length || i + k > L->length + 1)
	{
		cout << "删除位置不正确" << endl << endl;
	}
	else
	{
		e = L->elem[i-1];
		for(int j = i + k - 1; j <= L->length; j++)
		{
			L->elem[j-k] = L->elem[j];
		}
		L->length -= k;
		cout << "从" << e << "开始的" << k << "个元素已被删除" << endl << endl;
	


原文地址:https://www.cnblogs.com/lgh1992314/p/5835179.html