双向链表(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语言开发 ...
随机推荐
- Funq之Lambda表达式入门
今天接受了一个Tranning关于.net3.5 framework中的new feature. 其中最不明白的还是Lambda表达式.回来后又仔细的思考了一番,总算有点体会在这里写一下.既然是入门, ...
- 如何选择合适的Linux系统进行桌面程序开发?
32 or 64 ? 众所周知,64位的Windows系统可以近乎完美地运行32位的应用程序,微软出于商业考虑做了这样一个兼容层.而Linux系统则划分的很清楚,默认情况下64位的Linux系统无法运 ...
- 最新版express使用时的变化
原文:http://www.unfish.net/archives/772-20131207.html 很幸运地找到这篇文章,里面的内容讲的非常的细,对于开始着手搭建项目的我来说特别有用.但文中的部分 ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- java配置好jdk-bash: /usr/bin/java: No such file or directory
在 Linux 系统中安装 JDK 环境,配置好环境变量后,输入 java.javac 或者 java -version 等时,都提示如下错误: -bash: /usr/local/java/bin/ ...
- HGVS的变异格式
符号: 1.HGVS的变异格式由两部分组成: 1.1 reference sequence file identifier (accession.version-number) : actual d ...
- Django 组合索引
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join( ...
- 计数排序(COUNTING-SORTING)
计数排序的思想: 计数排序是对每一个输入元素x;确定小于x的元素个数. 计数排序算法: 第一个for循环为统计arra 中的每一个数值的个数,并且放在相应arrc 数组中的arra[i]位,第二个fo ...
- spring mvc 对象型参数的传递(遇到坑了)
直接来个列子: 这里设置了,contenType="application/json" 这里post 接收的参数对象. 但是问题来了: <html> <head& ...
- 解析远程域名主机的IP地址
我们知道,计算机在访问远程主机的时候,本质上是通过IP地址来进行访问的,但我们实际在使用的时候,例如我们想访问百度的主页,我们是通过在浏览器的地址栏输入百度的域名来进行访问的,因此,计算机需要将百度的 ...