A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王琪 中级黑马   /  2014-3-10 19:56  /  765 人查看  /  2 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王琪 于 2014-3-10 20:08 编辑
  1. <b>package dom;
  2. import dom.IntSLLNode;
  3. public class IntSLList {
  4.         protected IntSLLNode head, tail;
  5.         public IntSLList(){
  6.                 head=tail=null;
  7.         }
  8.         public boolean isEmpty(){
  9.                 return head==null;
  10.         }
  11.         public void addTOHead(int e1){
  12.                 head=new IntSLLNode(e1,head);
  13.                 if(tail==null){
  14.                         tail=head;
  15.                 }
  16.         }
  17.         public void addTotail(int e1){
  18.                 if(!isEmpty()){
  19.                         tail.next=new IntSLLNode(e1);
  20.                         tail=tail.next;
  21.                 }
  22.                 else head=tail=new IntSLLNode(e1);
  23.         }
  24.         public int deleteFromHead(){
  25.                 int e1=head.info;
  26.                 if(head==tail)
  27.                                 head=tail=null;
  28.                 else head=head.next;
  29.                 return e1;
  30.         }
  31.         public int deleteFromTail(){
  32.                 int e1=tail.info;
  33.                 if(head==tail)
  34.                                 head=tail=null;
  35.                 else{
  36.                         IntSLLNode tmp;
  37.                         for(tmp=head;tmp.next!=tail;tmp=tmp.next);
  38.                         tail=tmp;
  39.                         tail.next=null;
  40.                 }
  41.                 return e1;
  42.         }
  43.         public void printAll(){
  44.                 for(IntSLLNode tmp=head ;tmp!=null ;tmp=tmp.next ){
  45.                         System.out.println(tmp.info+" ");
  46.                 }
  47.         }
  48.         public boolean isInList(int e1){
  49.                 IntSLLNode tmp;
  50.                 for(tmp=head ;tmp!=null&&tmp.info!=e1 ;tmp=tmp.next );
  51.                         return tmp!=null;
  52.         }
  53.         public void delete(int e1){
  54.                 if(!isEmpty()){
  55.                         if(head==tail&&e1==head.info){
  56.                                 head=tail=null;
  57.                         }
  58.                         else if(e1==head.info){
  59.                                 head=head.next;
  60.                         }
  61.                         else{
  62.                                 IntSLLNode pred,tmp;
  63.                                 for(pred=head ,tmp=head.next ;tmp!=null&&tmp.info!=e1 ;pred=pred.next,tmp=tmp.next );
  64.                                 if(tmp!=null){
  65.                                         pred.next=tmp.next;
  66.                                         if(tmp==tail)
  67.                                                 tail=pred;
  68.                                 }
  69.                         }
  70.                 }
  71.         }
  72. }
  73. </b>



复制代码
  1. package dom;

  2. import org.junit.Test;

  3. public class IntSLLNode {
  4.         public int info;
  5.         public IntSLLNode next;
  6.         public IntSLLNode(int i){
  7.                 this(i,null);
  8.         }
  9.         public IntSLLNode(int i,IntSLLNode s){
  10.                 this.info=i;
  11.                 this.next=s;
  12.         }
  13.                
  14. }
复制代码








评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

2 个回复

倒序浏览
谢谢楼主分享
回复 使用道具 举报
链表的查找



  1. public class SLLNode {
  2.         public Object info;
  3.         public SLLNode next;
  4.         public SLLNode(){
  5.                 next=null;
  6.         }
  7.         public SLLNode(Object e1){
  8.                 info=e1;
  9.                 next=null;
  10.         }
  11.         public SLLNode(Object e1,SLLNode otr){
  12.                 info=e1;
  13.                 next=otr;
  14.         }
  15. }
复制代码

  1. public class SLList {
  2.         protected SLLNode head=null;
  3.         public SLList(){
  4.         }
  5.         public boolean isEmpty(){
  6.                 return head==null;
  7.         }
  8.         public Object first(){
  9.                 return head.info;
  10.         }
  11.         public void printAll(java.io.PrintStream out){
  12.                 for(SLLNode tmp=head ;tmp!=null ;tmp=tmp.next )
  13.                 out.print(tmp.info+" ");
  14.         }
  15.         public void add(Object e1){
  16.                 head=new SLLNode(e1.toString(),head);
  17.         }
  18.         public Object find(Object e1){
  19.                 SLLNode tmp=head;
  20.                 for( ;tmp!=null&&!e1.equals(tmp.info) ;tmp=tmp.next );
  21.                 if(tmp==null){
  22.                         return null;
  23.                 }
  24.                 else{
  25.                         return tmp.info;
  26.                 }
  27.         }
  28.         public Object deleteHead(){
  29.                 Object e1=head.info;
  30.                 head=head.next;
  31.                 return e1;
  32.         }
  33.         public void delete(Object e1){
  34.                 if(head!=null){
  35.                         if(e1.equals(head.info)){
  36.                                 head=head.next;
  37.                         }
  38.                         else{
  39.                                 SLLNode pred=head,tmp=head.next;
  40.                                 for( ;tmp!=null&&!(tmp.info.equals(e1)) ;pred=pred.next,tmp=tmp.next ){
  41.                                         pred=pred.next;
  42.                                         tmp=tmp.next;
  43.                                 }
  44.                                 if(tmp!=null){
  45.                                         pred.next=tmp.next;
  46.                                 }
  47.                         }
  48.                 }
  49.         }
  50. }

复制代码






回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马