ADT(抽象数据类型):泛泛的说,就是将类(数据类型)进行高度抽象.着重于他做了什么而忽略了他是怎么做的.
即下面两类用链表分别模拟栈(Stack)和队列(Queue).
栈: FILO先进后出.拥有push(入栈)和pop(出栈)
队列: FIFO先进先出.拥有insert(入列)和remove(出列).
ADT即是将链表方法进行封装.让使用者只关心本身方法使用,而不关心具体的实现方式.
模拟的代码分别如下:
(一)栈模拟
LinkStack
package cn.com.linkstack;
class Link {
public double dData;
public Link next;
public Link(double dd) {
dData = dd;
}
public void displayLink() {
System.out.print("{" + dData + "}");
}
}
class LinkList {
public Link first;
public LinkList() {
first = null;
}
public boolean isEmpty () {
return (first == null);
}
public void insertFirst(double dd){
Link newLink = new Link(dd);
newLink.next = first;
first = newLink;
}
public void deleteFirst(){
first = first.next;
}
public void displayList() {
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
}
}
class LinkStack {
LinkList theList;
public LinkStack() {
theList = new LinkList();
}
// 入栈
public void push(double dd){
theList.insertFirst(dd);
}
// 出栈
public void pop(){
theList.deleteFirst();
}
public boolean isEmpty () {
return theList.isEmpty();
}
public void displayStack(){
System.out.print("Stack (top ---> bottom)");
theList.displayList();
System.out.println();
}
}
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();
}
}
执行结果:先进后出
Stack (top ---> bottom){40.0}{20.0}
Stack (top ---> bottom){80.0}{60.0}{40.0}{20.0}
Stack (top ---> bottom){40.0}{20.0}
(二)模拟队列
LinkQueue
package cn.com.queuestack;
class Link {
public double dData;
public Link next;
public Link(double dd){
dData = dd;
}
public void displayLink(){
System.out.print("{" + dData + "}");
}
}
class FirstLastList {
public Link first;
public Link last;
public FirstLastList() {
first = null;
last = null;
}
public boolean isEmpty(){
return (first == null);
}
public void insertLast(double dd) {
Link newLink = new Link(dd);
if(isEmpty()){
first = newLink;
}else {
last.next = newLink;
}
last = newLink;
}
public void deleteFirst(){
if(first.next == null) {
last = null;
}
first = first.next;
}
public void displayList() {
Link current = first;
while(current != null) {
current.displayLink();
current = current.next;
}
}
}
class LinkQueue {
FirstLastList theList;
public LinkQueue() {
theList = new FirstLastList();
}
public boolean isEmpty() {
return theList.isEmpty();
}
// 入列
public void insert(double dd) {
theList.insertLast(dd);
}
// 出列
public void remove() {
theList.deleteFirst();
}
public void displayQueue(){
System.out.print("Queue (front ---> rear)");
theList.displayList();
System.out.println();
}
}
public class LinkQueueApp
{
public static void main(String[] args)
{
LinkQueue theQueue = new LinkQueue();
theQueue.insert(20);
theQueue.insert(40);
theQueue.displayQueue();
theQueue.insert(60);
theQueue.insert(80);
theQueue.displayQueue();
theQueue.remove();
theQueue.remove();
theQueue.displayQueue();
}
}
执行结果:先进先出
Queue (front ---> rear){20.0}{40.0}
Queue (front ---> rear){20.0}{40.0}{60.0}{80.0}
Queue (front ---> rear){60.0}{80.0}