面向对象第二 \ 三次实验

#include <iostream>
using namespace std;

typedef struct lnode
{
	int data;
	struct lnode* nxt;
}lnode, * linklist;

class list
{
public:
	list();
	~list();
	list(const list& li);
	void init(int n);
	bool insert(int index, int elem);
	bool Delete(int elem);
	bool remove(int elem);
	void print();
	void clean();
	bool empty();
	int size();
	list operator=(const list& L);
	friend ostream& operator<<(ostream& output, list L);
	friend istream& operator>>(istream& input, list& L);

private:
	linklist l;
	int lenght;
};

list::list()
{
	l = new lnode;
	l->nxt = NULL;
	lenght = 0;
}

list::list(const list& L)
{
	l = new lnode;
	l->nxt = NULL;
	lenght = 0;
	linklist r = l, t;
	linklist p;
	p = new lnode;
	p = L.l->nxt;
	while (p != NULL)
	{
		t = new lnode;
		t->data = p->data;
		t->nxt = NULL;
		r->nxt = t;
		r = t;
		p = p->nxt;
		++lenght;
	}
	r->nxt = NULL;
}

list::~list()
{
	clean();
}

void list::init(int n)
{
	linklist r = l;

	for (int i = 0; i < n; i++)
	{
		linklist p = new lnode;
		int temp;
		cin >> temp;
		p->nxt = NULL;
		p->data = temp;
		r->nxt = p;
		r = p;
	}
	r->nxt = NULL;
	lenght = n;
}

bool list::insert(int index, int elem)
{
	int cnt = 1;
	linklist p = l;
	while (cnt < index)
		p = p->nxt, cnt++;
	linklist r = new lnode;
	r->data = elem;
	r->nxt = p->nxt;
	p->nxt = r;
	++lenght;
	return true;
}

bool list::Delete(int elem)
{
	linklist p = new lnode;
	p = l;
	while (p->nxt && p->nxt->data != elem)
		p = p->nxt;
	if (p->nxt)
	{
		linklist t = new lnode;
		t = p->nxt;
		p->nxt = p->nxt->nxt;
		delete t;
		--lenght;
		return true;
	}
	return false;
}

bool list::remove(int index)
{
	if (index > lenght)
	{
		cout << "字符串中无此元素" << endl;
		return false;
	}
	linklist p = l;
	int cnt = 1;
	while (cnt++ < index)
		p = p->nxt;
	linklist t = p->nxt;
	p->nxt = t->nxt;
	delete t;
	return true;
}

void list::print()
{
	linklist p = l->nxt;
	while (p)
	{
		cout << p->data << " ";
		p = p->nxt;
	}
	cout << endl;
}

void list::clean()
{
	if (lenght == 0)
		return;
	linklist p = l;
	linklist t;
	while (p)
	{
		t = p;
		p = p->nxt;
		delete t;
	}

	lenght = 0;
}

bool list::empty()
{
	if (!lenght)
		return true;
	return false;
}

int list::size()
{
	return lenght;
}

list list::operator=(const list& L)
{
	list temp;
	linklist r = temp.l, t;
	linklist p;
	p = new lnode;
	p = L.l->nxt;
	while (p != NULL)
	{
		t = new lnode;
		t->data = p->data;
		t->nxt = NULL;
		r->nxt = t;
		r = t;
		p = p->nxt;
		++temp.lenght;
	}
	r->nxt = NULL;
	return temp;
}

ostream& operator<<(ostream& output, list L)
{
	linklist p = L.l->nxt;
	while (p)
	{
		output << p->data << " ";
		p = p->nxt;
	}
	output << endl;
	return output;
}

istream& operator>>(istream& input, list& L)
{
	int n;
	linklist r = L.l;
	cout << "链表长度 : " << endl;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		linklist p = new lnode;
		int temp;
		input >> temp;
		p->nxt = NULL;
		p->data = temp;
		r->nxt = p;
		r = p;
	}
	r->nxt = NULL;
	L.lenght = n;
	return input;
}

int  main()
{
	list a;
	//重载流输入运算符
	cin >> a;
	//重载赋值
	list b = a;
	//重载流输出运算符
	cout << b << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值