数据结构与算法分析(java语言描述)第三章,第二题

通过只调整链表(而不是数据)来交换两个相邻元素,使用

a.单链表

b.双链表

a中是整体代码,书中P54的Demo代码

a.单链表

package Test;

import java.util.Iterator;

public class Test2b implements Iterable{
	private static class Node{
		public Node(AnyType data, Node prev, Node next) {
			this.data = data;
			this.prev = prev;
			this.next = next;
		}
		public AnyType data;
		public Node prev;
		public Node next;	
	}
	
	public Test2b() {
		doClear();
	}
	
	public void clear(){
		doClear();
	}
	private void doClear(){
		beginMarker = new Node(null,null,null);
		endMarker = new Node(null,beginMarker,null);
		beginMarker.next = endMarker;
		
		theSize = 0;
		modCount++;
	}
	public int size(){
		return theSize;
	}
	public boolean isEmpty(){
		return size()==0;
	}
	
	public boolean add(AnyType x) {
		add(size(),x);
		return true;
	}
	public void add(int idx,AnyType x) {
		addBefore(getNode(idx,0,size()),x);
	}
	public AnyType get(int idx) {
		return getNode(idx).data;
	}
	public AnyType set(int idx,AnyType newVal) {
		Node p = getNode(idx);
		AnyType oldVal = p.data;
		p.data = newVal;
		return oldVal;
	}
	public AnyType remove(int idx) {
		return remove(getNode(idx));
	}
 	private void addBefore(Node p,AnyType x) {
		Node newNode = new Node<>(x,p.prev,p);
		newNode.prev.next = newNode;
		p.prev = newNode;
		theSize++;
		modCount++;
	}
	private AnyType remove(Node p) {
		p.next.prev = p.prev;
		p.prev.next = p.next;
		theSize--;
		modCount++;
		return p.data;
	}
	private Node getNode(int idx){
		return getNode(idx,0,size()-1);
	}
	private Node getNode(int idx,int lower,int upper){
		Node p;
		if (idxupper) 
			throw new IndexOutOfBoundsException();
		if (idx idx; j--) 
				p = p.prev;
			}
		return p;
	}
	
	public java.util.Iterator iterator(){
		return new LinkedListIterator();
	}
	private class LinkedListIterator implements java.util.Iterator{
		private Node current = beginMarker.next;
		private int expectedModCount = modCount;
		private boolean okToRemove = false;
		public boolean hasNext() {
			return current !=endMarker;
		}
		public AnyType next() {
			if (modCount != expectedModCount) {
				throw new java.util.ConcurrentModificationException();
			}
			if (!hasNext()) {
				throw new java.util.NoSuchElementException();
			}
			AnyType nextItem = current.data;
			current = current.next;
			okToRemove = true;
			return nextItem;
		}
		public void remove() {
			if (modCount != expectedModCount) {
				throw new java.util.ConcurrentModificationException();
			}
			if (!okToRemove) {
				throw new IllegalStateException();
			}
			Test2b.this.remove(current.prev);
			expectedModCount++;
			okToRemove = false;
		}
	}
	/*
	 * 单链表
	 * */
	public static void seapWithNext(Node beforep) {
		Node p,afterp;
		//定义之前p的位置关系
		p = beforep.next;//位置关系:beforep,p,afterp
		afterp = p.next;
		
		afterp.next = p.next;//更改位置关系,需要改变3条链
		
		//更改后的位置关系
		afterp = beforep.next;
		p = afterp.next;
		
	}
	private int theSize;
	private int modCount;
	private Node beginMarker;
	private Node endMarker;
	
}

b.双链表(将单链表代码块部分替换即可)

/*
	 * 双链表
	 * */
	public static void seapWithNext(Node p) {
		Node beforep, afterp;
		//定义之前p的位置关系,
		//p是传入的,而新定义的beforep和afterp是null(没被赋值,没有引用)
		//p = afterp.prev;是错误的
		beforep = p.prev;
		afterp = p.next;
		
		//更改后的位置关系
		p.next = afterp.next;
		p = afterp.next;
		afterp.next = p;
		afterp.prev = beforep;
		beforep.next = afterp;
		p.prev = afterp;
		
	}

 

你可能感兴趣的:(数据结构与算法分析)