链接表 List

#ifndef SJTU_LIST_HPP
#define SJTU_LIST_HPP

#include <cstddef>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <climits>
#include <cstddef>

namespace sjtu {
class exception
	{
protected:
		const std::string variant = "";
		std::string detail = "";
public:
		exception() {}
		exception(const exception &ec) : variant(ec.variant), detail(ec.detail) {}
		virtual std::string what() {
			return variant + " " + detail;
		}
	};
class index_out_of_bound : public exception {};
class runtime_error : public exception {};
class invalid_iterator : public exception {};
class container_is_empty : public exception {};

template<typename T>
class list {
private:
	struct node
	{
		node* pre;
		node* next;
		T* data;
		node()
		{
			next = NULL;
			pre = NULL;
			data = NULL;
		}
		~node()
		{
			delete data;
		}
	};
	node* head;
	node* tail;
	int currentsize;
public:
	list()
	{
		currentsize = 0;
		head = new node;
		tail = new node;
		head->next = tail;
		tail->pre = head;
		head->pre = NULL;
		tail->next = NULL;

	}
	list(const list<T> &other)
	{
		currentsize = 0;
		head = new node;
		tail = new node;
		head->next = tail;
		tail->pre = head;
		head->pre = NULL;
		tail->next = NULL;
		node* tmp = head;
		node* tmp2 = other.head;
		for(int i = 1 ; i <= other.currentsize;i++)
		{
			tmp->next=new node;
			tmp->next->pre=tmp;
			tmp=tmp->next;
			tmp2=tmp2->next;
			tmp->data = new T(*(tmp2->data));
			currentsize++;
		}
		tmp->next = tail;
		tail->pre = tmp;
	}
	~list()
	{
		for(int i = currentsize;i>=1;i--)
		{
			node* p = tail->pre;
			tail->pre=tail->pre->pre;
			delete p;
		}
		 delete head;
		 delete tail;
	}
	class iterator;
	class const_iterator;
	class iterator {
	friend class list;
	private:
		node* p;
		list<T>* thelist;
	public:
		iterator(const list<T>* ptrlist, node* ptrnode)
		{
			thelist = (list<T>*)ptrlist;
			p=ptrnode;
		}
		iterator operator++(int)
		{
			iterator tmp = *this;
			p=p->next;
			return tmp;
		}
		iterator& operator++()
		{
			p=p->next;
			return *this;
		}
		iterator operator--(int)
		{
			iterator tmp = *this;
			p=p->pre;
			return tmp;
		}
		iterator& operator--()
		{
			p=p->pre;
			return *this;
		}
		T& operator*() const
		{
			return *(p->data);
		}
		bool operator==(const iterator &rhs) const
		{
			return p==rhs.p;
		}
		bool operator==(const const_iterator &rhs) const
		{
			return p==rhs.p;
		}
		bool operator!=(const iterator &rhs) const
		{
			return p != rhs.p;
		}
		bool operator!=(const const_iterator &rhs) const
		{
			return p != rhs.p;
		}
		T* operator->()
		{
			return p->data;
		}
	};
	class const_iterator {
	friend class list;
	private:
		node* p;
		list<T>* thelist;
	public:
		const_iterator(const list<T>* ptrlist, node* ptrnode)
		{
			thelist = (list<T>*)ptrlist;
			p = ptrnode;
		}
		const_iterator operator++(int)
		{
			iterator tmp = *this;
			p=p->next;
			return tmp;
		}
		const_iterator& operator++()
		{
			p=p->next;
			return *this;
		}
		const_iterator operator--(int)
		{
			const_iterator tmp = *this;
			p=p->pre;
			return tmp;
		}
		const_iterator& operator--()
		{
			p=p->pre;
			return *this;
		}
		T& operator*() const
		{
			return *(p->data);
		}
		bool operator==(const iterator &rhs) const
		{
			return p==rhs.p;
		}
		bool operator==(const const_iterator &rhs) const
		{
			return p==rhs.p;
		}
		bool operator!=(const iterator &rhs) const
		{
			return p != rhs.p;
		}
		bool operator!=(const const_iterator &rhs) const
		{
			return p != rhs.p;
		}
		T* operator->()
		{
			return p->data;
		}
	};
	void clear()
	{
		for (int i = currentsize;i >= 1;i--)
		{
			node* p = tail->pre;
			tail->pre = tail->pre->pre;
			delete p;
		}
		currentsize = 0;
	}
	list<T> & operator=(const list<T> &other)
	{
		if (&other == this) return *this;
		clear();
		node* p1 = head;
		node* p2 = other.head->next;
		for(int i = 1 ; i <= other.currentsize ; i ++)
		{
			p1->next = new node;
			p1->next->pre = p1;
			p1 = p1->next;
			p1->data = new T(*(p2->data));
			p2 = p2->next;
		}
		tail->pre = p1;
		p1->next = tail;
		currentsize = other.currentsize;
		return *this;
	}
	const T & front() const
	{
		return *(head->next->data);
	}
	const T & back() const
	{
		return *(tail->pre->data);
	}
	iterator begin()
	{
		iterator tmp(this,head->next);
		return tmp;
	}
	const_iterator cbegin() const
	{
		const_iterator tmp(this,head->next);
		return tmp;
	}
	iterator end()
	{
		iterator tmp(this,tail);
		return tmp;
	}
	const_iterator cend() const
	{
		const_iterator tmp(this,tail);
		return tmp;
	}
	bool empty() const
	{
		return currentsize == 0;
	}
	size_t size() const
	{
		return (size_t)currentsize;
	}
	iterator insert(iterator pos, const T &value)
	{
		if (pos.thelist != this) throw invalid_iterator();
		node* tmp = pos.getp();
		tmp->pre->next = new node;
		*(tmp->pre->next->data)=value;
		tmp->pre->next->pre=tmp->pre;
		tmp->pre->next->next = tmp;
		tmp->pre = tmp->pre->next;
		tmp=tmp->pre;
		currentsize++;
		return pos;
	}
	iterator erase(iterator pos)
	{
		if (pos.thelist != this) throw invalid_iterator();
		currentsize--;
		iterator tmp(this,pos.p->next);
		node* p = pos.p;
		if(p==head)
		{
			return tmp;
		}
		if(p==tail)
		{
			return tmp;
		}
		p->pre->next = p->next;
		p->next->pre = p->pre;
		delete p;

		return tmp;
	}
	iterator erase(iterator first, iterator last)
	{
		if (first.thelist != this ||last.thelist != this)
			throw invalid_iterator();
		do
		{
			if (first.p == tail) throw invalid_iterator();
			first=erase(first);
		} while (first != last);
		return first;
	}
	void push_back(const T &value)
	{
		node* p = new node;
		p->data = new T(value);
		p->pre = tail->pre;
		tail->pre->next = p;
		tail->pre = p;
		p->next = tail;
		currentsize++;
	}
	void pop_back()
	{
		node* p = tail->pre;
		tail->pre=tail->pre->pre;
		tail->pre->next = tail;
		delete p;
		currentsize--;
	}
	void push_front(const T &value)
	{
		node* p0 = head->next;
		head->next = new node;
		head->next->data = new T(value);
		head->next->next = p0;
		p0->pre = head->next;
		head->next->pre = head;
		currentsize++;
	}
	void pop_front()
	{
		node* p0 = head->next->next;
		node* p1 = head->next;
		delete p1;
		head->next = p0;
		p0->pre = head;
		currentsize--;
	}
};

}

