制作属于自己的工具类

最近活不多,借此来制作属于自己的一套工具类,先从数据结构和算法开始,下面上代码
/**
 * 单向链表
 * @author TMACJ
 * 
 *
 */
public class LinkedList {
	
	private int size = 0;
	
	private Node head;//头指针(不含数据域)
	
	public LinkedList() {
		this.head = new Node(null, null, null);
	}
	/**
	 * 增
	 * @param node
	 */
	public void insert(Node node){
		this.insert(node, this.size);
	}
	/**
	 * 增
	 * @param node
	 * @param position
	 */
	public void insert(Node node,int position){
		Node tempNode = moveCursor(position);
		//设置节点指针指向
		node.setPrevious(tempNode);
		node.setNext(tempNode.getNext());
		Node next = tempNode.getNext();
		if(next!=null){
			next.setPrevious(node);
		}
		tempNode.setNext(node);
		this.size++;
	}
	/**
	 * 删
	 * @param position
	 */
	public void delete(int position){
		position++;
		Node tempNode = moveCursor(position);
		Node previous = tempNode.getPrevious();
		Node next = tempNode.getNext();
		if(previous!=null){
			previous.setNext(tempNode.getNext());
		}
		if(next!=null){
			next.setPrevious(tempNode.getPrevious());
		}
		this.size--;
	}
	/**
	 * 改
	 * @param node
	 * @param position
	 */
	public void update(Node node,int position){
		position++;
		Node tempNode = moveCursor(position);
		Node previous = tempNode.getPrevious();
		Node next = tempNode.getNext();
		if(previous!=null){
			previous.setNext(node); 
		}
		if(next!=null){
			next.setPrevious(node);
		}
		node.setPrevious(previous);
		node.setNext(next);
		
	}
	/**
	 * 查
	 * @param position
	 * @return
	 */
	public Node select(int position){
		position++;
		Node tempNode = moveCursor(position);
		return tempNode;
	}
	
	public Node moveCursor(int position){
		int i = 0;
		Node tempNode = head;
		while(i<position){
			tempNode = tempNode.getNext();
			i++;
		}
		return tempNode;
	}
	
	public void iterator(){
		int i = 0;
		Node tempNode = head;
		while(i<this.getSize()){
			tempNode = tempNode.getNext();
			i++;
			System.out.println(tempNode.getData());
		}
	}


	public int getSize() {
		return size;
	}


	public void setSize(int size) {
		this.size = size;
	}


	public Node getHead() {
		return head;
	}


	public void setHead(Node head) {
		this.head = head;
	}
	
}
 
 
/**
 * 插入排序(升序)
 * @author TMACJ
 *思路:先取一个有序的队列,然后将其他数字一个一个和这个有序数列排序
 *稳定
 *时间复杂度  最好情况:O(n) 最坏情况O(n²)
 *空间复杂度 O(n)
*/
public class StraightInsertionSort {
	
	public static void sort(LinkedList ll){
		//把第一数据当成一个有序的一组,然后把其他的数据依次插入比较
		for(int i=1;i<ll.getSize();i++){
			for(int j=0;j<i;j++){
				if((int)ll.select(i).getData()<(int)ll.select(j).getData()){
					int temp = (int)ll.select(j).getData();
					ll.select(j).setData(ll.select(i).getData());
					ll.select(i).setData(temp);
				}
			}
		}
	}
	
}


/*测试类*/
public class Test{
	
	public static void main(String[] args) {
		LinkedList ll = new LinkedList();
		ll.insert(new Node(null, null, 10));
		ll.insert(new Node(null, null, 9));
		ll.insert(new Node(null, null, 8));
		ll.insert(new Node(null, null, 7));
		ll.insert(new Node(null, null, 6));
		ll.insert(new Node(null, null, 5));
		ll.insert(new Node(null, null, 4));
		ll.insert(new Node(null, null, 3));
		ll.insert(new Node(null, null, 2));
		ll.insert(new Node(null, null, 1));
		StraightInsertionSort.sort(ll);
		ll.iterator();
	}
	
}

输出结果:

1
2
3
4
5
6
7
8
9
10


/**
 * 希尔排序(直接插入排序的改良算法,升序)
 * @author TMACJ
 * 时间复杂度O(n^2)(这里虽然有4个循环,但是while和for之间是没有联系的,所以时间复杂度还是O(n^2))
 * 空间复杂度(1)
 * 希尔排序利用插入排序对于基本有序的数列高效的特点,先选取增量d,然后在进行直接插入排序,达到提升效率的目的
 * 然而,对于增量如何选取,目前也没有什么太好的办法,好像数学界还没解决这个问题..我就不浪费时间了
 * 一般而言,第一次的增量选择采用N/2
 */
public class ShellSort {

	public static void sort(LinkedList ll){
		int n = ll.getSize();
		int d = n/2;
		while(d!=0){
			int i = 0;
			while(i<d){
				for(int j=1;j<ll.getSize();j=j+d){
					for(int k=0;k<j;k=k+d){
						if((int)ll.select(j).getData()<(int)ll.select(k).getData()){
							int temp = (int)ll.select(k).getData();
							ll.select(k).setData(ll.select(j).getData());
							ll.select(j).setData(temp);
						}
					}
				}
				i++;
			}
			d = d/2;
		}
	}
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值