【说明】 链表和栈对象的共同特征是:在数据上执行的操作与在每个对象中实体存储的基本类型无关。例如,一个栈存储实体后,只要保证最后存储的项最先用,最先存储的项最后用,则栈的操作可以从链表的操作中派生得到。程序6-1实现了链表的操作,程序6-2实现了栈操作。 import java.io.*; class Node //定义结点 { private String m_content;private Node m_next;Node(String str){ m_content=str; m_next=null; }Node(String str,Node next){ m_content=str; m_next=next; }String getData()//获取结点数据域{ return m_content;}void setNext(Node next] //设置下一个结点值{ m_next=next;}Node getNext()//返回下一个结点{ return m_next; ) } 【程序6-1】 class List { Node Head;List(){ Head=null; }void insert(String str) //将数据str的结点插入在整个链表前面{ if(Head==null) Head=new Node(str); else (1) }void append(String str) //将数据str的结点插入在整个链表尾部{ Node tempnode=Head; it(tempnode==null) Heed=new Node(str); else { white(tempnode.getNext()!=null) (2) (3) }} String get() //移出链表第一个结点,并返回该结点的数据域 { Srting temp=new String(); if(Head==null) { System.out.println("Errow! from empty list!")System.exit(0); } else { temp=Head.getData(); (4) } return temp; } } 【程序6-2】 class Stack extends List { void push(String str)//进栈 { (5) } String pop()//出栈{ return get();} }