#endif

  

时间: 2024-10-05 20:55:10

链接表 List的相关文章

C++培训:C语言链接表分析

C++培训之前有同学再问C语言链接表怎么破?今天小编把这个知识点给大家分享出来,有疑问的可以多看看! 相信学了c语言的人对链表或多或少有了解,链表也是数据结构的重要内容,今天就来聊聊最简单的单向动态链表的建立与输出.首先要了解什么是链表,链表是程序设计中一种重要的动态数据结构,是动态地进行存储分配的一种结构.其中动态主要表现在元素位置可以变化,即随意删除随意插入等;元素个数可增可减,不像数组声明后长度就固定不变了.这就想起前段时间有人在群里问怎么删除素组中任意一个元素,如果没有链表刚开始学感觉就

3、链接表的实现:双向链表

链接表ADT: 1 package ren.laughing.datastructure.base; 2 3 4 import ren.laughing.datastructure.exception.InvalidNodeException; 5 import ren.laughing.datastructure.exception.OutOfBoundaryException; 6 /** 7 * 链接表ADT 8 * 单链表和双链表用顺序存储结构并不友好 9 * 链表方便插入,不方便查找

该 ISAM 不支持在链接表中删除数据。

使用OleDB方式操作Excel,删除表中的数据时提示该错误 相关代码: 连接字符串: //定义OleDB连接字符串 string strConn = "Provider=Microsoft.Ace.OleDb.12.0;Persist Security Info=False;" + "data source=" + @excelPath + ";Extended Properties='Excel 12.0; HDR=yes; IMEX=10'"

第二章 线性表2(链接表)

3.5.2 链接表接口 链接表可以看成是一组结点序列以及基于结点进行操作的线性结果的抽象,或则说是对链表的抽象. 链接表的接口: 1 package com.datastructure.chapter03.interfaces; 2 3 import com.datastructure.chapter03.exception.InvalidNodeException; 4 import com.datastructure.chapter03.exception.OutOfBoundaryExce

(二)单链接表的实现之从尾部插入节点

(二)单链接表的实现之从尾部插入节点 从尾部插入节点构建一个简单的列表什么是头节点 从尾部插入节点 从尾部插入节点??如上图所示,只要将创建的节点的pNext指针指向下一节点地址即可:pHeader->pNext = New;??从尾部插入节点分成两个步骤: 找到链表的最后一个节点: 将新的节点和原来的最后一个节点链接起来. 1/* 2 *pH : 表示链表的头指针 3 *new: 新节点的首地址 4 */ 5void insert_tail(struct node *pH,strtuct no

Access使用链接表写入冲突问题

一般来说该方法多数表现为Access链接表使用了(是/否)作为字段值,如图1所示该值在Access表现为-1/0,而Sqlserver服务器该值是bit,表现为1/0如(图2)所示. (图1)Access表 (图2) Sqlserver数据 (表2)为access与Sqlserver的链接表.当在最下一行输入一个数值时,access会自动默认添加一行.此时查看Sqlserver值会发现只插入一个字段值,其他字段为null,而该字段值取决于你在access的哪个字段输入.如(图3)所示,在字段1输

JS DOM:显示文献来源链接表

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Explaining the Ddocument Ob Model</title> <link href="style08.css" type="text/css" rel="stylesheet&qu

JS DOM编程艺术——显示文献来源链接表—— JS学习笔记2015-7-17(第86天)

function displayCitations(){ // 取得所有引用 var quotes = document.getElementsByTagName("blockquote"); //遍历引用 for( var i=0; i<quotes.length; i++){ // 如果没有cite属性,继续循环 if(!quotes[i].getAttribute("cite")) continue; // 保存cite属性 var url = quot

2227 邮票--FUoj(链接表+树的直径)

http://acm.fzu.edu.cn/problem.php?pid=2227 我感觉这道题可以随意搞 题目大意: 给你的一个图就是一条链,但是不知道起始点和结束点,而且每个点只会访问一次. 因为数太大了 只能用邻接表保存. 我不知不觉的用了树的直径   代码写的太乱了  但是是我自己写的,虽然这道题真的不难,但是还是很开心 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ma

dede_flink|友情链接表主表详细介绍

dedecms二次开发目录点这个:dedecms二次开发教程目录 字段 类型 整理 属性 Null 默认 额外 id smallint(5) UNSIGNED 是 NULL 友链ID sortrank smallint(6) 是 0 排序值 url char(60) utf8_general_ci 是 链接地址 webname char(30) utf8_general_ci 是 网站名 msg char(200) utf8_general_ci 是 网站简况 email char(50) ut