java 链表形式线性表插入排序

链表形式的线性表排序时采用插入排序

import java.util.Iterator;

public interface ListInterface<T>{
	public boolean add(T newEntry);

	public boolean add(int newPosition, T newEntry);

	public T remove(int givenPosition);

	public void clear();

	public T set(int givenPosition, T newEntry);

	public T get(int givenPosition);

	public boolean contains(T anEntry);

	public int size();

	public boolean isEmpty();

	public boolean isFull();

	public void display();
}

public class LinkedChainList<T extends Comparable<? super T>> implements
		ListInterface<T> {

	private Node firstNode;
	private int length;

	private class Node {
		private T data;
		private Node next;

		Node(T dataPortion) {
			data = dataPortion;
			next = null;

		}

		Node(T dataPortion, Node nextNode) {
			data = dataPortion;
			next = nextNode;
		}

		public T getData() {
			return data;
		}

		public void setData(T data) {
			this.data = data;
		}

		public Node getNext() {
			return next;
		}

		public void setNext(Node next) {
			this.next = next;
		}

	}

	public LinkedChainList() {
		// TODO Auto-generated constructor stub
		clear();
	}

	public Node getNodeAt(int givenPosition) {
		Node currentNode = firstNode;
		if (!isEmpty() && givenPosition >= 0 && givenPosition < length) {
			for (int i = 0; i < givenPosition; i++) {
				currentNode = currentNode.getNext();
			}
		} else {
			currentNode = null;
		}
		return currentNode;
	}

	@Override
	public boolean add(T newEntry) {
		// TODO Auto-generated method stub
		Node newNode = new Node(newEntry);
		if (isEmpty()) {
			firstNode = newNode;
		} else {
			Node lastNode = getNodeAt(length - 1);
			lastNode.setNext(newNode);

		}
		length++;
		System.out.println("add : " + newEntry);
		return true;
	}

	@Override
	public boolean add(int newPosition, T newEntry) {
		// TODO Auto-generated method stub
		boolean isAdd = true;

		if (newPosition >= 0 && newPosition <= length) {
			Node newNode = new Node(newEntry);
			if (isEmpty() || newPosition == 0) {
				newNode.setNext(firstNode);
				firstNode = newNode;
			} else {
				Node before = getNodeAt(newPosition - 1);
				Node after = before.getNext();
				newNode.setNext(after);
				before.setNext(newNode);
			}
			length++;
		} else
			isAdd = false;
		return isAdd;
	}

	@Override
	public T remove(int givenPosition) {
		// TODO Auto-generated method stub
		T result = null;
		if (!isEmpty() && givenPosition >= 0 && givenPosition < length) {
			if (givenPosition == 0) {
				result = firstNode.getData();
				firstNode = firstNode.getNext();
			} else {
				Node before = getNodeAt(givenPosition - 1);
				Node remove = before.getNext();
				Node after = remove.getNext();
				before.setNext(after);
				result = remove.getData();
			}
			length--;
		} else {
			System.out.println("删除出错");
		}
		return result;
	}

	@Override
	public final void clear() {
		// TODO Auto-generated method stub
		firstNode = null;
		length = 0;
	}

	@Override
	public T set(int givenPosition, T newEntry) {
		// TODO Auto-generated method stub
		T old = null;
		if (!isEmpty() && givenPosition >= 0 && givenPosition < length) {
			Node oldNode = getNodeAt(givenPosition);
			old = oldNode.getData();
			oldNode.setData(newEntry);
		} else {
			System.out.println("设置出错");
		}
		return old;
	}

	@Override
	public T get(int givenPosition) {
		// TODO Auto-generated method stub
		T result = null;
		if (!isEmpty() && givenPosition >= 0 && givenPosition < length)
			result = getNodeAt(givenPosition).getData();
		return result;
	}

	@Override
	public boolean contains(T anEntry) {
		// TODO Auto-generated method stub
		boolean isContain = false;
		Node currentnNode = firstNode;
		while (!isContain && currentnNode != null) {
			if (anEntry.equals(currentnNode.getData())) {
				isContain = true;
				System.out.println("包含该元素");
			} else {
				currentnNode = currentnNode.getNext();

			}

		}
		if (isContain == false)
			System.out.println("不包含该元素");
		return isContain;
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		System.out.println("size :  " + length);
		return length;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		boolean result;
		if (length == 0 && firstNode == null) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	@Override
	public boolean isFull() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void display() {

		// TODO Auto-generated method stub
		Node currentNode = firstNode;
		System.out.print("The list : ");
		while (currentNode != null) {
			System.out.print(currentNode.getData() + "  ");
			currentNode = currentNode.getNext();
		}
		System.out.println();
	}

	private void insertInOrder(Node nodeToInsert)
	{
          T item=nodeToInsert.getData();
          Node currentNode=firstNode;
          Node previousNode=null;
          while((currentNode!=null)&&item.compareTo(currentNode.getData())>0)
          {
        	  previousNode=currentNode;
        	  currentNode=currentNode.getNext();
        	  
          }
          if(previousNode!=null)
          {
        	  previousNode.setNext(nodeToInsert);
        	  nodeToInsert.setNext(currentNode);
        	  
          }
          else {
			nodeToInsert.setNext(firstNode);
			firstNode=nodeToInsert;
		}
	}
	
	public void insertSort() {
		if (length > 1) {
			Node unsortedPart = firstNode.getNext();
			firstNode.setNext(null);
			while(unsortedPart!=null)
			{
				Node nodeToInsert=unsortedPart;
				unsortedPart=unsortedPart.getNext();
				insertInOrder(nodeToInsert);
			}

		} else if (length == 1) {
			System.out.println("链表中只有一个元素不用排序");
		} else {
			System.out.println("链表中没有元素无法排序");
		}
	}
	public static void main(String[] argv)
	{
		LinkedChainList<Integer> list=new LinkedChainList<Integer>();
		list.add(2);
		list.add(3);
		list.add(2);
		list.add(6);
		list.add(4);
		list.add(5);
		list.display();
		list.insertSort();
		list.display();
		list.add(1);
		list.display();
		list.insertSort();
		list.display();
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值