2017.12.20T19_B3.1

package com.xdf.linkdemo;

/**  * 单链表  */ public class MyLink {  // 存储链表的值  private int data;  private MyLink next;// 指针指向下一个数据

 public MyLink(int i) { // 创建带参构造   data = i;  }

 // 向链表中增加值  public void add(MyLink link) {   link.next = next;   next = link;

 }

 private void append(MyLink myLink) {   MyLink x = this;   while (x.next != null) {    x = x.next;   }   x.next = myLink;

 }

 private void show() {   MyLink x = this;   while (x != null) {    System.out.println(x.data);    x = x.next;   }

 }

 public static void main(String[] args) {   MyLink link = new MyLink(1);   link.append(new MyLink(10));// 拼接   link.append(new MyLink(20));// 拼接   link.append(new MyLink(50));// 拼接   link.add(new MyLink(6)); // 新增   link.show();

 }

posted @ 2017-12-20 09:44  水墨&丹青  阅读(111)  评论(0编辑  收藏  举报