Java is very inconvenient to use because of the generic eraseAbstract classBasequeue{ AbstractBoolean enQueue (T x); AbstractT front (t x);//Note that There's a difference between this and C + +, which directly returns the header element AbstractBoolean Delqueue (); Abstract voidClear ();}classQueue{ intmmaxsize,mindexf,mindexr; T []mqueue; @SuppressWarnings ("unchecked") Queue (ClassintMaxSize)
First, the containerMany times, the program always creates new objects based on certain conditions that the runtime knows about, and before that the number of objects required, and even the type of the object, is required to hold the object. The Java container class can automatically adjust its size. A container class library can be divided into two different concepts:
Collection. A sequence of independent elements, such as list (which saves
; } } Public int Pop() {//Two stacks are empty, no elements can pop up if(Queue1.isempty () queue2.isempty ()) {Try{Throw NewException ("Stack is empty"); }Catch(Exception e) { } }//If queue1 is empty, Queue2 has elements, queue2 elements are placed in queue1, until the last element, we pop up. if(Queue1.isempty ()) { while(Queue2.size () >1) {Queue1.add (Queue2.poll ()); }returnQueue2.poll (); }if(Queue2.isempty ()) { while(Queue1.size () >1) {Queue2.add (Que
IdeasWhen you queue, press the element into the S1. when out of the team, determine whether S2 is empty, if not empty, the top element is directly ejected, if empty, S1 elements are "poured" S2, the last element ejected and out of the team. Public classQuenewithtwostacks {PrivateStackStack1; PrivateStackStack2; Publicquenewithtwostacks () {Stack1=NewStack(); Stack2=NewStack(); } PublicInteger Poll () {if(Stack1.isempty () stack2.isempty ())r
Collection. A sequence of independent elements that obey one or more rules. The list must save the element in the order in which it was inserted, and set cannot have duplicate elements. Map. A pair of key-value pairs of objects that allow keys to be used to look up values. There are 2 types of lists: basic ArrayList, longer than random-access elements, but slower when inserting and removing elements in the middle of a list. LinkedList, it provides optimized sequential access by inserting and del
Day14-09Shortlist Stack first-in-first-out, like a cup; The queue first-in-first-out Fisrt is like a pipe.
Removelast; removefirst
Package test. bixiangdong; import Java. util. shortlist; public class test {private shortlist link; test () {link = new shortlist () ;}// add an elementpublic void add (Object OBJ) {link. add (OBJ);} // first in first out to get an elementpublic object get () {return link
Down-Stack (linked list implementation):Import Java.util.iterator;public class LinkedstackFIFO Queue (linked list implementation):Import Java.util.iterator;public class Linkedqueue Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Algorithm (fourth edition) Java implementation stack and queue for learning
37Attention:Here to pay attention to the title of the one or two. Refers to all people in order after the completion of the report, and then 123. If you have:First time off one or two: Staff: 1 2 3 4 5 6 7 8 9 10Count off: 1 2 1 2 1 2 1 2 1 2Second off 123: Staff: 1 3 5 7 9Count off: 1 2 3 1 2So loop the above two steps. Know the number of persons not exceeding three.Another detail is in the output is, control the formatting problem, the last one cannot have a space behindCode:Import Java.util.
implemented.VI. implementation of the Code1 /**2 * method for direct insertion of sorting3 * @paraman array that needs to be sorted4 */5 Public voidInsertsort (int[] a) {6 //iterates over all elements except the first element, inserting an ordered area of element one by one in the initial unordered region7 for(inti = 1; i ) {8 //Storing insert elements9 inttemp =A[i];Ten //define J Variables and initialize, j variables for inse
Package Collection;import Java.util.linkedlist;/*linkedlist: Unique method AddFirst ()/addlast (); GetFirst ()/getlast (); Removefirst ()/removelast (); If the list is empty, throw no exception to this element/nosuchelementexception but the JDK1.6 version has an alternative method: Offerfirst ()/offerlast () ;p Eekfirst ()/peeklast () Pollfirst ()/polllast (): Returns the Null*/public class Linklistdemo{public static void main If the list is empty ( String[] args) {linkedlist
PublicItem Next () {Item Item=Current.item; Current=Current.next; returnitem; } }}//List -implemented queuesImportJava.util.Iterator; Public classQueueImplementsIterable { PrivateNode first;//Defining a table header PrivateNode last;//Define footer Private intN//define the number of elements Private classNode/*Defining node Classes*/{item item; Node Next; } Public BooleanIsEmpty () {returnfirst==NULL;} Public intSize () {returnN;} Public voidEnquene (item item) {//ad
Stackelement see the implementation of the stackPackage Com.lip.datastruture.stack;public class QueueOperation Result:Implementation of the queue of data structures (JAVA)
package multithreading concurrency; import java.util.stack;/** * Created by z84102272 on 2018/7/17. */public class Blockqueueimpl {private final static Object Pushlock = new Object (); Push lock private Final static Object Poplock = new Object (); The lock of the pop private stack Java Implementation blocking queue
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.