Java simple implementation of stacks and queues

Source: Internet
Author: User

Stack: This is an advanced data structure, a similar browser in life to return to the previous page can be implemented with this structure, the code is as follows:

 Public classStack<t> {    Privateobject[] data;//Storing Data    Private intTop//represents the top element of the stack     PublicStack () {Data=Newobject[ -];//to illustrate the principle of arbitrary designationTop =-1; }     PublicSynchronizedvoidPut (T-t) {//Press StackData[data.length] =T; Top++; }     PublicSynchronized T pop () {//out of the stack        if(top<0){            return NULL; } t T=(T) data[top]; Top--; returnT; }}

It's just that my own Java implementation only describes the principle, but actually in Java is the use of vectors to store data, we know that the vector is actually a thread-safe ArrayList, and the bottom of ArrayList is an array, so the principle is similar

Queue: Queue is actually our life in the queue phenomenon, first into the first out, after entering the back, the code is implemented as follows:

  

 Public classQueue<t> {    Privateobject[] data;//Storing Data    Private intHead//Head    Private intTail//Tail     PublicQueue () {Data=NewOBJECT[100];//to illustrate the principle of arbitrary designationHead =1; Tail=1; }     Public voidput (T-t) {Data[tail]=T; Tail++; }     Publict get () {T T=(T) data[head]; Head++; returnT; }}

As shown above, but also only to explain the principle, in fact, there are many different pairs of columns in the Java JDK, and the column can be implemented by the array, or can be implemented by the list, the following list

A linked list is a data structure similar to a string necklace in real life, the previous data points to the latter data, of course, the latter data can point to the previous data, this is a doubly linked list, here is no discussion

 Public classMynode<t> {    PrivateNode<t>Headnode;  PublicMynode (T t) {Headnode=NewNode<> (T,NULL); }     PublicMynode () {}Private Static classNode<e>{Object e; Node<E>Next; Node (E element, node<E>next) {             This. E =element;  This. Next =Next; }    }     Public voidput (T-t) {if(headnode==NULL) {Headnode=NewNode<> (T,NULL); }Else{Headnode=NewNode<>(T,headnode); }    }     PublicT Get (intindex) {Node Node=Headnode;  for(inti=2;i<index;i++) {node=Node.next; } t T=(T) node.e; returnT; }}

  

The above is a simple list of Java implementation, and a doubly linked list is an element pointing to the previous element and the latter element, the principle is probably the same, here you can read the list of fixed position data is very troublesome, must start from the first to find, this is linklist do not use for (int i=0 I++;i<size ()), because the bottom of the linklist is implemented through a linked list.

Java simple implementation of stacks and queues

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.