JAVA数据结构学习1.3单链表

单链表的实现

单链表插入有两种:头插法和尾插法;
头插法是:point.next=head.next;
head.next=point;
尾插法是: head.next=point;
head=point;

package primDataStructure;

public class nodeList {
	public String value;
	public nodeList next;
	public nodeList() {
		next=null;
		value=null;
	}
	public void setValue(String value) {
		this.value=value;
	}
	public void addNode(nodeList node) {
		nodeList next=this.next;
		this.next=node;
		node.next=next;
	}
	public int indexOfValue(String value) {
		int i=0;
		nodeList node=this;
		while(node.next!=null) {
			if(node.value.equals(value))
				return i;
			node=node.next;
			i++;
		}
		return -1;
	}
	public static void main(String args[]) {
		nodeList node=new nodeList();
		node.setValue("0");
		for(int i=0;i<10;i++) {
			nodeList nextNode=new nodeList();
			nextNode.setValue(""+i);
			node.addNode(nextNode);
		}
		nodeList nexts=node;
		while(nexts.next!=null) {
			System.out.print(" "+nexts.value);
			nexts=nexts.next;
		}
		System.out.println("找到index"+node.indexOfValue("8"));
	}
}

发布了13 篇原创文章 · 获赞 1 · 访问量 454

猜你喜欢

转载自blog.csdn.net/weixin_43458072/article/details/104331732