導航:首頁 > 編程語言 > tailjava實現

tailjava實現

發布時間:2024-04-23 13:30:57

java用數組實現隊列

1.1. 隊列的數據結構

隊列是一種特殊的線性表,特殊之處在於它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作,和棧一樣,隊列是一種操作受限制的線性表。進行插入操作的端稱為隊尾,進行刪除操作的端稱為隊頭。

1.2. Java實現

QueueTest

package ch04;

public class QueueTest {

public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(10);

System.out.println(queue.isEmpty());

for (int i = 0; i < 10; i++) {
queue.insert(i);
}
System.out.println(queue.isFull());

while (!queue.isEmpty()) {
System.out.println(queue.remove());
}
}

}

class ArrayQueue {
private int[] arrInt;// 內置數組
private int front;// 頭指針
private int rear;// 尾指針

public ArrayQueue(int size) {
this.arrInt = new int[size];
front = 0;
rear = -1;
}

/**
* 判斷隊列是否為空
*
* @return
*/
public boolean isEmpty() {
return front == arrInt.length;
}

/**
* 判斷隊列是否已滿
*
* @return
*/
public boolean isFull() {
return arrInt.length - 1 == rear;
}

/**
* 向隊列的隊尾插入一個元素
*/
public void insert(int item) {
if (isFull()) {
throw new RuntimeException("隊列已滿");
}
arrInt[++rear] = item;
}

/**
* 獲得對頭元素
*
* @return
*/
public int peekFront() {
return arrInt[front];
}

/**
* 獲得隊尾元素
*
* @return
*/
public int peekRear() {
return arrInt[rear];
}

/**
* 從隊列的對頭移除一個元素
*
* @return
*/
public int remove() {
if (isEmpty()) {
throw new RuntimeException("隊列為空");
}
return arrInt[front++];
}
}

運行結果如下:

false

true

0

1

2

3

4

5

6

7

8

9

⑵ java如何實現列印功能

Print.java--列印內容定義

[code]
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;

