双向链表(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语言开发 ...
随机推荐
- 每天一个Linux命令(39)free命令
free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. (1)用法: 用法: free [选项参数] (2)功能: ...
- 04_Apache Hadoop 生态系统
内容提纲: 1)对 Apache Hadoop 生态系统的认识(Hadoop 1.x 和 Hadoop 2.x) 2) Apache Hadoop 1.x 框架架构原理的初步认识 3) Apache ...
- 获取电脑连接WiFi的信息
在cmd中执行如下命令,即可查看到所有连接过的WiFi信息 for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show ...
- 纯代码编写qt登录界面(转)
1. 新建Qt Widgets Application,项目名称为login1,在类信息页面保持类名和基类为MainWindow和QMainWindow不变,取消选择创建界面选项,如下图所示. ...
- android 加固防止反编译-重新打包
http://blog.csdn.net/u010921385/article/details/52505094 1.需要加密的Apk(源Apk) 2.壳程序Apk(负责解密Apk工作) 3.加密工具 ...
- MySQL运维问题集锦
1.莫名的慢查询问题.解决思路:http://hidba.org/?spm=5176.153233.793262.6.d75LDx&p=1119
- 超酷Loading进度条
在线演示 本地下载
- INSPIRED启示录 读书笔记 - 第5章 产品管理与软件开发
保持融洽的合作关系 形成合作关系的关键是双方承认彼此平等——任何一方不从属于另一方,产品经理负责定义正确的产品,开发团队负责正确地开发产品,双方相互依赖 产品经理要求开发团队完成任务,必须先取得他们的 ...
- 微服务(MicroServices)
微服务Architecture(MicroServices) 微服务架构简单的定义 采用一组Service的方式来构建一个应用,服务独立部署在不同的进程(Container)中,不同Service通过 ...
- Docker 监控平台Prometheus
Prometheus 是一个强大的监控平台,提供了监控数据搜集.存储.处理.可视化和告警一套完整的解决方案. 官方网站:https://prometheus.io
public class doubleLinkedList <Item>{
private Node first;