随笔 - 0  文章 - 3  trackbacks - 0
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

留言簿

文章档案(50)

搜索

  •  

最新评论



java 代码




  1. class IntNode {   

  2.     public int info; // 记录信息   

  3.   

  4.     public IntNode next;// 指向下一节点的指针   

  5.   

  6.     public IntNode(int i) { // 创建一个节点并把它的info定义并且把它的下一个指   

  7.         // 针指向null   

  8.         this(i, null);   

  9.     }   

  10.   

  11.     public IntNode(int i, IntNode n) { // 这是初使化的地点,this(i,null)就指它   

  12.         info = i;   

  13.         next = n;   

  14.     }   

  15. }   

  16.   

  17. // 定义完毕   

  18.   

  19. // IntSLList.java   

  20. public class IntSLList {   

  21.     private IntNode head, tail; // 定义指向头和尾的指针   

  22.   

  23.     public IntSLList() {   

  24.         head = tail = null// 定义一开始使head和tail都为空,定义一个空链表   

  25.     }   

  26.   

  27.     public boolean isEmpty() { // 判断链表是否为空,这算法偶是佩服了!   

  28.         return head == null;   

  29.     }   

  30.   

  31.     public void addToHead(int el) { // 创建头指针,该方法只用一次!   

  32.         head = new IntNode(el, head); // 此时head的指为null进而初使化,我看为   

  33.         // 了节约代码才这么写!要不也可以写成head=new IntNode(el);   

  34.         // 拥有了指向第一个元素的指针咯!   

  35.         if (tail == null// 若只有一个元素的化那么其尾指针指向头指针   

  36.             tail = head;// 第一次初使化的时候尾也有了!头也是它尾也是它!   

  37.     }   

  38.   

  39.     public void addToTail(int el) { // 添加尾指针,该方法使用多次   

  40.         if (!isEmpty()) { // 若链表非空那么将尾指针的next初使化为一个新的元素   

  41.             tail.next = new IntNode(el); // 然后将尾指针指向现在它自己的下一个元素   

  42.             tail = tail.next;   

  43.         } else { // 如果为空则创建一个新的!并将头尾同时指向它!   

  44.             head = tail = new IntNode(el);// 这句可以有多种写法   

  45.         }   

  46.     }   

  47.   

  48.     public int deleteFromHead() { // 删除头并且返回它的信息   

  49.         int el = head.info;   

  50.         if (head == tail) { // 如果只有一个元素或没有元素则一律清空为空链表   

  51.             head = tail = null;   

  52.         } else  

  53.             head = head.next; // 否则就把头指针后移   

  54.         return el;   

  55.     }   

  56.   

  57.     public int deleteFromTail() { // 删除尾并返回它的info   

  58.         int el = tail.info;   

  59.         if (head == tail) // 和上面一样   

  60.             head = tail = null;   

  61.         else { // 如果不是上面那种情况就把tail设置到原来tail的上一个元素   

  62.             IntNode temp; // 这个是临时的   

  63.             for (temp = head; temp.next == tail; temp = temp.next)   

  64.                 // 这循环很特殊咯!   

  65.                 tail = temp; // 循环的目的自己想   

  66.             tail.next = null// 把原来的尾设置为null   

  67.         }   

  68.         return el; // 返回   

  69.     }   

  70.   

  71.     public void printAll() { // 输出所有的元素竟然没用Iterator强!   

  72.         for (IntNode temp = head; temp != null; temp = temp.next)   

  73.             System.out.println(temp.info + " ");   

  74.     }   

  75.   

  76.     public boolean isInList(int el){ //判断链表内是否有元素的info与参数相等   

  77.       IntNode temp;   // 有个缺陷!只能判断出离head进的那一个!有多个就不行   

  78.       for(temp=head;temp!=null&&temp.info!=el;temp=temp.next);// 仔细理解   

  79.       return temp!=null;                           // 这个分号的作用   

  80.     }   

  81.   

  82.     public void delete(int el) { // 通过el删除元素(有缺陷)   

  83.         if (!isEmpty()) // 若非空并且只有一个元素且头元素的info和el等   

  84.             if (head == tail && head.info == el) // 那么就删除头   

  85.                 head = tail = null;   

  86.             else if (el == head.info) // 若非空并且el和头元素的info相等那么删除头   

  87.                 head = head.next;   

  88.             else {   

  89.                 IntNode pred, temp;   

  90.                 for (pred = head, temp = head.next; temp != null  

  91.                         && temp.info != el; pred = pred.next, temp = temp.next);// 又是分号咯!判断其el是否在表内!   

  92.                 if (temp != null) { // 若非空则删除元素   

  93.                     pred.next = temp.next;   

  94.                     if (temp == tail) // 若为尾则删除尾   

  95.                         tail = pred;   

  96.                 }   

  97.             }   

  98.     }   

  99. }  







评论也很精彩,请点击查看精彩评论。欢迎您也添加评论。查看详细 >>





JavaEye推荐
北京:优秀公司NHNChina招聘:WEB开发,系统管理,JAVA开发, DBA
广州:急招 JAVA开发经理/系统架构师(10-15K/月)也招聘java程序员
与Hibernate之父面对面-4月19日 Gavin King上海交流研讨会
高薪工作机会 美国法国上海 15-20k/月 J2EE SA



文章来源: http://xiaozhe.javaeye.com/blog/59214
posted on 2007-03-12 10:15 xiaozhe 阅读(88) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: