class StackT {

10多年的同德網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整同德建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“同德網(wǎng)站設(shè)計”,“同德網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
private VectorT v;
public Stack(){
v = new VectorT();
}
public T pop(){
if (v.size()==0) return null;
return v.get(v.size()-1);
}
public void push(T t){
v.add(t);
}
public boolean isEmpty(){
return v.size()==0;
}
}
class QueueT{
private VectorT v;
public Queue(){
v = new VectorT();
}
//入隊列
public void enqueue(T t){
v.add(t);
}
//出隊列
public T dequeue(){
if (v.size()==0) return null;
return v.get(0);
}
public boolean isEmpty(){
return v.size() == 0;
}
}
具體代碼如下:
以下是兩個線程:
import java.util.*;
public class Thread_List_Operation {
//假設(shè)有這么一個隊列
static List list = new LinkedList();
public static void main(String[] args) {
Thread t;
t = new Thread(new T1());
t.start();
t = new Thread(new T2());
t.start();
}
}
//線程T1,用來給list添加新元素
class T1 implements Runnable{
void getElemt(Object o){
Thread_List_Operation.list.add(o);
System.out.println(Thread.currentThread().getName() + "為隊列添加了一個元素");
}
@Override
public void run() {
for (int i = 0; i 10; i++) {
getElemt(new Integer(1));
}
}
}
//線程T2,用來給list添加新元素
class T2 implements Runnable{
void getElemt(Object o){
Thread_List_Operation.list.add(o);
System.out.println(Thread.currentThread().getName() + "為隊列添加了一個元素");
}
@Override
public void run() {
for (int i = 0; i 10; i++) {
getElemt(new Integer(1));
}
}
}
//結(jié)果(亂序)
Thread-0為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
public class QueueE {
private Object[] data=null;
private int maxSize; //隊列容量
private int front; //隊列頭,允許刪除
private int rear; //隊列尾,允許插入
//構(gòu)造函數(shù)
public Queue(){
this(10);
}
public Queue(int initialSize){
if(initialSize =0){
this.maxSize = initialSize;
data = new Object[initialSize];
front = rear =0;
}else{
throw new RuntimeException("初始化大小不能小于0:" + initialSize);
}
}
//判空
public boolean empty(){
return rear==front?true:false;
}
//插入
public boolean add(E e){
if(rear== maxSize){
throw new RuntimeException("隊列已滿,無法插入新的元素!");
}else{
data[rear++]=e;
return true;
}
}
//返回隊首元素,但不刪除
public E peek(){
if(empty()){
throw new RuntimeException("空隊列異常!");
}else{
return (E) data[front];
}
}
//出隊
public E poll(){
if(empty()){
throw new RuntimeException("空隊列異常!");
}else{
E value = (E) data[front]; //保留隊列的front端的元素的值
data[front++] = null; //釋放隊列的front端的元素
return value;
}
}
//隊列長度
public int length(){
return rear-front;
}
}
當(dāng)前題目:java隊列代碼,java隊列代碼實現(xiàn)
鏈接地址:http://chinadenli.net/article16/dsgjddg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、做網(wǎng)站、虛擬主機(jī)、手機(jī)網(wǎng)站建設(shè)、建站公司、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)