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

留言簿

文章档案(50)

搜索

  •  

最新评论

习题1:编写从尾部添加节点的方法.


java 代码





  1. import java.io.*;   

  2. import java.util.*;   

  3.   

  4. class Node {   

  5.     int element;   

  6.     Node next;   

  7.     public Node(int i) {   

  8.         this(i, null);   

  9.     }   

  10.     public Node(int i, Node next) {   

  11.         this.element = i;   

  12.         this.next = next;   

  13.     }   

  14. }   

  15.   

  16. class LinkedList {   

  17.     public Node header;   

  18.     public LinkedList() {   

  19.         header = null;   

  20.     }   

  21.     public boolean isEmpty() {   

  22.         return header == null;   

  23.     }   

  24.     public void addtotail(int e) {   

  25.         if (!isEmpty()) {   

  26.             Node temp = header.next;   

  27.             if (temp == null) {   

  28.                 header.next = new Node(e);   

  29.             } else {   

  30.                 while (temp.next != null) {   

  31.                     temp = temp.next;   

  32.                 }   

  33.                 temp.next = new Node(e);   

  34.             }   

  35.         } else {   

  36.             header = new Node(e);   

  37.         }   

  38.     }   

  39.     public void printAll() {   

  40.         for (Node temp = header; temp != null; temp = temp.next) {   

  41.             System.out.print(temp.element + " ");   

  42.         }   

  43.     }   

  44. }   

  45. public class AddToTail {   

  46.     public static void main(String args[]) throws IOException {   

  47.         LinkedList list = new LinkedList();   

  48.         System.out.println("please input the number you want to insert to the tail");   

  49.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   

  50.         String s = br.readLine();   

  51.         StringTokenizer fen = new StringTokenizer(s, " ");   

  52.         int n = fen.countTokens();   

  53.         int m[] = new int[n];   

  54.         for (int i = 0; i < n; i++) {   

  55.             m[i] = Integer.parseInt(fen.nextToken());   

  56.             list.addtotail(m[i]);   

  57.         }   

  58.         list.printAll();   

  59.     }   

  60. }   



运行结果:


please input the number you want to insert to the tail

12 45 78 95 62 32 14

12 45 78 95 62 32 14


习题2:有La和Lb两个有序链表,要求生成新的链表Lc,使之有序.


java 代码





  1. import java.io.*;   

  2. import java.util.*;   

  3.   

  4. class Node {   

  5.     int element;   

  6.     Node next;   

  7.     public Node(int i) {   

  8.         this(i, null);   

  9.     }   

  10.     public Node(int i, Node next) {   

  11.         this.element = i;   

  12.         this.next = next;   

  13.     }   

  14. }   

  15. class LinkedList {   

  16.     public Node header;   

  17.     public LinkedList() {   

  18.         header = null;   

  19.     }   

  20.     public boolean isEmpty() {   

  21.         return header == null;   

  22.     }   

  23.     public void addtotail(int e) {   

  24.         if (!isEmpty()) {   

  25.             Node temp = header.next;   

  26.             if (temp == null) {   

  27.                 header.next = new Node(e);   

  28.             } else {   

  29.                 while (temp.next != null) {   

  30.                     temp = temp.next;   

  31.                 }   

  32.                 temp.next = new Node(e);   

  33.             }   

  34.         } else {   

  35.             header = new Node(e);   

  36.         }   

  37.     }   

  38.     public void printAll() {   

  39.         for (Node temp = header; temp != null; temp = temp.next) {   

  40.             System.out.print(temp.element + " ");   

  41.         }   

  42.     }   

  43. }   

  44. public class NewListC {   

  45.     public static void main(String args[]){   

  46.         LinkedList La=new LinkedList();   

  47.         LinkedList Lb=new LinkedList();   

  48.         LinkedList Lc=new LinkedList();   

  49.         for(int i=1; i<12; i=i+3){   

  50.             La.addtotail(i);   

  51.         }   

  52.         for(int i=3; i<25; i=i+4){   

  53.             Lb.addtotail(i);   

  54.         }   

  55.         System.out.print("链表A为: ");   

  56.         La.printAll();   

  57.         System.out.println();   

  58.         System.out.print("链表B为: ");   

  59.         Lb.printAll();   

  60.         Node tempA=La.header;   

  61.         Node tempB=Lb.header;   

  62.         while(tempA!=null){   

  63.             if(tempA.element

  64.                 Lc.addtotail(tempA.element);   

  65.                 tempA=tempA.next;   

  66.             }   

  67.             else if(tempA.element==tempB.element){   

  68.                 Lc.addtotail(tempA.element);   

  69.                 Lc.addtotail(tempB.element);   

  70.                 tempA=tempA.next;   

  71.                 tempB=tempB.next;   

  72.             }   

  73.             else{   

  74.                 Lc.addtotail(tempB.element);   

  75.                 tempB=tempB.next;   

  76.             }   

  77.         }   

  78.         while(tempB!=null){   

  79.             Lc.addtotail(tempB.element);   

  80.             tempB=tempB.next;   

  81.         }   

  82.         System.out.println();   

  83.         System.out.print("新生成的链表C为: ");   

  84.         Lc.printAll();   

  85.     }   

  86. }  



运行结果:


链表A为: 1 4 7 10

链表B为: 3 7 11 15 19 23

新生成的链表C为: 1 3 4 7 7 10 11 15 19 23


还有两个作业没有写完,


明天补充上去...


这是明天的任务...


要熄灯了,还有一件事情要留到明天去写,communication...






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





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



文章来源: http://xiaozhe.javaeye.com/blog/66203
posted on 2007-03-28 23:16 xiaozhe 阅读(79) 评论(0)  编辑  收藏

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


网站导航: