习题1:编写从尾部添加节点的方法.
java 代码
- import java.io.*;
- import java.util.*;
-
- class Node {
- int element;
- Node next;
- public Node(int i) {
- this(i, null);
- }
- public Node(int i, Node next) {
- this.element = i;
- this.next = next;
- }
- }
-
- class LinkedList {
- public Node header;
- public LinkedList() {
- header = null;
- }
- public boolean isEmpty() {
- return header == null;
- }
- public void addtotail(int e) {
- if (!isEmpty()) {
- Node temp = header.next;
- if (temp == null) {
- header.next = new Node(e);
- } else {
- while (temp.next != null) {
- temp = temp.next;
- }
- temp.next = new Node(e);
- }
- } else {
- header = new Node(e);
- }
- }
- public void printAll() {
- for (Node temp = header; temp != null; temp = temp.next) {
- System.out.print(temp.element + " ");
- }
- }
- }
- public class AddToTail {
- public static void main(String args[]) throws IOException {
- LinkedList list = new LinkedList();
- System.out.println("please input the number you want to insert to the tail");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String s = br.readLine();
- StringTokenizer fen = new StringTokenizer(s, " ");
- int n = fen.countTokens();
- int m[] = new int[n];
- for (int i = 0; i < n; i++) {
- m[i] = Integer.parseInt(fen.nextToken());
- list.addtotail(m[i]);
- }
- list.printAll();
- }
- }
运行结果:
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 代码
- import java.io.*;
- import java.util.*;
-
- class Node {
- int element;
- Node next;
- public Node(int i) {
- this(i, null);
- }
- public Node(int i, Node next) {
- this.element = i;
- this.next = next;
- }
- }
- class LinkedList {
- public Node header;
- public LinkedList() {
- header = null;
- }
- public boolean isEmpty() {
- return header == null;
- }
- public void addtotail(int e) {
- if (!isEmpty()) {
- Node temp = header.next;
- if (temp == null) {
- header.next = new Node(e);
- } else {
- while (temp.next != null) {
- temp = temp.next;
- }
- temp.next = new Node(e);
- }
- } else {
- header = new Node(e);
- }
- }
- public void printAll() {
- for (Node temp = header; temp != null; temp = temp.next) {
- System.out.print(temp.element + " ");
- }
- }
- }
- public class NewListC {
- public static void main(String args[]){
- LinkedList La=new LinkedList();
- LinkedList Lb=new LinkedList();
- LinkedList Lc=new LinkedList();
- for(int i=1; i<12; i=i+3){
- La.addtotail(i);
- }
- for(int i=3; i<25; i=i+4){
- Lb.addtotail(i);
- }
- System.out.print("链表A为: ");
- La.printAll();
- System.out.println();
- System.out.print("链表B为: ");
- Lb.printAll();
- Node tempA=La.header;
- Node tempB=Lb.header;
- while(tempA!=null){
- if(tempA.element
- Lc.addtotail(tempA.element);
- tempA=tempA.next;
- }
- else if(tempA.element==tempB.element){
- Lc.addtotail(tempA.element);
- Lc.addtotail(tempB.element);
- tempA=tempA.next;
- tempB=tempB.next;
- }
- else{
- Lc.addtotail(tempB.element);
- tempB=tempB.next;
- }
- }
- while(tempB!=null){
- Lc.addtotail(tempB.element);
- tempB=tempB.next;
- }
- System.out.println();
- System.out.print("新生成的链表C为: ");
- Lc.printAll();
- }
- }
运行结果:
链表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) 编辑 收藏