Java-based Data Structure: linked list, stack, queue, priority queue, and hash table

Source: Internet
Author: User

I recently reviewed Java when I was looking for a job. After reading the books and online textbooks, I found that although many books use Java to talk about data structures, they focus on the knowledge of data structures, using Java class libraries to implement data structures is rarely explained in a systematic manner, so I would like to make a summary here to make it easier for anyone who is preparing and using Java to implement data structures.

Attachment: Code download: http://download.csdn.net/detail/sunnyskyliu/4754827

STACK:

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

Queue:

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

Priority queue:

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

Linked List:

// Link listvoid m_linklist () {Response List <string> head = new Response List <string> (); head. add ("1"); head. add ("3"); head. add (1, "2"); // The contents Table automatically prints all content systems in the same way. out. println (head); system. out. println ("size is:" + head. size (); // Add the numeric vector <string> V = new vector <string> () to the values table using collections such as vector or arraylist; // need to import Java. util. vector; // struct list <string> V = new struct list <string> (); // Add things V to the vector. add ("A"); V. add ("B"); // Add the prefix vector to the last head of the forward vertex table. addall (V); system. out. println (head); // Add the first vector to the specified position of the first vertex table. addall (2, V); system. out. println (head); // print the content system at the specified position. 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); // remove the first head. remove (head. getfirst (); head. remove (head. getlast (); system. out. println (head); // remove the first one, and use the lower mark method. The lower Mark starts from 0. remove (0); system. out. println (head); system. out. println ();}

(Thanks: http://wangyu.iteye.com/blog/190762)

Hash:

Void m_hash () {hashtable <string, integer> H = new hashtable <string, integer> (); H. put ("user 1", new INTEGER (90); H. put ("user 2", new INTEGER (50); H. put ("user 3", new INTEGER (60); H. put ("user 4", new INTEGER (70); H. put ("user 1", new INTEGER (80); system. out. println ("user 1:" + H. get ("user 1");/* // enumerate 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 call:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.