public class Print implements Printable{
int m_wPage;
int m_hPage;
int m_orientation;
Printable m_target;
int maxNumPage=1;
String title="數據表格列印";
Font titleFont=new Font("黑體",Font.BOLD,14);
boolean hasTail=true;
int tailAlign=0;
int headAlign=0;
int topSpace=0;
int leftSpace=0;

int yStart=0;
int yEnd=0;
int xStart=topSpace;
int xEnd=0;
int x=0,y=0;

String strTemp="列印內容";

public void doPrint(){
try{
m_orientation=PageFormat.PORTRAIT;
//設置列印對象,默認紙張
PrinterJob prnJob=PrinterJob.getPrinterJob();
PageFormat pageFormat=prnJob.defaultPage();
pageFormat.setOrientation(m_orientation);
m_wPage=(int)(pageFormat.getWidth());
m_hPage=(int)(pageFormat.getHeight());

//將待列印的窗體根據默認紙張設置傳入列印對象
prnJob.setPrintable(this,pageFormat);
if(!prnJob.printDialog()) return;
prnJob.print();
}catch(PrinterException ex){
ex.printStackTrace();
System.err.println("列印錯誤:"+ex.toString());
}
}
/**
* 初始化列印參數
*/
public void initPrintParameter()
{

}

/**
*構造列印內容,以送列印機列印
*/
public int print(Graphics pg,PageFormat pageFormat,
int pageIndex) throws PrinterException{
//初始化列印參數
initPrintParameter();

//將畫布設置為頁面大小
pg.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());
int wPage=0;
int hPage=0;

//根據列印機頁面設置調整畫布大小
if(pageFormat.getOrientation()==pageFormat.PORTRAIT){
wPage=(int)pageFormat.getImageableWidth();
hPage=(int)pageFormat.getImageableHeight();
}
else{
wPage=(int)pageFormat.getImageableWidth();
wPage+=wPage/2;
hPage=(int)pageFormat.getImageableHeight();
pg.setClip(0,0,wPage,hPage);
}
wPage=wPage-2*leftSpace;
hPage=hPage-2*topSpace;
xStart=leftSpace;
xEnd=wPage-2;

//為畫布設置顏色和字體
int y=topSpace;
pg.setFont(titleFont);
pg.setColor(Color.black);
//畫標題,並使其居中
Font fn=pg.getFont();
FontMetrics fm=pg.getFontMetrics();
y+=fm.getAscent();
alignText(title,pg,y,xStart,xEnd,headAlign);
y+=30;

x=leftSpace+2;

Font headerFont=new Font("宋體",Font.BOLD,14);
pg.setFont(headerFont);
fm=pg.getFontMetrics();

int h=fm.getAscent();
yStart=y-1;
y+=h;

pg.setFont(headerFont);
fm=pg.getFontMetrics();
int header=y;
h=fm.getHeight();

//計算行高,每頁行數,總行數和指定頁碼的起始行、結束行
int rowH=Math.max(h,10);
int tailH=rowH+30;
int rowPerPage=0;
int leftPix=0;
if(hasTail){
rowPerPage=(hPage-header-tailH)/rowH;
leftPix=(hPage-header-tailH)%rowH;
yEnd=hPage-leftPix-tailH+2;
}
else{
rowPerPage=(hPage-header)/rowH;
leftPix=(hPage-header)%rowH;
yEnd=hPage-leftPix+2;
}

pg.drawString(strTemp,x,y);

//畫表格邊框
pg.drawLine(xStart,yStart,xStart,yEnd);
pg.drawLine(xStart,yStart,xEnd,yStart);
pg.drawLine(xEnd,yStart,xEnd,yEnd);
pg.drawLine(xStart,yEnd,xEnd,yEnd);

//列印頁碼
if(hasTail){
int pageNumber=pageIndex+1;
String s="第"+pageNumber+"頁";
alignText(s,pg,yEnd+30,xStart,xEnd,tailAlign);
}
System.gc();
return PAGE_EXISTS;
}

/**
* 文字排列,坐標在y處,顯示範圍(start-end)
* 0表示居中顯示,1表示左對齊,2表示右對齊
*/
private void alignText(String s,Graphics pg,int y,int start,
int end,int mode){
Font fn=pg.getFont();
FontMetrics fm=pg.getFontMetrics();
int wString=fm.stringWidth(s);
int x=start;
switch(mode)
{
case 0:
if((end-start-wString)>0) x=start+(end-start-wString)/2;
break;
case 1:
break;
case 2:
if((end-start-wString)>0) x=start+(end-start-wString);
break;
}
pg.drawString(s,x,y);
}
public static void main(String[] args){
Print p=new Print();
p.doPrint();
}
}
[code]

運行方法:
>javac -d . Print.java
>java Print

自己運行一下

⑶ Java語言沒有指針,怎樣實現鏈表

