zoukankan      html  css  js  c++  java
  • 线性表——(3)双向链表

    #include"ListNode.h"
    
    template<class T>
    class DoubleList
    {
    public:
    	DoubleList() {
    		head = new ListNode<T>();
    		head->p_prior = head;
    		head->p_next = head;
    	}
    	~DoubleList() {
    		Clear();
    		delete head;
    	}
    	void Clear();	// 清空
    	int Length() const; // length
    	ListNode<T>* Find(T item);	// 查找item,返回指针,找不到返回空
    	int GetItem(int n, T &targ);	// 查找n号元素,存入targ
    	int Insert(int n, const T targ);	//	将targ插入n号前
    	int Delete(int n);	// 删除n号
    
    private:
    	ListNode<T>* head;
    };
    
    template<class T>
    inline void DoubleList<T>::Clear()
    {
    	ListNode<T>* p = head->p_next;
    	ListNode<T>* pdel;
    	while (p != head) {
    		pdel = p;
    		p = pdel->p_next;
    		delete pdel;
    	}
    	p->p_prior = head;
    	p->p_next = head;
    }
    
    template<class T>
    inline int DoubleList<T>::Length() const
    {
    	int count = 0;
    	ListNode<T>* p = head->p_next;
    	while (p != head) {
    		++count;
    		p = p->p_next;
    	}
    	return count;
    }
    
    template<class T>
    inline ListNode<T>* DoubleList<T>::Find(T item)
    {
    	ListNode<T>* prior = head->p_prior;
    	ListNode<T>* next = head->p_next;
    	while (prior->p_next != next && prior != p_next) {
    		if (prior->data == item)
    			return prior;
    		if (next->data == item)
    			return next;
    		prior = prior->p_prior;
    		next = next->p_next;
    	}
    	return NULL;
    }
    
    template<class T>
    inline int DoubleList<class T>::GetItem(int n, T & targ)
    {
    	if (n < 0)
    		return 0;
    	ListNode<T>* p = head;
    	for (int i = 0; i < n; ++i) {
    		p = p->p_next;
    		if (p == head)
    			return 0;
    		T data = p->data;
    		return data;
    	}
    }
    
    template<class T>
    inline int DoubleList<T>::Insert(int n, const T targ)
    {
    	if (n < 0)
    		return 0;
    	ListNode<T>* p_newnode = new ListNode<T>(targ);
    	ListNode<T>* p = head;
    	for (int i = 0; i < n; ++i) {
    		p = p->p_next;
    		if (p == head)
    			return 0;
    	}
    	p_newnode->p_next = p->p_next;
    	p_newnode->p_prior = p;
    	p->p_next = p_newnode;
    	p->p_next->p_prior = p_newnode;
    	return 1;
    }
    
    template<class T>
    inline int DoubleList<T>::Delete(int n)
    {
    	if (n < 0)
    		return 0;
    	ListNode<T>* p = head;
    	ListNode<T>* pdel;
    	for (int i = 0; i < n; ++i) {
    		p = p->p_next;
    		if (p == head)
    			return 0;
    	}
    	pdel = p;
    	p->p_prior->p_next = p->p_next;
    	p->p_next->p_prior = p->p_prior;
    	delete pdel;
    	return 1;
    }
    
    
    醉 生 梦 死
  • 相关阅读:
    C#学习之自定义类实现foreach
    C#学习之用迭代器实现枚举器
    silverlight学习之获取照片的路径
    Java语言基础基本数据类型与运算符
    Java语言基础数组
    Asp.Net 学习资源列表
    测试第一篇BLOG文,高亮代码
    当屌丝想看《蜀山剑侠传》[python屏幕抓取]
    jQuery数据显示插件整合
    腾讯面试题:50个阶梯,你一次可以上一阶或两阶,走上去,共有多少种走法【原】
  • 原文地址:https://www.cnblogs.com/TuerLueur/p/9839193.html
Copyright © 2011-2022 走看看