链式线性表的c++实现

<span style="color:#ff0000;">//FILE: List.h
</span>//CLASS PROVIDED: List(线性表的链式储存结构)
//
//CONSTRUCTOR for the List class:
//List();
//后置条件:建立头结点
//
//void GetElem_L(int i, int &e);
//前置条件:下标i在长度范围之内
//后置条件:若下标i在长度范围之内,则将下标i对应的元素赋值给e,否则返回
//
//void ListInsert_L(int i,int e);
//前置条件:下标i在长度范围之内
//后置条件:若下标i在长度范围之内,则将元素e插在第i个位置之前,否则返回
//
//void ListDelete(int i,int &e);
//前置条件:下标i在长度范围之内
//后置条件:若下标i在长度范围之内,则删除第i个元素,并由e返回其值,否则直接返回
//
//void CreatList_L(int n);
//后置条件:逆序输入n个元素的值,建立单链表
//
//void MergeList_L(List &la,List &lb,List &lc);
//前置条件:两个链表la,lb且均为从小到大排列
//后置条件:将两个链表la,lb的元素合并懂到lc中
//
//void PriorElem_L(int i,int &pri_e);
//前置条件:第i个元素之前存在元素
//后置条件:用pri_e返回第i个元素之前的元素
//
//void NextElem_L(int i,int &next_e);
//前置条件:第i歌元素之后存在元素
//后置条件:用next_e返回第i歌元素之后的元素
//
//void ListEmpty_L();
//后置条件:清空链表
//
//~List();
//后置条件:删除链表
#ifndef LIST_H
#define LIST_H
typedef int Elemtype;
struct Node
{
	Elemtype data;
	Node *next;
};
class List
{
public:
	List();
	void GetElem_L(int i, Elemtype &e);
	void OutputList();
	void ListInsert_L(int i, Elemtype e);
	void ListDelete(int i, Elemtype &e);
	void CreatList_L(int n);
	void MergeList_L(List &la, List &lb,List &lc);
	void PriorElem_L(int i, Elemtype &pri_e);
	void NextElem_L(int i, Elemtype &next_e);
	void ListEmpty_L();
	~List();
	Node *Lnode;
};
#endif
<span style="color:#ff0000;">//主程序
</span>#include"List.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	List list1;
	int e;
	list1.CreatList_L(5);
	list1.OutputList();
	list1.GetElem_L(6,e);
	list1.ListDelete(2, e);
	cout << e;
	list1.ListEmpty_L();
	system("pause");
	return 0;
}

<span style="color:#ff0000;">//list.cpp
</span>#include "List.h"
#include<iostream>
using namespace std;
List::List()
{
	Lnode = new Node;
	Lnode->next = NULL;
}
void List::CreatList_L(int n)
{
	for (int i = 0; i < n; i++)
	{
		Node *p = new Node;
		cin >> p->data;
		p->next = Lnode->next;
		Lnode->next = p;
	}
}
void List::OutputList()
{
	Node *p = new Node;
	p = Lnode->next;
	while (p)
	{
		cout << p->data;
		p = p->next;
	}
	cout << endl;
}
void List::GetElem_L(int i, int &e)
{
	Node *p = Lnode->next;
	size_t j = 1;
	while (p&&j < i)
	{
		p = p->next;
		j++;
	}
	if (!p || j > i)
		return;
	else
		e = p->data;
}
void List::ListInsert_L(int i, Elemtype e)
{
	Node *p = new Node;
	p = Lnode;
	int j = 0;
	while (p&&j < i - 1)
	{
		p = p->next;
		j++;
	}
	if (!p || j > i - 1)
		return;
	else
	{
		Node *s = new Node;
		s->data = e;
		s->next = p->next;
		p->next = s;
	}
}
void List::ListDelete(int i, int &e)
{
	Node *p = new Node;
	p = Lnode;
	int j = 0;
	while (p&&j < i-1)
	{
		p = p->next;
		j++;
	}
	if (!p->next || j > i-1)
		return;
	Node *q = p->next;
	p->next = q->next;
	e = q->data;
	delete q;
}
void MergeList_L(List &la, List &lb, List &lc)
{
	Node *pa = new Node;
	Node *pb = new Node;
	Node *pc = new Node;
	pa = la.Lnode->next;
	pb = lb.Lnode->next;
	pc = lc.Lnode;
	lc.Lnode = la.Lnode;
	while (pa&&pb)
	{
		if (pa->data <= pb->data)
		{
			pc->next = pa;
			pc = pa;
			pa = pa->next;
		}
		else
		{
			pc->next = pb;
			pc = pb;
			pb = pb->next;
		}
	}
	pc->next = pa ? pa : pb;
	delete pb;
}
void List::PriorElem_L(int i, int &pri_e)
{
	Node *p = new Node;
	p = Lnode;
	int j = 1;
	while (p&&j < i)
	{
		p = p->next;
		j++;
	}
	if (!p->next || j >= i)
		return;
	pri_e = p->data;
}
void List::NextElem_L(int i, int &next_e)
{
	Node *p = new Node;
	p = Lnode;
	int j = 0;
	while(p&&j < i)
	{
		p = p->next;
		j++;
	}
	if (!p->next&&j >= i)
		return;
	next_e = p->next->data;
}
void List::ListEmpty_L()
{
	Node *p = new Node;
	p = Lnode;
	while (p->next)
	{
		Node *q = new Node;
		q = p->next;
		p->next = q->next;
		delete q;
	}
}
List::~List()
{
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值