简单双向链表队列的创建

根据马克(啊)思主义基本原理,事物是相互联系,相互影响的,在现实生活中,很多事物往往是相互联系在一起的,双向链表使用十分广泛,接下来就创建一个简单的双向链表队列

 

实现带头结点的双向链表的建立、求长度,取元素、修改元素、插入、删除、置空等双向链表的基本操作。

[基本要求]

 (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;
	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 基于双向链表实现双端队列结构 */ package dsa; public class Deque_DLNode implements Deque { protected DLNode header;//指向头节点(哨兵) protected DLNode trailer;//指向尾节点(哨兵) protected int size;//队列中元素的数目 //构造函数 public Deque_DLNode() { header = new DLNode(); trailer = new DLNode(); header.setNext(trailer); trailer.setPrev(header); size = 0; } //返回队列中元素数目 public int getSize() { return size; } //判断队列是否为空 public boolean isEmpty() { return (0 == size) ? true : false; } //取首元素(但不删除) public Object first() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); return header.getNext().getElem(); } //取末元素(但不删除) public Object last() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); return trailer.getPrev().getElem(); } //在队列前端插入新节点 public void insertFirst(Object obj) { DLNode second = header.getNext(); DLNode first = new DLNode(obj, header, second); second.setPrev(first); header.setNext(first); size++; } //在队列后端插入新节点 public void insertLast(Object obj) { DLNode second = trailer.getPrev(); DLNode first = new DLNode(obj, second, trailer); second.setNext(first); trailer.setPrev(first); size++; } //删除首节点 public Object removeFirst() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); DLNode first = header.getNext(); DLNode second = first.getNext(); Object obj = first.getElem(); header.setNext(second); second.setPrev(header); size--; return(obj); } //删除末节点 public Object removeLast() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); DLNode first = trailer.getPrev(); DLNode second = first.getPrev(); Object obj = first.getElem(); trailer.setPrev(second); second.setNext(trailer); size--; return(obj); } //遍历 public void Traversal() { DLNode p = header.getNext(); while (p != trailer) { System.out.print(p.getElem()+" "); p = p.getNex
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值