Java用鏈表實現棧和隊列

來源:互聯網
上載者:User

標籤:棧   隊列   

1、用鏈表實現棧

package stack;/** *  * @author denghb * */class Link {public long dData;public Link next;public Link(long dd) {dData = dd;}public void displayLink() {System.out.print(dData + " ");}}class LinkList {private Link first;    public LinkList() {    first = null;    }    public boolean isEmpty() {    return (first == null);    }    public void insertFirst(long dd) {    Link newLink = new Link(dd);    newLink.next = first;    first = newLink;    }    public long deleteFirst() {    //假設這是一個非空鏈表    Link temp = first;    first = first.next;    return temp.dData;    }    public void displayList() {    Link current = first;    while(current != null) {    current.displayLink();    current = current.next;    }    System.out.println("");    }}class LinkStack {private LinkList theList;public LinkStack() {theList = new LinkList();}public void push(long j) {theList.insertFirst(j);}public long pop() {return theList.deleteFirst();}public boolean isEmpty() {return (theList.isEmpty());}public void displayStack() {System.out.print("Stack (top-->bottom): ");theList.displayList();}}public class LinkStackApp {public static void main(String[] args) {LinkStack theStack = new LinkStack();theStack.push(20);theStack.push(40);theStack.displayStack();theStack.push(60);theStack.push(80);theStack.displayStack();theStack.pop();theStack.pop();theStack.displayStack();}}

2、用鏈表實現隊列

package queue;/** *  * @author denghb * */class Link {public long dData;public Link next;public Link(long dd) {dData = dd;}public void displayLink() {System.out.print(dData + " ");}}class FirstLastList {private Link first;private Link last;    public FirstLastList() {    first = null;    last = null;    }    public boolean isEmpty() {    return (first == null);    }    public void insertLast(long dd) {    Link newLink = new Link(dd);    if(isEmpty()) {    first = newLink;    } else {    last.next = newLink;    }    last = newLink;    }    public long deleteFirst() {    //假設這是一個非空鏈表    long temp = first.dData;    if(first.next == null) {  //如果只有一個連結點    last = null;    }    first = first.next;    return temp;    }    public void displayList() {    Link current = first;    while(current != null) {    current.displayLink();    current = current.next;    }    System.out.println("");    }}class LinkQueue {private FirstLastList theList;public LinkQueue() {theList = new FirstLastList();}public void insert(long j) {theList.insertLast(j);}public long remove() {return theList.deleteFirst();}public boolean isEmpty() {return (theList.isEmpty());}public void displayStack() {System.out.print("Stack (top-->bottom): ");theList.displayList();}}public class LinkQueueApp {public static void main(String[] args) {LinkQueue theQueue = new LinkQueue();theQueue.insert(20);theQueue.insert(40);theQueue.displayStack();theQueue.insert(60);theQueue.insert(80);theQueue.displayStack();theQueue.remove();theQueue.remove();theQueue.displayStack();}}

第一個來源程式的輸出結果:

Stack (top-->bottom): 40 20 
Stack (top-->bottom): 80 60 40 20 
Stack (top-->bottom): 40 20 

第二個來源程式的輸出結果:

Stack (top-->bottom): 20 40 
Stack (top-->bottom): 20 40 60 80 
Stack (top-->bottom): 60 80 

Java用鏈表實現棧和隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.