首页 > 代码库 > 单链表的

单链表的

  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: }

0d328f8f-779b-43da-b63a-bc6f60ed8dff[8]