Java語言中的對象引用實際上是一個指針(這里的指針均為概念上的意義,而非語言提供的數據類型),所以我們可以編寫這樣的類來實現鏈表中的結點。
程序代碼:
class Node
{
Object data;
Node next;//指向下一個結點
}
將數據域定義成Object類是因為Object類是廣義超類,任何類對象都可以給其賦值,增加了代碼的通用性。為了使鏈表可以被訪問還需要定義一個表頭,表頭必須包含指向第一個結點的指針和指向當前結點的指針。為了便於在鏈表尾部增加結點,還可以增加一指向鏈表尾部的指針,另外還可以用一個域來表示鏈表的大小,當調用者想得到鏈表的大小時,不必遍歷整個鏈表。
鏈表的數據結構我們可以用類List來實現鏈表結構,用變數Head、Tail、Length、Pointer來實現表頭。存儲當前結點的指針時有一定的技巧,Pointer並非存儲指向當前結點的指針,而是存儲指向它的前趨結點的指針,當其值為null時表示當前結點是第一個結點,因為當刪除當前結點後仍需保證剩下的結點構成鏈表,如果Pointer指向當前結點,則會給操作帶來很大困難。如何得到當前結點呢?我們定義了一個方法cursor(),返回值是指向當前結點的指針。類List還定義了一些方法來實現對鏈表的基本操作,通過運用這些基本操作我們可以對鏈表進行各種操作。例如reset()方法使第一個結點成為當前結點。insert(Object d)方法在當前結點前插入一個結點,並使其成為當前結點。remove()方法刪除當前結點同時返回其內容,並使其後繼結點成為當前結點,如果刪除的是最後一個結點,則第一個結點變為當前結點。
鏈表類List的源代碼如下:
package cn.javatx; import java.io.IOException;/**
* @author ljfan
*
*/
public class List {
private Node Head = null;
private Node Tail = null;
private Node Pointer = null;
private int Length = 0;public void deleteAll() {
Head = null;
Tail = null;
Pointer = null;
Length = 0;
}public void reset() {
Pointer = null;
}public boolean isEmpty() {
return (Length == 0);
}public boolean isEnd() {
if (Length == 0)
throw new java.lang.NullPointerException();
else if (Length == 1)
return true;
else
return (cursor() == Tail);
}public Object nextNode() {
if (Length == 1)
throw new java.util.NoSuchElementException();
else if (Length == 0)
throw new java.lang.NullPointerException();
else {
Node temp = cursor();
Pointer = temp;
if (temp != Tail)
return (temp.next.data);
else
throw new java.util.NoSuchElementException();
}
}public Object currentNode() {
Node temp = cursor();
return temp.data;
}public void insert(Object d) {
Node e = new Node(d);
if (Length == 0) {
Tail = e;
Head = e;
} else {
Node temp = cursor();
e.next = temp;
if (Pointer == null)
Head = e;
else
Pointer.next = e;
}
Length++;
}public int size() {
return (Length);
}public Object remove() {
Object temp;
if (Length == 0)
throw new java.util.NoSuchElementException();
else if (Length == 1) {
temp = Head.data;
deleteAll();
} else {
Node cur = cursor();
temp = cur.data;
if (cur == Head)
Head = cur.next;
else if (cur == Tail) {
Pointer.next = null;
Tail = Pointer;
reset();
} else
Pointer.next = cur.next;
Length--;
}
return temp;
}private Node cursor() {
if (Head == null)
throw new java.lang.NullPointerException();
else if (Pointer == null)
return Head;
else
return Pointer.next;
}public static void main(String[] args) {
List a = new List();
for (int i = 1; i <= 10; i++)
a.insert(new Integer(i));
System.out.println(a.currentNode());
while (!a.isEnd())
System.out.println(a.nextNode());
a.reset();
while (!a.isEnd()) {
a.remove();
}
a.remove();
a.reset();
if (a.isEmpty())
System.out.println("There is no Node in List n");
System.out.println("You can press return to quitn");
try {
System.in.read();
} catch (IOException e) {
}
}
}class Node {
Object data;
Node next;Node(Object d) {
data = d;
next = null;
}
}
當然,雙向鏈表基本操作的實現略有不同。鏈表和雙向鏈表的實現方法,也可以用在堆棧和隊列的實現中

