顺序线性表的c++实现

//第一次发代码啊,新手而已
<span style="color:#ff0000;">//list.h</span>
#ifndef LIST_H
#define LIST_H
class List
{
public:
	List();//创建最大长度为100的线性表
	List(int size);//创建最大长度为size的线性表'
	~List();//删除线性表
	void CreatList(int length);//向线性表输入length个元素
	void GetList()const;//查看线性表中的元素
	bool ListEmpty();//检查线性表是否为空
	int ListLength()const;//返回线性表的长度
	int GetElem(int i, int &e)const;//返回下标为i的元素
	int LocateElem(int e);//检查是否有与e相同的元素,如有则返回下标,若无则返回-1
	int PriorElem(int cur_e, int &pre_e)const;//返回cur_e的前一个元素
	int NextElem(int cur_e, int &next_e)const;//返回cur_e的后一个元素
	void ListInsert(int index,int e);//在下标为index的元素前插入e
	int LiseDelete(int index,int &e);//删除下标为index的元素并返回该元素
	void MergeList(List la, List lb);//使用前需创建一个listsize=la.length+lb.length的lc

private:
	int *p;//数组
	int length;//当前长度
	int listsize;//最大长度
};
#endif<pre class="cpp" name="code"> 
 
 
 
 
 
 
 
 
<span style="color:#ff0000;">//主函数
</span>#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include"List.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	List la(2);
	List lb(3);
	int a;
	la.ListInsert(-1, 1);
	la.CreatList(2);
	lb.CreatList(3);
	List lc(6);
	lc.MergeList(la, lb);
	int e;
	lc.GetElem(0,e);
	la.ListInsert(1, 2);
	la.GetList();
	lb.GetList();
	lc.GetList();
	cout << setw(3)<<e<<endl;
	la.~List();
	lb.~List();
	lc.~List();
	system("pause");
	return 0;
}
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;">//list.cpp
</span>#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<cassert>
#include"List.h"
using  namespace std;
List::List()
{
	p = new int[100];
	for (int i = 0; i < 100; i++)
		p[i] = 'null';
	length = 0;
	listsize = 100;
}
List::List(int a)
{
	p = new int[a];
	for (int i = 0; i < a; i++)
		p[i] = 'null';
	length = 0;
	listsize = a;
}
List::~List()
{
}
void List::CreatList(int length)
{
	assert(length <= listsize);
	this->length = length;
	cout << "请从小到大输入"<<endl;
	for (int i = 0; i < length; i++)
	{
		cout << "请输入第" << i << "个元素:";
		cin >> p[i];
	}
}
void List::GetList() const
{
	for (int i = 0; i < length; i++)
		cout << setw(3) << p[i];
	cout << endl;
}
bool List::ListEmpty()
{
	if (p[0] == 'null')
		return true;
	else
		return false;
}
int List::ListLength() const
{
	return length;
}
int List::GetElem(int i, int &e) const
{
	e = p[i];
	return e;
}
int List::LocateElem(int e)
{
	for (int i = 0; i < length; i++)
	{
		if (p[i] == e)
			return i;
	}
	return -1;
}
int List::PriorElem(int cur_e, int &pre_e) const
{
	assert(p[0] == cur_e);
	for (int i = 1; i < length; i++)
	{
		if (p[i] == cur_e)
		{
			pre_e = p[i - 1];
			return pre_e;
		}
	}
}
int List::NextElem(int cur_e, int &next_e) const
{
	assert(p[length - 1] == cur_e);
	for (int i = 0; i < length - 1; i++)
	{
		if (p[i] == cur_e)
		{
			next_e = p[i + 1];
			return next_e;
		}
	}
}
void List::ListInsert(int index, int e)
{
	if (index > length || index<0)
	{
		cout << "下标不在线性表有效范围之内" << endl;
		cout << "有效下标为:" << 0 << "到" << length << endl;
		return;
	}
	if (length == listsize)
	{
		int *q = new int(listsize + 1);
		for (int i = 0; i<length; i++)
			q[i] = p[i];
		p = q;
		listsize += 1;
	}
	for (int i = length - 1; i >=index; i--)
		*(p + i+1)=*(p+i);
	*(p + index) = e;
	length += 1;
}
int List::LiseDelete(int index, int &e)
{
	assert(index >= length&&index < 0);
	e = *(p + index);
	for (int i = index; i < length; i++)
		*(p + i) = *(p + i + 1);
	length -= 1;
	return e;
}
void List::MergeList(List la, List lb)
{
	int i = 0, j = 0;
	int k;
	for (k=0; i < la.length&&j < lb.length;k++)
	{
		if (la.p[i] <= lb.p[j])
		{
			p[k] = la.p[i];
			i++;
		}
		else
		{
			p[k] = lb.p[j];
			j++;
		}
	}
	while (i < la.length)
	{
		p[k] = la.p[i];
		i++;
		k++;
	}
	while (j < lb.length)
	{
		p[k] = lb.p[j];
		j++;
		k++;
	}
	length = la.length + lb.length;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值