双向链表(Double-Linked List)
public class doubleLinkedList <Item>{ private Node first;
private Node last;
private int itemcount;
class Node{
Node prev;
Node next;
Item item; } public void addFirst(Item item){
Node oldfirst=first;
first=new Node();
first.item=item;
first.next=oldfirst;
if(oldfirst!=null){ oldfirst.prev=first; } if(itemcount==0){ last=first;
}
itemcount++;
} public void addlast(Item item){ Node oldlast=last;
last=new Node();
last.item=item;
last.prev=oldlast; if(oldlast!=null){ oldlast.next=last;
} if(itemcount==0){ last=first;
} itemcount++;
}
//
public Item delfirst(){
if(first==null){
throw new NullPointerException("no node linked list");
}
Item item=first.item;
first=first.next;
if(first!=null){ first.prev=null;
} if(itemcount==1){ last=null;
}
itemcount--;
return item; } public Item dellast(){ if(last==null){
throw new NullPointerException("no node this linked");
}
Item item =last.item;
last=last.prev; if(last!=null){ last.next=null; }
if(itemcount==1){ first=null;
}
itemcount--;
return item; } //
public void addbefore(Item targetItem,Item item){ Node target=first;
if(target==null){ throw new NullPointerException("no node this linked");
} if(target!=null && target.item!=targetItem){ target=target.next;
} if(target.prev==null){ addFirst(item);
}
else{
Node targetprev=target.prev;
Node newtarget=new Node();
newtarget.item=item;
target.prev=newtarget;
newtarget.next=target;
newtarget.prev=targetprev;
targetprev.next=newtarget; itemcount++;
}
} //
public void addafter(Item targetItem,Item item){ Node target =first;
if(target==null){ throw new NullPointerException("no node this linked!"); }
if(target!=null && target.item!=targetItem){
target=target.next; }
if(target.next==null){ addlast(item);
}
else{ Node oldtarget=target.next;
Node newnode=new Node();
newnode.item=item;
newnode.next=oldtarget;
oldtarget.prev=newnode;
target.next=newnode;
newnode.prev=target; itemcount++;
} }
}
双向链表(Double-Linked List)的更多相关文章
- Linux C double linked for any data type
/************************************************************************** * Linux C double linked ...
- 数据结构(C++)之Double Linked List实践
//double linked list (type int),the position starts from 0 #include <iostream> using namespace ...
- java数据结构——单链表、双端链表、双向链表(Linked List)
1.继续学习单链表,终于摆脱数组的魔爪了,单链表分为数据域(前突)和引用域(指针域)(后继),还有一个头结点(就好比一辆火车,我们只关心火车头,不关心其它车厢,只需知晓车头顺藤摸瓜即可),头结点没有前 ...
- javascript实现数据结构与算法系列:循环链表与双向链表
循环链表(circular linked list) 是另一种形式的链式存储结构.它的特点是表中最后一个结点的指针域指向头结点,整个表形成一个环. 循环链表的操作和线性链表基本一致,仅有细微差别. w ...
- C++ Templates STL标准模板库的基本概念
STL标准库包括几个重要的组件:容器.迭代器和算法.迭代器iterator,用来在一个对象群集的元素上进行遍历操作.这个对象群集或许是一个容器,或许是容器的一部分.迭代器的主要好处是,为所有的容器提供 ...
- stl源码剖析 详细学习笔记priority_queue slist
// // priority_queue.cpp // 笔记 // // Created by fam on 15/3/16. // // //------------------------- ...
- complexity_action
大话数据结构 /* 顺序存储的结构 */ #define MAXSIZE 20 //存储空间初始分配量 typedef int ElemType; //ElemType类型根据实际情况而定,这里假设为 ...
- [DT] 数据结构术语中英文对照
数据结构术语中英文对照 数据 Data 数据元素 Data element 数据项 Data item 数据结构 Data structure 逻辑结构 Logical structure 数据类型 ...
- .Net Core使用分布式缓存Redis:数据结构
一.前言 本篇主要使用StackExchangeRedis在.Net Core中使用Redis,使用基础见:点击此处. 二.五种基础数据结构 1.字符串类型String 字符串类型是Redis中最基本 ...
- 分布式架构-Redis 从入门到精通 完整案例 附源码
导读 篇幅较长,干货十足,阅读需要花点时间,全部手打出来的字,难免出现错别字,敬请谅解.珍惜原创,转载请注明出处,谢谢~! NoSql介绍与Redis介绍 什么是Redis? Redis是用C语言开发 ...
随机推荐
- 用js来实现那些数据结构 第二章
这一篇文章,我们一起来看看数组还有哪些用法,以及在实际工作中我们可以用这些方法来做些什么.由于其中有部分内容并不常用,所以我尽量缩小篇幅.在这篇文章内介绍完大部分的数组方法,加快我们实现其它数据结构的 ...
- eclipse新建自定义EL函数
==================================================================================================== ...
- SpringBoot Mybatis keyProperty和useGeneratedKeys的作用
在使用mybatis时,常常会出现这种需求: 当主键是自增的情况下,添加一条记录的同时,其主键是不能被使用的(为空),但是有时我们需要该主键,这时我们该如何处理呢?这时我们只需要在其对应xml中加入以 ...
- SQLite3时间函数小结
import sqlite3 conn = sqlite3.connect('/tmp/sqlite.db') cur = conn.cursor() 接下来干嘛呢?建一张表吧.这里需要注意的是,SQ ...
- 微信小程序与微信公众号同一用户登录问题
微信小程序与微信公众号同一用户登录问题 最近在做微信小程序与微信公众号登录合并的接口.整理相关资料以及个人认识的心得写了这篇文章与大家一起分享. 首先,简单说下我遇到的问题是我们的程序调用微信小程序得 ...
- SYN blood攻击
SYN Flood (SYN洪水) 是种典型的DoS (Denial of Service,拒绝服务) 攻击.效果就是服务器TCP连接资源耗尽,停止响应正常的TCP连接请求. 说到原理,还得从TCP如 ...
- Complex social network Partition for Balanced Subnetworks---Hao Lan Zhang,Jiming Liu,Chunyu Feng,Chaoyi Pang,Tongliang Li,Jing He阅读
摘要:Abstract—Complex social network analysis methods have been applied extensively in various domains ...
- Java中的UDP协议编程
一. UDP协议定义 UDP协议的全称是用户数据报,在网络中它与TCP协议一样用于处理数据包.在OSI模型中,在第四层——传输层,处于IP协议的上一层.UDP有不提供数据报分组.组装和不能对数据包 ...
- DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。
DATE_FORMAT(date,format) format参数的格式有 %a 缩写星期名 %b 缩写月名 %c 月,数值 %D 带有英文前缀的月中的天 %d 月的天,数值(00-31) %e 月的 ...
- MapReduce-排序(全部排序、辅助排序)
排序 排序是MapReduce的核心技术. 1.准备 示例:按照气温字段对天气数据集排序.由于气温字段是有符号的整数,所以不能将该字段视为Text对象并以字典顺序排序.反之,用顺序文件存储数据,其In ...