《Java数据结构和算法(第二版)》读书笔记

第2,3章 数组和简单排序

public class ArrayBub {

	private long[] a;
	private int nElems;//数组长度
	
	public ArrayBub(int max){
		a = new long[max];
		nElems = 0;
	}
	//插入
	public void insert(long num){
		a[nElems] = num;
		nElems++;
	}
	//显示
	public void diaplay(){
		for(int i = 0; i < nElems; i++){
			System.out.print(a[nElems]+" ");
		}
		System.out.println();
	}
	//查询
	public boolean find(long num){
		int i = 0;
		for(i = 0; i < nElems; i++){
			if(a[nElems] == num){
				break;
			}
		}
		if(i == nElems){
			return false;
		}else{
			return true;
		}
	}
	//删除
	public boolean delete(long num){
		int i = 0;
		for(i = 0; i < nElems; i++){
			if(a[nElems] == num){
				break;
			}
		}
		if(i == nElems){
			return false;
		}else{
			for(int j = i;j < nElems - 1;j++){
				a[j] = a[j+1];
			}
			num--;
			return true;
		}
	}
	
	//冒泡排序(O(n^2))
	//相邻两个比较,若前面值大,交换,否则向后移一位进行比较,直到数据结束,则最后一位为最大值,
	//然后再从第一位比到倒数第二位,依次进行
	public void bubbleSort(){
		for(int out = nElems-1; out > 1; out--)
			for(int in = 0; in < out; in++)
				if(a[in] > a[in+1])
					swap(in,in+1);
	}
	
	//选择排序(O(n^2),比冒泡排序交换次数少)
	//扫描一遍数组,从中挑出最小的一个与数组最左端值进行交换,最左端为有序的了,不需要再进行比较
	//再次扫描队列就从1开始,依次进行
	public void selectSort(){
		int out,in,min;
		for(out = 0; out < nElems - 1;out++){
			min = out;
			for(in = out + 1;in < nElems; in++){
				if(a[min] > a[in]){
					min = in;
				}
			}
			swap(out,min);
		}
	}
	
	//插入排序O(n^2)
	//将一个记录插入到已经排好的有序表中,从而得到一个新的、记录数增1的有序表
	public void insertSort(){
		int in,out;
		for(out = 1; out < nElems; out++){
			long temp = a[out];
			in = out;
			while(in > 0 && a[in-1] >= temp){
				a[in] = a[in-1];
				in--;
			}
			a[in] = temp;
		}
	}
	
	//交换
	public void swap(int i, int j){
		long temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
	
	
}

第4章 栈和队列

先进后出
用数组实现栈
public class Stack {

	private	int maxSize;
	private int[] stackArray;
	private int top;//栈顶
	
	public Stack(int size){
		maxSize= size;
		stackArray = new int[size];
		top = -1;
	}
	
	public void pop(int value){
		stackArray[++top] = value;
	}
	
	public int pop(){
		return stackArray[top--];
	}
	
	public boolean isEmpty(){
		return (top == -1);
	}
	
	public boolean isfull(){
		return (top == maxSize-1);
	}
}
队列
先进后出

第5章 链表

单链表
package sort;

class Link{
	public int iData;
	public double dData;
	public Link next;
	
	public Link(int id, double dd){
		iData = id;
		dData = dd;
	}
	
	public void display(){
		System.out.println("{"+iData +"," + dData +"}");
	}
}


public class LinkList {

	private Link first;
	
	public LinkList(){
		first = null;
	}
	//插入第一个节点
	public void insertFirst(int id,double dd){
		Link newLink = new Link(id, dd);
		newLink.next = first;
		first = newLink;
	}
	
	public Link deleteLink(int id){
		Link link = first;
		first = first.next;
		return link;
	}
	//显示link
	public void displayLink(){
		Link current = first;
		while(current != null){
			current.display();
			current = current.next;
		}
	}
	//查找指定链节点
	public Link find(int id){
		Link current = first;
		while(current.iData != id){
			if(current.next == null)
				return null;
			else 
				current = current.next;
		}
		return current;
	}
	
	//删除指定节点
	public Link delete(int key){
		Link current = first;
		Link previous = first;
		while(current.iData != key){
			if(current.next == null)
				return null;
			else{
				previous = current;
				current = current.next;
			}
		}
		if(current == first)
			first = first.next;
		else
			previous.next = current.next;
		return current;
	}
	
}
双向链表

双向链表的缺点是,每次处理一个节点的时候,要处理四个节点的引用。
class Link{
	public int iData;
	public double dData;
	public Link next;
	public Link previous;
	
	public Link(int id, double dd){
		iData = id;
		dData = dd;
	}
	
	public void display(){
		System.out.println("{"+iData +"," + dData +"}");
	}
}

 
     

第8章 二叉树

在树中,查找数据项的速度与有序数组一样快。插入、删除速度和链表一样快。

class Node{
	public int iData;
	public double dData;
	public Node leftNode;
	public Node rightNode;
	
}
public class Tree {
	private Node root;
	
	public Node find(int key){
		Node current = root;
		while(current.iData != key){
			if(current.iData > key){
				current = current.leftNode;
			}else{
				current = current.rightNode;
			}
			if(current == null){
				return null;
			}
		}
		return current;
	}
	//先序遍历
	public void preOrder(Node localRoot){
		if(localRoot != null){
			System.out.print(localRoot.iData+" ");
			inOrder(localRoot.leftNode);
			inOrder(localRoot.rightNode);
		}
	}
	//中序遍历
	public void inOrder(Node localRoot){
		if(localRoot != null){
			inOrder(localRoot.leftNode);
			System.out.print(localRoot.iData+" ");
			inOrder(localRoot.rightNode);
		}
	}
	//后序遍历
	public void postOrder(Node localRoot){
		if(localRoot != null){
			inOrder(localRoot.leftNode);
			inOrder(localRoot.rightNode);
			System.out.print(localRoot.iData+" ");
		}
	}
}















你可能感兴趣的:(读书笔记)