数据结构那些事儿(二)

说明:

这是转载的。

前一篇见:

http://blog.csdn.net/kakaxi_77/article/details/8333009


题目:数组中有重复数据,求出每个数据出现的次数并按照次数的由大到小排列出来

题目还是老题目,曲不离口,拳不离手,卖油翁2015:

1.二维数组

/**
* Program Name:
* Description: 数组中有重复数据,求出每个数据出现的次数并按照次数的由大到小排列出来
* Methods:
* @version ver:1.0
*/
public class CharCntTwo {
	private static int[][] iArray = {};
	
	public void handle(int[] arr){
		this.initArray(arr);
		this.sortArray();
		this.display();
	}
	
	public void initArray(int[] arr){
		if(null == arr || arr.length == 0)
			return;
		
		/*iArray = new int[arr.length][2];
		int i = 0;
		int key = 0;
		
		for(int iVar : arr){
			key = i++;
			iArray[key][0] = iVar;
			iArray[key][1] = 1;
		}*/
		
		int length = 0;
		Map map = new HashMap();
		for(int iVar : arr){
			map.put(iVar,"0");
		}
		
		iArray = new int[map.size()][2];
		
		System.out.println(iArray.length);
		System.out.println("=========");
		
		int index = 0;
		List list = new ArrayList();
		for(int i=0; i < arr.length; i++){
			int cnt = 1;
			
			if(list.contains(arr[i]))
				continue;
			for(int j=i+1; j < arr.length; j++){
				if(arr[i] == arr[j]){
					cnt++;
					continue;
				}
				
			}
			
			list.add(arr[i]);
			
			int key = index;
			System.out.println("key--->" + key);
			System.out.println("i--->" + i);
			iArray[key][0] = arr[i];
			iArray[key][1] = cnt;
			index++;
			
		}
		
		
		
		
	}
	
	public void sortArray(){
		for(int i=0; i<iArray.length -1; i++){
			for(int j=0; j<iArray.length -1 - i; j++){
				if(iArray[j][1] > iArray[j+1][1]){
					int[] temp = iArray[j];
					iArray[j] = iArray[j+1];
					iArray[j+1] = temp;
				}
			}
		}
	}
	
	public void display(){
		for(int i=0; i<iArray.length; i++){
			System.out.println(iArray[i][1] + "--->" + iArray[i][0]);
		}
	}
}

评论:

重点是学会二维数组的用法!

2. 链表

Node类

public class Node {
	public int data;
	public String desc;
	
	public Node next;
	
	public Node(int data, String desc){
		this.data = data;
		this.desc = desc;
	}
	
	public void displayNode(){
		System.out.println(this.data + " --> " + this.desc);
	}

}


链表类:

public class LinkList {
	private Node first;
	
	private LinkList(){
		first = null;
	}
	
	public boolean isEmpty(){
		return null == first ? true : false;
	}
	
	public void insertFirst(int key, String str){
		Node newNode = new Node(key, str);
		
		if(null == first){
			first = newNode;
		}else{
			newNode.next = first;
			first = newNode;
		}
	}
	
	public Node deletefirst(){
		if(isEmpty())
			return null;
		Node temp = first;
		first = first.next;
		
		return temp;
	}
	
	public Node find(int key){
		if(this.isEmpty())
			return null;
		
		Node current = first;
		while(current.data != key){
			if(null == current.next){
				return null;
			}else{
				current = current.next;
			}
		}
		
		return current;
	}
	
	public Node delete(int key){
		if(this.isEmpty())
			return null;
		
		Node current = first;
		Node previous = first;
		
		while(current.data != key){
			
			if(null == current.next){
				return null;
				
			}else{
				previous = current;
				current = current.next;
			}
		}
		

		if(first == current){
			first = first.next;
		}else{
			previous.next = current.next;
		}
		
		return current;
		
		
	}
	
	public void displayLink(){
		if(this.isEmpty())
			return;
		
		Node current = first;
		while(null != current){
			current.displayNode();
			current = current.next;
		}
	}
	
	public static void main(String[] args){
		LinkList list = new LinkList();;
		
		list.insertFirst(1, "a");
		list.insertFirst(2, "b");
		list.insertFirst(3, "c");
		
		list.displayLink();
		
		System.out.println("==========1=========");
		Node node = list.find(3);
		node.displayNode();
		
		System.out.println("==========2=========");
		list.delete(2);
		list.displayLink();
	}
	

}

