【JAVA数据结构】 简单链表的实现

写在前面:

    我也是一名java语言的爱好者,仅以此文作为学习的记录,对于文中出现的代码规范,代码格式,算法效率等问题,希望各路大神不吝赐教,在下感激不尽。同是学习的同学也同样希望互相交流,取长补短。

                                                                                                                                                      ——zsferrier@126.com

简单链表的实现:

根据Mark Allen Weiss的《数据结构与算法分析》一书,队简单链表的描述。我定义了printList,makeEmpty,find,insert,remove,findKth方法,方法功能正如其字面意思。具体的在代码中有体现。

代码结构:


1.node.java

//节点类

public class node<T> {
	public T data;			//数据
	public node nextnodeNode; 		//“指向”下一个节点
	public node(T data) { //构造函数
		super();
		this.data = data;
		this.nextnodeNode = null;
	}
	
}

2.linkList.java

//简单链表类
public class linkedList {
	public int ListLen;
	public node frontNode;
	public linkedList() {
		super();
		ListLen = 0;
		this.frontNode = null;
	}
	//在指定位置插入元素
	public void insert(node x,int position){
		if (ListLen==0) {
			frontNode = x;
		}else {
			node<Integer> pointNode = frontNode;//遍历指针指向链首
			for (int i=0;i<position-1;i++){//定位到position前一个位置
				pointNode = pointNode.nextnodeNode;//往后遍历
			}
			//找到位置 插入
			x.nextnodeNode = pointNode.nextnodeNode;
			pointNode.nextnodeNode = x;
		}
		ListLen ++;//总长度自增
	}


	//查找指定元素位置
	public int find(int x){
		node<Integer> pointNode = frontNode;//遍历指针指向链首
		int p = 0;//位置
		while(pointNode != null){
			if(pointNode.data==x){
				return p;
			}else {
				p++;
				pointNode = pointNode.nextnodeNode;
			}
		}
		System.out.println("find not res");
		return -1;
	}
	//输出简单链表序列
	public void printList() {
		String listSentence = "";
		node<Integer> pointNode = frontNode;//遍历指针指向链首
		for (int i = 0 ; i < ListLen ; i++){
			listSentence = listSentence+ pointNode.data +"     ";
			pointNode = pointNode.nextnodeNode;//往后遍历
		}
		System.out.println(listSentence);
	}
	//清空链表
	public void makeEmpty() {
		ListLen = 0;
		frontNode = null;
	}
	//移除某个元素
	public void remove(int x){
		int pos = find(x);  //获得位置
		if(pos==-1){
			return;
		}
		node<Integer> pointNode = frontNode;//遍历指针指向链首
		for (int i = 0 ; i < pos-1 ; i++){ //找到pos前面一个元素
			pointNode = pointNode.nextnodeNode;//往后遍历
		}
		pointNode.nextnodeNode = pointNode.nextnodeNode.nextnodeNode;//从链表中删除
		ListLen--;
	}
	//查找并返回某个位置上的元素
	public node findKth(int pos){
		node<Integer> pointNode = frontNode;//遍历指针指向链首
		for (int i = 0 ; i < pos ; i++){ //找到pos前面一个元素
			pointNode = pointNode.nextnodeNode;//往后遍历
		}
		return pointNode;
	}
}



3.Listcontroller.java

//测试类
public class Listcontroller {


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		linkedList list = new linkedList();
		int[] numlist = {4,5,13,16,2,7,15,11,32,24};
		for (int i=0;i<10;i++){
			node<Integer> n = new node<Integer>(numlist[i]);
			list.insert(n, i);
		}
		list.printList();
		int pos = list.find(13);
		if (pos != -1){
			System.out.println("位置为"+pos);
		}
		//插入元素50
		node<Integer> n1 = new node<Integer>(50);
		list.insert(n1, 5);
		list.printList();
		//删除元素7
		list.remove(7);
		list.printList();
	}
}

4.输出结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值