⑷ Java語言沒有指針,怎樣實現鏈表

  1. Java語言中的對象引用實際上是一個指針(這里的指針均為概念上的意義,而非語言提供的數據類型),所以我們可以編寫這樣的類來實現鏈表中的結點。
    程序代碼:

  2. classNode
    {
    Objectdata;
    Nodenext;//指向下一個結點
    }
  3. 將數據域定義成Object類是因為Object類是廣義超類,任何類對象都可以給其賦值,增加了代碼的通用性。為了使鏈表可以被訪問還需要定義一個表頭,表頭必須包含指向第一個結點的指針和指向當前結點的指針。為了便於在鏈表尾部增加結點,還可以增加一指向鏈表尾部的指針,另外還可以用一個域來表示鏈表的大小,當調用者想得到鏈表的大小時,不必遍歷整個鏈表。
    鏈表的數據結構我們可以用類List來實現鏈表結構,用變數Head、Tail、Length、Pointer來實現表頭。存儲當前結點的指針時有一定的技巧,Pointer並非存儲指向當前結點的指針,而是存儲指向它的前趨結點的指針,當其值為null時表示當前結點是第一個結點,因為當刪除當前結點後仍需保證剩下的結點構成鏈表,如果Pointer指向當前結點,則會給操作帶來很大困難。如何得到當前結點呢?我們定義了一個方法cursor(),返回值是指向當前結點的指針。類List還定義了一些方法來實現對鏈表的基本操作,通過運用這些基本操作我們可以對鏈表進行各種操作。例如reset()方法使第一個結點成為當前結點。insert(Object d)方法在當前結點前插入一個結點,並使其成為當前結點。remove()方法刪除當前結點同時返回其內容,並使其後繼結點成為當前結點,如果刪除的是最後一個結點,則第一個結點變為當前結點。
    鏈表類List的源代碼如下:

  4. packagecn.javatx;importjava.io.IOException;/**
    *@authorljfan
    *
    */
    publicclassList{
    privateNodeHead=null;
    privateNodeTail=null;
    privateNodePointer=null;
    privateintLength=0;publicvoiddeleteAll(){
    Head=null;
    Tail=null;
    Pointer=null;
    Length=0;
    }publicvoidreset(){
    Pointer=null;
    }publicbooleanisEmpty(){
    return(Length==0);
    }publicbooleanisEnd(){
    if(Length==0)
    thrownewjava.lang.NullPointerException();
    elseif(Length==1)
    returntrue;
    else
    return(cursor()==Tail);
    }publicObjectnextNode(){
    if(Length==1)
    thrownewjava.util.NoSuchElementException();
    elseif(Length==0)
    thrownewjava.lang.NullPointerException();
    else{
    Nodetemp=cursor();
    Pointer=temp;
    if(temp!=Tail)
    return(temp.next.data);
    else
    thrownewjava.util.NoSuchElementException();
    }
    }publicObjectcurrentNode(){
    Nodetemp=cursor();
    returntemp.data;
    }publicvoidinsert(Objectd){
    Nodee=newNode(d);
    if(Length==0){
    Tail=e;
    Head=e;
    }else{
    Nodetemp=cursor();
    e.next=temp;
    if(Pointer==null)
    Head=e;
    else
    Pointer.next=e;
    }
    Length++;
    }publicintsize(){
    return(Length);
    }publicObjectremove(){
    Objecttemp;
    if(Length==0)
    thrownewjava.util.NoSuchElementException();
    elseif(Length==1){
    temp=Head.data;
    deleteAll();
    }else{
    Nodecur=cursor();
    temp=cur.data;
    if(cur==Head)
    Head=cur.next;
    elseif(cur==Tail){
    Pointer.next=null;
    Tail=Pointer;
    reset();
    }else
    Pointer.next=cur.next;
    Length--;
    }
    returntemp;
    }privateNodecursor(){
    if(Head==null)
    thrownewjava.lang.NullPointerException();
    elseif(Pointer==null)
    returnHead;
    else
    returnPointer.next;
    }publicstaticvoidmain(String[]args){
    Lista=newList();
    for(inti=1;i<=10;i++)
    a.insert(newInteger(i));
    System.out.println(a.currentNode());
    while(!a.isEnd())
    System.out.println(a.nextNode());
    a.reset();
    while(!a.isEnd()){
    a.remove();
    }
    a.remove();
    a.reset();
    if(a.isEmpty())
    System.out.println("ThereisnoNodeinListn");
    System.out.println("Youcanpressreturntoquitn");
    try{
    System.in.read();
    }catch(IOExceptione){
    }
    }
    }classNode{
    Objectdata;
    Nodenext;Node(Objectd){
    data=d;
    next=null;
    }
    }
  5. 當然,雙向鏈表基本操作的實現略有不同。鏈表和雙向鏈表的實現方法,也可以用在堆棧和隊列的現實中。

