线性表的相关操作

# include<iostream>
# include<stdlib.h>
using namespace std;
typedef int ZhizhenType;//使用zhizhenType代替int,便于了解那个定义的是int*类型,定义的变量和List内的数值有关
struct List
{
	ZhizhenType * list; //存储线性表的动态存储空间的指针
	int size; //存储线性表的长度
	int ListSize; //初始化list数组的长度
};
//初始化List为空
void InitList(List &L)
{
	L.ListSize = 10;
	L.list = new ZhizhenType[L.ListSize]; //给list分配动态存储空间
	if (L.list == NULL)
	{
		cout << "存储空间用完,程序退出" << endl;
		exit(1);  //exit 表示让程序退出,exit(0) 表示程序正常退出,exit⑴/exit(-1)表示程序异常退出
	}
	L.size = 0; //list长度为0,为空表
}
//删除L中的元素
void ClearList(List &L)
{
	if (L.list != NULL)
	{
		delete[]L.list; //释放list数组的存储空间
		L.list = NULL;
	}
	L.ListSize = 0;
	L.list = 0;
}
//返回List的长度
int LengthList(List &L)
{
	return L.size;
}
//判断List是否为空
bool EmptyList(List &L)
{
	return L.size == 0;
}
//返回List中第n个元素的值
ZhizhenType GetList(List &L, int n)
{
	if (n < 1 || n > L.size)
	{
		cerr << "没有第n个值" << endl;
	}
	return L.list[n - 1];
}
//遍历List中的所有元素并输出
void TraverseList(List &L)
{
	for (int i = 0; i < L.size; i++)
	{
		cout << L.list[i] << " ";
	}
	cout << endl;
}
//从List中查找某一个元素并确定是否存在
bool FindList(List &L, ZhizhenType number)
{
	for (int i = 0; i < L.size; i++)  //循环查找
	{
		if (L.list[i] == number)
		{
			number = L.list[i];
			return true;
		}
	}
	return false;
}
//修改List中的元素
bool UpdateList(List &L, const ZhizhenType number)
{
	for (int i = 0; i < L.size; i++)
	{
		if (L.list[i] == number)
		{
			L.list[i] = number; //更新list[i]的信息
			return true;
		}
	}
	return false;
}
//向L中插入元素
bool InsertList(List &L, ZhizhenType number, int position)
{
	//判断位置是否合法
	if (position < -1 || position>L.size + 1)
	{
		cout << "position不合法!" << endl;
		return false;
	}
	int i;  //循环变量  
	//判断插入的位置  
	if (position == 0)  //顺序插入  
	{
		for (i = 0; i<L.size; i++)
		{
			if (L.list[i]>number)
				break;
		}
		position = i + 1;
	}
	else if (position == -1)    //插入表尾  
	{
		position = L.size + 1;
	}
	//待插入位置的后续位置元素依次后移  
	for (i = L.size - 1; i >= position - 1; i--)
	{
		L.list[i + 1] = L.list[i];
	}
	//把number的值赋给已空出的下标为position-1的位置  
	L.list[position - 1] = number;
	L.size++;   //线性表的长度加1  
	return true;    //返回真,表示插入成功  
}
//从L中删除元素  
bool DeleteList(List &L, ZhizhenType number, int position)
{
	if (L.size == 0)   //检查线性表是否为空  
	{
		cout << "线性表为空,删除无效!" << endl;
		return false;
	}
	//判断pos的值是否合法  
	if (position<-1 || position>L.size + 1)
	{
		cout << "position的值不合法!" << endl;
		return false;
	}
	int i;  //循环变量  
	//判断删除的位置  
	if (position == 0)  //按值删除  
	{
		for (i = 0; i<L.size; i++)
		{
			if (L.list[i] == number)
				break;
		}
		if (i == L.size) return false; //无元素可删除  
		position = i + 1;
	}
	else if (position == -1)    //删除表尾元素  
	{
		position = L.size;
	}
	number = L.list[number - 1]; //将被删除元素的值赋给变参number带回  
	//待删除位置的后续位置元素依次前移  
	for (i = position; i<L.size; i++)
	{
		L.list[i - 1] = L.list[i];
	}
	L.size--;   //线性表的长度减1  
	return true;    //返回真,表示删除成功  
}


//对L中的所元素按给定条件进行排序  
void SortList(List &L)
{
	int i, j;
	ZhizhenType x;
	for (i = 1; i<L.size; i++)  //共循环n-1次  
	{
		x = L.list[i];    //把无序表中的第一个元素暂存x中  
		for (j = i - 1; j >= 0; j--)    //向前属性进行比较和移动  
		{
			if (x<L.list[j])
				L.list[j + 1] = L.list[j];
			else break;
		}
		L.list[j] = x;  //把x写入到已经空出的位置  
	}
}

//主函数  

int main()
{
	int a[12] = { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36 };
	int i;
	ZhizhenType x;
	List t;
	InitList(t);
	for (i = 0; i<12; i++)
	{
		InsertList(t, a[i], i + 1);
	}
	InsertList(t, 48, 13);
	InsertList(t, 40, 0);
	cout << GetList(t, 4) << ' ' << GetList(t, 9) << endl;
	TraverseList(t);
	cout << "输入待查找的元素值:";
	cin >> x;
	if (FindList(t, x)) cout << "查找成功!" << endl;
	else cout << "查找失败!" << endl;
	cout << "输入带删除元素的值:";
	cin >> x;
	if (DeleteList(t, x, 0)) cout << "删除成功!" << endl;
	else cout << "删除失败!" << endl;
	TraverseList(t);
	cout << "按值插入,输入待插入的元素的值:";
	cin >> x;
	if (InsertList(t, x, 0)) cout << "插入成功!" << endl;
	else cout << "插入失败!" << endl;
	TraverseList(t);
	cout << "线性表的长度:" << LengthList(t) << endl;
	if (EmptyList(t)) cout << "线性表为空!" << endl;
	else cout << "线性表不空!" << endl;
	ClearList(t);
	system("pause");
}
线性表的相关操作,大家可以结合注释看一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值