我的大学之数据结构NO.1

顺序表基本操作以及有序表的合并
一、需求分析
1.实现顺序表的各种基本操作,包括创建顺序表、插入和删除指定序号的元素、读取表元、获取最大和最小值元素、查找元素、表元素的排序、表元素逆置、顺序表的输入和输出。
2.实现两个有序顺序表的合并。
问题描述:创建两个有序的顺序表L1和L2,表中元素值由键盘随机输入,再将它们合并为一个新的顺序表L3,合并后L3仍然有序(重复元素只保留一个),最后输出顺序表中的各个元素值。

二、部分代码
1.定义顺序表

typedef int ElemType;
typedef struct {
	ElemType *elem; //存储空间基址
	int length; //当前长度
	int listsize; //允许的最大存储容量
}SqList; 

2.顺序表初始化

void InitList(SqList &L)
{
	int j;
	int maxsize;
	cout << "请输入顺序表的最大长度: ";
	cin >> maxsize;
	if (maxsize == 0)
		maxsize = MAXLISTSIZE;
	L.elem = new ElemType[maxsize];
	if (!L.elem) exit(1); //储存分配失败
	cout << "请输入顺序表的长度: ";
	cin >> L.length;
	L.listsize = maxsize; //该顺序表可以存储元素的最大容量
	cout << "请输入顺序表的元素: ";
	for (j = 0; j < L.length; j++)
	{
		cin >> L.elem[j];
	}
	system("pause"); //任意键继续
}

3.在顺序表L中查找第一个值与e相等的元素,若存在输出该元素的位置,若不存在,输出所找的元素不存在

void LocateElem(SqList L)
{
	ElemType e;
	int j;
	j = 0; 
	cout << "请输入要查找的元素:";
	cin >> e;
	while (j < L.length && L.elem[j] != e)

		++j; 
	if (j < L.length) cout << "所找元素的位置为:" << j + 1; //找到满足判定条件的数据元素为第j个元素
	else cout << "所找的元素不存在"; //该线性表中不存在满足判定的数据元素
	cout << endl;
	system("pause");
}

4.插入函数

void ListInsert(SqList &L)
{
	int j;
	int pos;
	ElemType e;
	cout << "请输入所要插入的位置及元素:";
	cin >> pos >> e;
	if (pos < 1 || pos > L.length + 1)
	{
		cout << "插入位置不合法" << endl;
		system("pause");
		return;
	}
	if (L.length >= L.listsize)
	{
		cout << "当前存储空间已满,无法插入,请重新选择顺序表功能" << endl;
		system("pause");
		return;
	}
	for (j = L.length - 1; j >= pos - 1; --j)
		L.elem[j + 1] = L.elem[j]; //插入位置之后的元素右移
	L.elem[pos - 1] = e; //插入e
	++L.length; //表长增1
	cout << "插入后的顺序表:";
	for (j = 0; j < L.length; j++)
	{
		cout << L.elem[j] << ' ';
	}
	cout << endl;
	system("pause");
}

5.删除函数

void ListDelete(SqList &L)
{
	int j;
	int pos;
	cout << "请输入所要删除元素的位置: ";
	cin >> pos;
	if (pos < 1 || pos > L.length)
	{
		cout << "删除位置不合法"<<endl;
		system("pause");
		return;
	}
	for (j = pos; j < L.length; ++j)
		L.elem[j - 1] = L.elem[j]; ///被删除元素之后的元素左移
	--L.length; //表长减1
	cout << "删除后的顺序表:";
	for (j = 0; j < L.length; j++)
	{
		cout << L.elem[j] << ' ';
	}
	cout << endl;
	system("pause");
}

6.释放函数

void DestroyList(SqList &L)
{
	delete[] L.elem;
	L.listsize = 0;
	L.length = 0;
	cout << "顺序表已销毁" << endl;
	system("pause");
}

void ListLength(SqList L)//输出表长
{
	cout << "表长为:" << L.length << endl;
	system("pause");
}

void ListEmpty(SqList L)//判空函数
{
	if (L.length == 0)
		cout << "该顺序表为空" << endl;
	else
		cout << "该顺序表不为空" << endl;
	system("pause");
}

7.取出函数