⑸ 用Java如何實現獲取linux控制台的輸出(分很多)

import java.io.*;

public class Linux {
public static void main(String[] args) throws IOException {
//將根目錄下的文件列出並將結果寫入 /tmp/list.out
Process p = Runtime.getRuntime().exec("ls -al /");
InputStream in = p.getInputStream();
OutputStream out = new FileOutputStream("/tmp/list.out");
byte[] b = new byte[1024];
int r;
while((r=in.read(b))>-1)
out.write(b,0,r);
out.flush();
out.close();
}
}

⑹ 用Java語言實現單向鏈表

1.先定義一個節點類

package com.buren;

public class IntNode {
//定義一個節點類

int
info;
//定義屬性,節點中的值
IntNode next;
//定義指向下一個節點的屬性

public IntNode(int
i){ //構造一個next為空的節點
this(i,null);
}

public IntNode(int i,IntNode
n){ //構造值為i指向n的節點
info=i;
next=n;
}

}

2.再定義一個鏈表類,這是主要部分

package com.buren;

public class IntSLList {

private IntNode head,tail;
//定義指向頭結點和尾結點的指針,
//如果大家看著這個不像指針的話,那就需要對指針有更深刻的了解

public
IntSLList(){
//定義一個空節點
head=tail=null;
}

public boolean
isEmpty(){
//判斷節點是否為空
return
head==null;
//這行代碼看起來似乎很神奇,其實真的很神奇,偶是服了
}

public void addToHead(int el){
//將el插入到頭結點前
head=new
IntNode(el,head);
//將節點插入到頭結點前,作為新的投節點
if(head==tail){
//給空鏈表插入節點時
tail=head;
//頭結點和尾結點指向同一個節點
}
}

public void addToTail(int
el){
//向鏈表的尾部增加結點
if(!isEmpty()){
//判斷鏈表是否為空
tail.next=new
IntNode(el);
//新建立一個值為el的節點,將鏈表的尾結點指向新節點
tail=tail.next;
//更新尾指針的指向
}else{
head=tail=new
IntNode(el);
//如果鏈表為空,新建立一個節點,將頭尾指針同時指向這個節點
}
}

public int
deleteFromHead(){
//刪除頭結點,將節點信息返回
int
el=head.info;
//取出節點信息
if(head==tail){
//如果鏈表中只有一個節點
head=tail=null;
//刪除這一個節點
}else{
head=head.next;
//如果鏈表中不止一個節點,將頭結點的下一個節點作為頭結點
}
return
el;
//返回原頭結點的值
}

public int
deleteFromTail(){
//刪除尾結點,返回尾結點的信息
int
el=tail.info;
//取出尾結點的值
if(head==tail){
// 如果鏈表中只有一個節點
head=tail=null;
//刪除這個節點
}else{
IntNode
temp;
//定義中間變數
for(temp=head;temp.next!=tail;temp=temp.next);
//找出尾結點的前一個節點,注意最後的分號,

//這個for循環是沒有循環體的,目的在於找出尾結點的前一個節點

//在整個程序中用了很多次這樣的寫法,相當經典啊
tail=temp;
//將找出來的節點作為尾結點,刪除原來的尾結點
tail.next=null;
//將新尾結點的指向設為空
}
return
el;
//返回原尾結點的信息
}

public void
printAll(){
//列印鏈表中所有節點的信息
if(isEmpty()){
//如果鏈表為空
System.out.println("This
list is
empty!");
//輸出提示信息
return;
//返回到調用的地方
}
if(head==tail){
//當鏈表中只有一個節點時
System.out.println(head.info);
//輸出這個節點的信息,就是頭結點的信息
return;
}
IntNode
temp;
//定義一個中間變數
for(temp=head;temp!=null;temp=temp.next){
//遍歷整個鏈表
System.out.print(temp.info+"
");
//輸出每個節點的信息
}
System.out.println();
//輸出一個換行,可以沒有這一行
}

public boolean isInList(int
el){
//判斷el是否存在於鏈表中
IntNode
temp;
//定義一個中間變數
for(temp=head;temp!=null
&&
temp.info!=el;temp=temp.next);
//將el找出來,注意最後的分
return
temp!=null;
// 如果存在返回true,否則返回flase,這兩行代碼很有思想
}

public void delete(int
el){
//刪除鏈表中值為el的節點
if(head.info==el
&&
head==tail){
//如果只有一個節點,並且節點的值為el
head=tail=null;
//刪除這個節點
}else
if(head.info==el){
// 不止一個節點,而頭結點的值就是el
head=head.next;
//刪除頭結點
}else{
IntNode
pred,temp;
//定義兩個中間變數
for(pred=head,temp=head.next;temp.info!=el
&&
temp.next!=null;pred=pred.next,temp=temp.next);
//跟上面的類似,自己琢磨吧,也是要注意最後的分號
pred.next=temp.next;
//將temp指向的節點刪除,最好畫一個鏈表的圖,有助於理解
if(temp==tail){
//如果temp指向的節點是尾結點
tail=pred;
//將pred指向的節點設為尾結點,
}
}
}

//下面這個方法是在鏈表中值為el1的節點前面插入一個值為el2的節點,
//用類似的思想可以再寫一個在鏈表中值為el1的節點後面插入一個值為el2的節點
public boolean insertToList(int el1,int
el2){
//定義一個插入節點的方法,插入成功返回true,否則返回false
IntNode
pred,temp; //定義兩個中間變數
if(isEmpty()){
//判斷鏈表是否為空
return
false;
//如果鏈表為空就直接返回false
}
if(head.info==el1
&&
head==tail){
//如果鏈表中只有一個節點,並且這個節點的值是el1
head=new
IntNode(el2,head);
//新建立一個節點
return
true;
}else if(head.info==el1){
IntNode t=new
IntNode(el2);
t.next=head;
head=t;
return
true;
}else{
for(pred=head,temp=head.next;temp!=null
&&
temp.info!=el1;pred=pred.next,temp=temp.next);
if(temp!=null){
IntNode
a=new IntNode(el2);
pred.next=a;
a.next=temp;
return
true;
}else{
System.out.println(el1+"
NOT EXEISTS!");
return
false;
}
}
}

3.下面是測試代碼
public static void main(String[] args){
IntSLList test=new
IntSLList();

//test.addToHead(7);
test.addToTail(7);

System.out.println(test.insertToList(7,5));
test.printAll();
System.out.println(test.isInList(123));
}
}

⑺ Java璇璦娌℃湁鎸囬拡錛屾庢牱瀹炵幇閾捐〃

閱讀全文

與tailjava實現相關的資料

熱點內容
大尺度電影韓劇 瀏覽:680
安卓手機怎麼聯接a 瀏覽:715
好色小姨 小說 瀏覽:677
網站的源碼怎麼使用 瀏覽:60
我的世界伺服器b2怎麼玩 瀏覽:581
付費電影免費看。 瀏覽:844
白領解壓培訓 瀏覽:577
密碼加密用在什麼地方 瀏覽:12
python教程100字 瀏覽:442
pdf小馬 瀏覽:982
馬雲入股伺服器 瀏覽:934
sdca哪個文件夾最好用 瀏覽:991
海貓電影網 瀏覽:32
程序員一天編程多少個小時 瀏覽:62
java與模式下載 瀏覽:649
javaintlong區別 瀏覽:688
刀塔2如何選擇中國伺服器 瀏覽:810
英文劇,7個孩子 瀏覽:246
哈利波特電影名英文名 瀏覽:51