c14

1.链表、栈、队列实现:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

template <typename T>
class list
{
	struct Node
	{
		T data;
		Node *next;
		Node(const T& t = T()):data(t),next(NULL){}
	};
	Node *head;
public:
	list():head(NULL){}
	void clear()
	{
		while (head)
		{
			Node *p = head->next;
			delete head;
			head = p;
		}
	}
	~list()
	{
		clear();
	}
	void insert_front(const T& t)
	{
		Node *p = new Node(t);
		p->next = head;
		head = p;
	}
	void insert_back(const T& t)
	{
		Node* p = new Node(t);
		if (!head)
			head = p;
		else
			getPointer(size() - 1)->next = p;
	}
	void travel()
	{
		Node *p = head;
		while (p)
		{
			cout<<p->data<<' ';
			p = p->next;
		}
		cout<<endl;
	}
	int size()
	{
		int cnt = 0;
		Node *p = head;
		while (p)
		{
			++cnt;
			p = p->next;
		}
		return cnt;
	}
	T getHead()
	{
		if (!head)
			throw "no head";
		return head->data;
	}
	T getTail()
	{
		if (!head)
			throw "no tail";
		Node* p = head;
		while (p->next)
			p = p->next;
		return p->data;
	}
	bool empty()
	{
		return head == NULL;
	}
	int find(const T& t)
	{
		int pos = 0;
		Node* p = head;
		while (p)
		{
			if (p->data == t)
				return pos;
			p = p->next;
			++pos;
		}
		return -1;
	}
	bool update(const T& o, const T& n)
	{
		int pos = find(o);
		if (pos == -1)
			return false;
		getPointer(pos)->data = n;
		return true;
	}
	bool erase(const T& t)
	{
		int pos = find(t);
		if (pos == -1)
			return false;
		if (pos == 0)
		{
			Node *p = head->next;
			delete head;
			head = p;
		}
		else
		{
			Node *p = getPointer(pos - 1);
			Node *q = p->next;
			p->next = q->next;
			delete q;
			q = NULL;
		}
	}
private:
	Node* getPointer(int pos)
	{
		Node *p = head;
		for (int i = 0; i < pos; ++i)
			p = p->next;
		return p;
	}
};

template <typename T>
class Stack
{
	list<T> l;
public:
	void push(const T&t)
	{
		l.insert_front(t);
	}
	void pop()
	{
		l.erase(l.getHead());
	}
	T top()
	{
		return l.getHead();
	}
	bool empty()
	{
		return l.empty();
	}
	int size()
	{
		return l.size();
	}
	void clear()
	{
		l.clear();
	}
};

template <typename T>
class Queue
{
	list<T> l;
public:
	void push(const T& t)
	{
		l.insert_back(t);
	}
	void pop()
	{
		l.erase(l.getHead());
	}
	T front()
	{
		return l.getHead();
	}
	T back()
	{
		return l.getTail();
	}
	bool empty()
	{
		return l.empty();
	}
	int size()
	{
		return l.size();
	}
	void clear()
	{
		l.clear();
	}
};

int main()
{
	Stack<string> s;
	s.push("good");
	s.push("morning");
	s.push("everyone");

	while (!s.empty())
	{
		cout<<s.top()<<' ';
		s.pop();
	}
	cout<<endl;

	Queue<double> d;
	d.push(1.0);
	d.push(2.0);
	d.push(3.0);
	d.push(4.0);
	d.push(5.0);
	cout.setf(ios::showpoint);
	while (!d.empty())
	{
		cout<<d.front()<<' ';
		d.pop();
	}
	cout<<endl;
	system("pause");
	return 0;
}

2.try{ throw 数据; } catch(子类 e){} catch(父类 e){} catch(...){}逐层上抛

3.数组实现stack:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

template <typename T>
class Stack
{
	T a[10];
	int n;
public:
	void push(const T&t)
	{
		if (full())
			throw "overflow";
		a[n++] = t;
	}
	void pop()
	{
		if (empty())
		{
			throw "empty stack";
		}
		--n;
	}
	T top()
	{
		if (empty())
		{
			throw "no top";
		}
		return a[n-1];
	}
	bool empty()
	{
		return n == 0;
	}
	bool full()
	{
		return n == 10;
	}
	int size()
	{
		return n;
	}
	int capacity()
	{
		return 10;
	}
	void clear()
	{
		n = 0;
	}
	Stack():n(0){}
};

int main()
{
	Stack<string> s;
	s.push("good");
	s.push("morning");
	s.push("everyone");

	while (!s.empty())
	{
		cout<<s.top()<<' ';
		s.pop();
	}
	cout<<endl;
	system("pause");
	return 0;
}

4.throw "hello" catch(const char* e){}

5.二叉树:BST: binary searh/sort tree

二叉树实现


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值