根据马克(啊)思主义基本原理,事物是相互联系,相互影响的,在现实生活中,很多事物往往是相互联系在一起的,双向链表使用十分广泛,接下来就创建一个简单的双向链表队列
实现带头结点的双向链表的建立、求长度,取元素、修改元素、插入、删除、置空等双向链表的基本操作。
[基本要求]
(1)依次添加节点数据,建立带头结点的双向链表;
(2)打印双向链表中的数据元素
(3)求双向链表的长度;
(4)根据指定条件能够取元素和修改元素;
(5)实现在指定位置插入和删除元素的功能。
(6)链表置空操作
首先,定义一个双向链表节点类
/**
* 定义一个双向链表的节点类
* @author YangKang
*
*/
public class DoublyNode {
private Object data;//节点内的数据对象
private DoublyNode child;//对下一个节点的引用
private DoublyNode parent;//对上一节点的引用
//在创建节点对象的时候就传入节点中的数据对象
public DoublyNode (Object data) {
this.data = data;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public DoublyNode getChild() {
return child;
}
public void setChild(DoublyNode child) {
this.child = child;
}
public DoublyNode getParent() {
return parent;
}
public void setParent(DoublyNode parent) {
this.parent = parent;
}
}
然后定义双向链表队列(主函数用来测试其功能)
/**
* 定义一个简单的双向链表
* @author YangKang 2013.07.16
*
*/
public class DoublyNodeList {
public static DoublyNode root = null;//根节点
public static DoublyNode last = null;//最后一个节点
public static void main(String[] args) {
//加入节点
DoublyNodeList list = new DoublyNodeList();
//从链表最后添加节点,先进先出
list.add("aa");
list.add("bb");
list.add("cc");
//插入节点
list.insertDoublyNode(1, "000");
//链表队列置空
list.setNull();
list.add("aa");
list.add("bb");
list.add("cc");
//插入节点
list.insertDoublyNode(2, "000");
//删除节点
list.deleteDoublyNote(3);
//遍历节点
list.printDoublyNodeList(root);
}
在尾部增加节点
/**
* 在尾部增加节点
* @param obj :插入的节点对象
*/
public void add(Object obj){
DoublyNode node = new DoublyNode(obj);
if(null == root){
root = node;
last = root;
}else{
last.setChild(node);
node.setParent(last);
last = node;
}
}
在指定索引下插入节点
/**
* 在指定索引下插入节点
* @param index :(索引值)第几个节点(从零开始)
* @param obj :需要插入的节点对象
*/
public void insertDoublyNode(int index,Object obj){
if((this.getLength() < index)||(index < 0)){
throw new java.lang.RuntimeException("下标越界:" + index + ",最大长度:" +this.getLength());
}
else{
//创建一个新节点
DoublyNode newNode = new DoublyNode(obj);
//得到当前的索引位置的节点
DoublyNode node = this.getDoublyNode(index);
if(index == 0){
//若链表中没有节点,则插入的节点作为根节点
root = newNode;
}
else{
//得到父节点
DoublyNode fNode = node.getParent();
//加入待插入的节点,设置新的引用关系
fNode.setChild(newNode);
newNode.setParent(fNode);
}
//设置新的引用关系
newNode.setChild(node);
node.setParent(newNode);
}
}
根据索引删除节点
/**
* 根据索引删除节点
* @param index : (索引)第几个节点(从零开始)
*/
public void deleteDoublyNote(int index){
if((this.getLength() < index)||(index < 0)){
throw new java.lang.RuntimeException("下标越界:" + index + ",最大长度:" +this.getLength());
}
else{
//得到当前索引位置的节点
DoublyNode node =this.getDoublyNode(index);
//得到父节点
DoublyNode fNode = node.getParent();
//得到父节点
DoublyNode cNode = node.getChild();
//设置新的索引关系
if (fNode == null){
root = cNode;
}else if(cNode == null){
fNode.setChild(null);
}else {
fNode.setChild(cNode);
cNode.setParent(fNode);
}
}
}
根据索引取出节点
/**
* 根据索引取出节点
* @param index :第几个节点(从零开始索引)
* @return :根据索引返回的节点
*/
public DoublyNode getDoublyNode(int index){
if((this.getLength() < index)||(index < 0)){
throw new java.lang.RuntimeException("下标越界:" + index + ",最大长度:" +this.getLength());
}
else{
int num = 0;
DoublyNode node = root;
while (num != index){
node = node.getChild();
num++;
}
return node;
}
}
得到链表的长度
/**
* 得到链表的长度
* @return 链表的长度
*/
public int getLength(){
int count = 0;//内部计数器,可以不受多线程的影响
if(root == null){
return count;
}
DoublyNode node = root.getChild();
while (null != node){
count++;
node = node.getChild();
}
return count + 1;
}
修改对象节点
/**
* 修改对象节点
* @param index 对象节点的索引
* @param obj 修改对象内容
*/
public void ReviseDoublyNode(int index,Object obj){
if(this.getLength() < index || index < 0){
throw new java.lang.RuntimeException("下标越界:" + index + ",最大长度:" +this.getLength());
}else{
//得到当前索引的位置
DoublyNode node = this.getDoublyNode(index);
node.setData(obj);
}
}
遍历链表的方法
/**
* 遍历链表的方法
* @param root :链表的根节点
*/
public void printDoublyNodeList(DoublyNode root){
if (null != root ){
Object data = root.getData();
System.out.println(data);
DoublyNode temp = root.getChild();
printDoublyNodeList(temp);
}
}
链表置空操作:
/**
* 链表置空操作
*/
public void setNull(){
root = null;
last = null;
}