异质链表(C++实现)

list.h

#ifndef _LIST_H
#define _LIST_H

#include<string>
#include<iostream>

using namespace std;

class Base {
public:
	virtual ostream& printData(ostream&) = 0;
	friend ostream& operator << (ostream& out, Base* p);
};

template<class T>
class Node :public Base {
	T data;
public:
	Node(T x) :data(x) { };
	Node() :data(0) { };
	virtual ostream& printData(ostream& out) {
		out << data;
		return out;
	}
	virtual void set(T x) { data = x; }
	const T get() const { return data; }
	virtual ~Node() { };
};

class list {
	struct listNode {
		Base* num;
		listNode* next;
		listNode(Base* &x);
	}; //忘了写析构了……
	listNode* head;
	listNode* rear;
	listNode* find(int index)const;
	int size;
public:
	const int length()const;
	list();
	~list();
	void print()const;
	const listNode& get(int index)const;
	void push_back(Base* &x);
	void push_front(Base* &x);
	void push(Base* &x, int index);
	void del_back();
	void del_front();
	void del(int index);
	void reverse();
	void Union(const list& x);
	void operator =(const list& x);
};

typedef Node<int> Int;
typedef Node<double> Double;
typedef Node<char> Char;
typedef Node<string> String;

#endif

list.cpp

#include"list.h"
using namespace std;

list::listNode::listNode(Base* &x) :num(x) { }

list::list() : head(0), rear(0), size(0) {}

const int list::length()const
{
	return size;
}

void list::push_back(Base* &x)
{
	listNode* tmp = new listNode(x);
	tmp->next = 0;
	if (head == 0) {
		head = tmp;
		rear = head;
	}
	else {
		rear->next = tmp;
		rear = tmp;
	}
	size++;
}

void list::push_front(Base* &x)
{
	listNode* tmp = new listNode(x);
	if (head == 0) {
		head = tmp;
		rear = head;
	}
	else {
		tmp->next = head;
		head = tmp;
	}
	size++;
}

void list::push(Base* &x, int index)
{
	if (index == 1)push_front(x);
	else if (index == size + 1)push_back(x);
	else {
		listNode* q = new listNode(x);
		listNode* p = find(index - 1);
		q->next = p->next;
		p->next = q;
		size++;
	}
}

void list::del_front()
{
	if (size == 0)return;
	if (size == 1) {
		delete head;
		head = rear = 0;
		size--;
		return;
	}
	listNode *tmp = head;
	head = head->next;
	delete(tmp);
	size--;
	return;
}


void list::del(int index)
{
	if (index>size || index <= 0)return;
	if (index == 1 || size == 0 || size == 1)del_front();
	else {
		listNode *p = find(index - 1);
		listNode *q = p->next;
		p->next = q->next;
		q->next = 0;
		if (index == size)rear = p;
		delete q;
		size--;
	}
}

void list::del_back()
{
	del(size);
}

list::listNode* list::find(int index)const
{
	if (size == 0)return 0;
	int count = 1;
	listNode* p = head;
	while (p&&index != count) {
		p = p->next;
		count++;
	}
	return p;
}

void list::print()const
{
	listNode *p;
	p = head;
	while (p) {
		cout << p->num << " ";
		p = p->next;
	}
	cout << endl;
}

void list::reverse()
{
	if (size == 1)return;
	listNode *now = head->next;
	listNode *prev = head;
	rear = head;
	while (now) {
		listNode *nxt = now->next;
		now->next = prev;
		prev = now;
		now = nxt;
	}
	head = prev;
	rear->next = 0;
}

list::~list()
{
	listNode *p = head;
	while (p) {
		listNode *q = p;
		p = p->next;
		delete q;
	}
	head = rear = 0;
}

void list::Union(const list& x)
{
	if (size == 0)return;
	listNode* p = x.head;
	while (p) {
		push_back(p->num);
		p = p->next;
	}
}

void list::operator =(const list& x)
{
	if (this == &x)return;
	if (x.head == 0)return;
	listNode *p = x.head;
	while (p) {
		push_back(p->num);
		p = p->next;
	}
	return;
}

ostream& operator <<(ostream& out, Base* p)
{
	return p->printData(out);
}

main.cpp

#include"list.h"
#include<stdlib.h>
using namespace std; 

//测试 
class complex{
	int i;
	int r;
public:
	complex(int x):i(0),r(x) { };
	complex(int x,int y):i(y),r(x) { };
	friend ostream& operator <<(ostream& out,const complex& x);
};

ostream& operator <<(ostream& out,const complex& x){
	if(x.i==0&&x.r==0)out<<"0";
	else if(x.r==0)out<<x.i<<"i";
	else if(x.i==0)out<<x.r; 
	else if(x.i<0)out<<x.r<<"-"<<-x.i<<"i";
	else if(x.i==1)out<<x.r<<"+"<<"i";
	else out<<x.r<<"+"<<x.i<<"i";
	return out;
}

int main()
{
	list p,q,t;
	
	cout<<"从尾插入int:"<<endl; 
	for(int i=0;i<5;i++){
		Base *x=new Int(i);
		p.push_back(x);
		q.push_back(x);
	}
	p.print();
	cout<<"元素个数:"<<p.length()<<endl;
	
	cout<<endl<<"从头插入char:"<<endl; 
	for(char i='a';i<'d';i++){
		Base *x=new Char(i);
		p.push_front(x);
	} 
	p.print();
	cout<<"元素个数:"<<p.length()<<endl;
	
	cout<<endl<<"从中间插入double:"<<endl; 
	for(double i=0.1;i<=0.5;i+=0.1){
		static int cnt=4;
		Base *x=new Double(i);
		p.push(x,cnt++);
	} 
	p.print();
	cout<<"元素个数:"<<p.length()<<endl;

	cout<<endl<<"从中间插入string:"<<endl; 
    string a[3]={"well","are","you"};
	for(int i=0;i<3;i++){
		static int cnt=1;
		Base* x=new String(a[i]);
		p.push(x,cnt+=3);
	}
	p.print();
	cout<<"元素个数:"<<p.length()<<endl;
	
	cout<<endl<<"从末尾插入复数:"<<endl;
	
	complex com1(1,2),com2(5,-3);
	Base *x=new Node<complex>(com1);
	p.push_back(x); 
	x=new Node<complex>(com2);
	p.push_back(x);
	p.print();
	cout<<"元素个数:"<<p.length()<<endl;
	
	cout<<endl<<"链表反转:"<<endl; 
	p.reverse();
	p.print(); 
	cout<<"元素个数:"<<p.length()<<endl; 
	
	cout<<endl<<"删除末尾3个元素"<<endl; 
	for(int i=0;i<3;i++){
		p.del_back();
	}
	p.print();
	cout<<"元素个数:"<<p.length()<<endl;
	
	cout<<endl<<"删除开头3个元素"<<endl;
	for(int i=0;i<3;i++){
		p.del_front();
	} 
	p.print();
	cout<<"元素个数:"<<p.length()<<endl;
	
	
	cout<<endl<<"删除中间3个元素"<<endl;
	for(int i=0;i<3;i++){
	    int cnt=3;
		p.del(cnt);
	} 
	p.print();
	cout<<"元素个数:"<<p.length()<<endl;
	
	cout<<endl<<"链表合并"<<endl; 
	p.Union(q);
	p.print();
	cout<<"元素个数:"<<p.length()<<endl;
	
	cout<<endl<<"链表复制"<<endl; 
	t=p;	
	t.print();
	system("pause"); 
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值