java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表
最近在准备找工作的事情,就复习了一下java。翻了一下书和网上的教材,发现虽然很多书是用java讲数据结构的,但是侧重于将数据结构本身的知识,利用java本身的类库来实现数据结构的系统性的讲解少之又少,所以在此做一下总结,方便各位正在准备工作的和用java实现数据结构的朋友们。
附:代码下载:http://download.csdn.net/detail/sunnyskyliu/4754827
栈:
- void m_stack()
- {
- //Stack
- Stack<String> st=new Stack<String>();
- //Stack<Integer> st=new Stack<Integer>();
- st.push("1a");
- st.push("2b");
- st.push("3c");
- st.push("4d");
- System.out.println("pop is:"+st.pop());
- System.out.println("size is:"+st.size());
- System.out.println("peek is:"+st.peek());
- System.out.println("isEmpty is:"+st.isEmpty());
- System.out.println("toString is:"+st.toString());
- System.out.println();
- }
- void m_queue()
- {
- //Queue
- Queue<String> qu=new LinkedList<String>();//wrong:Queue<String> qu=new Queue<String>();
- qu.add("1a");
- qu.add("2b");
- qu.add("3c");
- qu.add("4d");
- //qu.offer("4d");//equal to add,but no exception
- System.out.println("remove is:"+qu.remove());
- //System.out.println("poll is:"+qu.poll());//equal to remove,but no exception
- System.out.println("peek is:"+qu.peek());
- System.out.println(qu.toString());
- System.out.println();
- }
优先级队列:
- class m_priorityQueue extends PriorityQueue<m_priorityQueue>
- implements Comparable<m_priorityQueue>
- {
- private char primary;
- private int secondary;
- private String item;
- public m_priorityQueue(String td,char pri,int sec){
- item = td;
- primary = pri;
- secondary = sec;
- }
- //for comparable
- public int compareTo(m_priorityQueue arg) {
- if(primary > arg.primary)
- return +1;
- if(primary == arg.primary)
- if(secondary > arg.secondary)
- return +1;
- else if(secondary == arg.secondary)
- return 0;
- return -1;
- }
- public String toString()
- {
- return Character.toString(primary)+ secondary + ": " + item;
- }
- public void add(String td,char pri,int sec)
- {
- super.add(new m_priorityQueue(td,pri,sec));
- }
- public void run()
- {
- add("sixth",'C',4);
- add("second",'A',2);
- add("fourth",'B',7);
- add("fifth",'C',3);
- add("first",'A',1);
- add("third",'B',1);
- while(!isEmpty())
- System.out.println(remove());
- }
- }
- //link list
- void m_linkList()
- {
- LinkedList<String> head = new LinkedList<String>();
- head.add("1");
- head.add("3");
- head.add(1, "2");
- //鏈表會自動用遍歷的方式打印所有內容
- System.out.println(head);
- System.out.println("size is:"+head.size());
- //利用Vector或者是ArrayList等Collection往鏈表中加數據
- Vector<String> v = new Vector<String>(); //need to import java.util.Vector;
- //LinkedList<String> v=new LinkedList<String>();
- //向Vector中加入東西
- v.add("a");
- v.add("b");
- //將當前Vector加在當前鏈表的最后
- head.addAll(v);
- System.out.println(head);
- //將當前Vector加在當前鏈表的指定位置
- head.addAll(2, v);
- System.out.println(head);
- //打印指定位置的內容
- System.out.println("get(2) is:"+head.get(2));
- System.out.println("index is:"+head.indexOf(head.get(2)));
- head.addFirst("fist");
- head.addLast("last");
- System.out.println(head);
- //刪除第一個
- head.remove(head.getFirst());
- head.remove(head.getLast());
- System.out.println(head);
- //再刪除第一個,采用下標的方式,下標從0開始
- head.remove(0);
- System.out.println(head);
- System.out.println();
- }
哈希:
- void m_hash()
- {
- Hashtable<String, Integer> h=new Hashtable<String, Integer>();
- h.put("用户1",new Integer(90));
- h.put("用户2",new Integer(50));
- h.put("用户3",new Integer(60));
- h.put("用户4",new Integer(70));
- h.put("用户1",new Integer(80));
- System.out.println("用户1: "+h.get("用户1"));
- /*
- //枚举
- Enumeration<Integer> e=h.elements();
- while(e.hasMoreElements())
- {
- System.out.println(e.nextElement());
- }*/
- for(Iterator<String> itor=h.keySet().iterator(); itor.hasNext();)
- {
- String word=itor.next().toString();
- Integer in=(Integer)h.get(word);
- System.out.println("word: "+word+" time:"+in);
- }
- }
main调用:
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- dataStructure da=new dataStructure();
- da.m_stack();
- da.m_queue();
- da.m_linkList();
- da.m_hash();
- dataStructure.m_priorityQueue pqueue=da.new m_priorityQueue(null, '0', 0);
- pqueue.run();
- }