评论:

普通链表,头插法。


3. 用链表实现的队列:

Node类

public class Node {
	public int data;
	public String value;
	public Node next;
	
	public Node(int data, String value){
		this.data = data;
		this.value = value;
	}
	
	public void display(){
		System.out.println(this.data + " -- > " + this.value);
	}
}

队列实现:

/**
 * Program Name:
* Description:
* Methods:
* @version ver:1.0
*
* Modified No:
* Modified By:
* Modified Date:
* Modified Description:
* 用双端链表实现的队列 
* 2015-05-03
 */
public class LinkQueue {
	private Node first;
	private Node last;
	
	private LinkQueue(){
		this.first = null;
		this.last = null;
	}
	
	public boolean isEmpty(){
		return null == first ? true : false;
	}
	
	/**
	 * 从尾部插入
	 * @param key
	 * @param val
	 */
	public void insertLast(int key, String val){
		Node newNode = new Node(key, val);
		if(this.isEmpty()){
			first = newNode;
			last = newNode;
		}else{
			last.next = newNode;
			last = newNode;
		}
	}
	
	/**
	 * 从头部移除
	 * @return
	 */
	public Node remove(){
		if(this.isEmpty())
			return null;
		
		Node temp = first;
		first = first.next;
		
		return temp;
			
	}
	
	public void display(){
		Node current = first;
		while(null != current){
			current.display();
			current = current.next;
		}
	}
	
	public static void main(String[] args){
		LinkQueue queue = new LinkQueue();
		queue.insertLast(1, "a");
		queue.insertLast(2, "b");
		queue.insertLast(3, "c");
		
		System.out.println("===========1===========");
		queue.display();
		System.out.println("===========2===========");
		Node temp = queue.remove();
		temp.display();
		System.out.println("===========3===========");
		queue.display();
	}
}

评论:

Java 自带队列类,参见LinkedList


4.树

节点类

public class Node {
	public int data;
	public String desc;
	
	public Node leftChild;
	public Node rightChild;
	
	public boolean isValid;
	
	public Node(int data, String str){
		this.data = data;
		this.desc = str;
		isValid = true;
	}
	
	public String toString(){
		return this.data + "-->" + this.desc;
	}

}


树类:

public class Tree {
	public Node root;
	
	public Node find(int key){
		Node current;
		
		if(null == root)
			return null;
		
		current = root;
		while(null != current){
			if(key == current.data)
				break;
			
			if(key < current.data){
				current = current.leftChild;
			}else{
				current = current.rightChild;
			}
		}
		
		
		return (null == current) ? null : current;
	}
	
	public void insert(int key, String value){
		Node node = new Node(key, value);
		Node current;
		Node parent;
		
		boolean isLeftChild = false;
		
		if(null == root){
			root = node;
			return;
		}
		
		current = root;
		parent = root;
		
		while(null != current){
			parent = current;
			if(key < current.data){
				current = current.leftChild;
				isLeftChild = true;
			}else{
				current = current.rightChild;
				isLeftChild = false;
			}
		}
		
		if(isLeftChild){
			parent.leftChild = node;
		}else{
			parent.rightChild = node;
		}			
		
	}
	
	/**
	 * 中序遍历
	 * @param node
	 */
	public void inOrder(Node node){
		if(null != node){
			inOrder(node.leftChild);
			if(node.isValid)
				System.out.println(node.toString());
			inOrder(node.rightChild);
		}
	}
	
	public boolean delete(int key){
		Node current;
		
		if(null == root)
			return false;
		
		current = root;
		while(null != current){
			if(key == current.data)
				break;
			
			if(key < current.data){
				current = current.leftChild;
			}else{
				current = current.rightChild;
			}
		}
		
		
		if(null == current)
			return false;
		
		current.isValid = false;   //置标志位,表示删除
		return true;
	}
}

评论:

一颗普通的树,删除的用法很灵活。

实际中,Java中自带红黑树:TreeMap

【总结】

实战中要灵活运用数据结构!

重点:不要重复发明轮子!

                                                ----2015-05-03




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值