void GetElem(SqList L)
{
	int j;
	cout << "取出顺序表元素的位置:";
	cin >> j;
	if (j < 1 || j > L.length)
	{
		cout << "取出位置不合法" << endl;
		system("pause");
		return;
	}
	cout << "取出的元素为:" << L.elem[j - 1] << endl;
	system("pause");
}

输入有序顺序表L1,L2.归并为L3.输出L3

void MergeList_Sq()
{
	SqList L1; SqList L2; SqList L3;
	int i, j, k; int maxsize;
	maxsize = MAXLISTSIZE;
	L1.elem = new ElemType[maxsize];
	L2.elem = new ElemType[maxsize];
	L3.elem = new ElemType[maxsize];
	if (!L1.elem || !L2.elem || !L3.elem) exit(1); //储存分配失败
	cout << "请输入顺序表La的长度: ";
	cin >> L1.length;
	L1.listsize = maxsize; 
	cout << "请输入顺序表的元素: ";
	for (j = 0; j < L1.length; j++)
	{
		cin >> L1.elem[j];
	}
	cout << "请输入顺序表Lb的长度: ";
	cin >> L2.length;
	L2.listsize = maxsize; 
	cout << "请输入顺序表的元素: ";
	for (j = 0; j < L2.length; j++)
	{
		cin >> L2.elem[j];
	}
	L3.listsize = L3.length = L1.length + L2.length;

	for (i = 0, j = 0, k = 0; i < L1.length && j < L2.length; k++)
		if (L1.elem[i] <= L2.elem[j])
		{
			L3.elem[k] = L1.elem[i];
			i++;
		}
		else
		{
			L3.elem[k] = L2.elem[j];
			j++;
		}
	while (i < L1.length)
	{
		L3.elem[k] = L1.elem[i];
		i++, k++;
	} //插入La的剩余元素
	while (j < L2.length)
	{
		L3.elem[k] = L2.elem[j];
		j++, k++;
	} //插入Lb的剩余元素
	int t;
	for(j=0; j<L3.length-1; j++)
	{
		for(i=0;i<L3.length-j-1; i++)   //冒泡法排序
                if(L3.elem[i] > L3.elem[i+1])
                { t = L3.elem[i]; L3.elem[i] = L3.elem[i+1]; L3.elem[i+1] = t; }
	}
	for (j = 0; j < L3.length-1; j++)
	{
		if(L3.elem[j]==L3.elem[j+1])
		{for (; j< L3.length-1; j++)
			{L3.elem[j] = L3.elem[j+1]; ///被删除元素之后的元素左移
			}
		}
		--L3.length; //表长减1
	}
	cout << "合并后的顺序表Lc为:";
	for (j = 0; j < L3.length; j++)
	{
		cout << L3.elem[j] << ' ';
	}
	cout << endl;
	system("pause");
}

9.顺序表逆置

void SListRev(SqList &L)
{
	int i,t;
    for(i=0; i<L.length/2; i++)
        {
            t = L.elem[i]; L.elem[i] = L.elem[L.length-i-1]; L.elem[L.length-i-1] = t;
        }
	for (i = 0; i < L.length; i++)
	{
		cout << L.elem[i] << ' ';
	}
	cout << endl;
    system("pause");
}

10.最大最小值函数

void MaxminList(SqList L)
{
	int i,max,min;
	max=min=L.elem[0];
	for(i=0; i<L.length-1; i++)
	{
		if(L.elem[i]<L.elem[i+1]) 
		{
			max=L.elem[i+1];
			min=L.elem[i];
		}
	}
	cout<<"最大值: "<<max<<endl;
	cout<<"最小值: "<<min<<endl;
	system("pause");
}

11.排序函数

void SortList(SqList &L)
{
    int i,j,t;
    for(j=0; j<L.length-1; j++)
	{
		for(i=0;i<L.length-j-1; i++)   //冒泡法排序
                if(L.elem[i] > L.elem[i+1])
                { t = L.elem[i]; L.elem[i] = L.elem[i+1]; L.elem[i+1] = t; }
	}
	for (i = 0; i < L.length; i++)
	{
		cout << L.elem[i] << " ";
	}
	cout << endl;
    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值