首页 > 代码库 > 单链表的
单链表的
1:2: package cn.cqu.huang;
3:4: public class SingleList {5: private int data; //数据域6: private SingleList next; //指针域7:8: public SingleList(int x){9: data = http://www.mamicode.com/x;>10: }11:12: public void append(SingleList x){ //在链表末尾增加一个元素13: SingleList p = this; //使p指向当前节点14: while(p.next!=null){15: p = p.next;16: }17: p.next = x;18: }19:20: public void add(SingleList x){ //在当前节点的后面添加一个节点21: x.next = next;22: next = x;23: }24:25: public void show(SingleList x){26: SingleList p = this;
27: while(p!=null){28: System.out.println(p.data);29: p = p.next;30: }31: }32:33:34: public static void main(String[] args){35: SingleList list = new SingleList(10);
36: list.append(new SingleList(20)); //在list节点后面增加节点37: list.append(new SingleList(30));
38: list.append(new SingleList(40));
39:40: list.add(new SingleList(50)); //在list节点前面增加节点41: list.append(new SingleList(60));
42:43: list.show(list);44: }45: }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。