本帖最后由 王琪 于 2014-3-10 20:08 编辑
- <b>package dom;
- import dom.IntSLLNode;
- public class IntSLList {
- protected IntSLLNode head, tail;
- public IntSLList(){
- head=tail=null;
- }
- public boolean isEmpty(){
- return head==null;
- }
- public void addTOHead(int e1){
- head=new IntSLLNode(e1,head);
- if(tail==null){
- tail=head;
- }
- }
- public void addTotail(int e1){
- if(!isEmpty()){
- tail.next=new IntSLLNode(e1);
- tail=tail.next;
- }
- else head=tail=new IntSLLNode(e1);
- }
- public int deleteFromHead(){
- int e1=head.info;
- if(head==tail)
- head=tail=null;
- else head=head.next;
- return e1;
- }
- public int deleteFromTail(){
- int e1=tail.info;
- if(head==tail)
- head=tail=null;
- else{
- IntSLLNode tmp;
- for(tmp=head;tmp.next!=tail;tmp=tmp.next);
- tail=tmp;
- tail.next=null;
- }
- return e1;
- }
- public void printAll(){
- for(IntSLLNode tmp=head ;tmp!=null ;tmp=tmp.next ){
- System.out.println(tmp.info+" ");
- }
- }
- public boolean isInList(int e1){
- IntSLLNode tmp;
- for(tmp=head ;tmp!=null&&tmp.info!=e1 ;tmp=tmp.next );
- return tmp!=null;
- }
- public void delete(int e1){
- if(!isEmpty()){
- if(head==tail&&e1==head.info){
- head=tail=null;
- }
- else if(e1==head.info){
- head=head.next;
- }
- else{
- IntSLLNode pred,tmp;
- for(pred=head ,tmp=head.next ;tmp!=null&&tmp.info!=e1 ;pred=pred.next,tmp=tmp.next );
- if(tmp!=null){
- pred.next=tmp.next;
- if(tmp==tail)
- tail=pred;
- }
- }
- }
- }
- }
- </b>
复制代码- package dom;
- import org.junit.Test;
- public class IntSLLNode {
- public int info;
- public IntSLLNode next;
- public IntSLLNode(int i){
- this(i,null);
- }
- public IntSLLNode(int i,IntSLLNode s){
- this.info=i;
- this.next=s;
- }
-
- }
复制代码
|