java實現 資料結構:鏈表、 棧、 隊列、優先順序隊列、雜湊表

來源:互聯網
上載者:User

最近在準備找工作的事情,就複習了一下java。翻了一下書和網上的教材,發現雖然很多書是用java講資料結構的,但是側重於將資料結構本身的知識,利用java本身的類庫來實現資料結構的系統性的講解少之又少,所以在此做一下總結,方便各位正在準備工作的和用java實現資料結構的朋友們。

附:代碼下載:http://download.csdn.net/detail/sunnyskyliu/4754827

棧:

void m_stack(){//StackStack<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(){//QueueQueue<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 exceptionSystem.out.println("remove is:"+qu.remove());//System.out.println("poll is:"+qu.poll());//equal to remove,but no exceptionSystem.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 listvoid 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();}

( 鳴謝:http://wangyu.iteye.com/blog/190762)

雜湊:

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 stubdataStructure 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();}

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.