Java数据结构系类之——链表(1):单链表及相关常用操作

package LinkList;

public class Node<T> {
	public T data;//数据域
	public Node next;//结点域

	//默认构造方法
	public Node(){
	}

	//带参构造方法,非头结点初始化
	public Node(T data,Node next){
		this.data=data;
		this.next=next;
	}

	//头结点初始化
	public Node(Node next){
		this.next=next;
	}

	//显示结点值
	public void display(){
		System.out.print(data+" ");
	}
}

************************************************华丽的分割线************************************************************************

package LinkList;
/**
 * ****************带头结点的单链表及其实现*********************
 *
 * @author wl
 *
 */
public class SinglyLinkList<T> {
	Node<?> head;//头结点
	int size;//链表大小
	Node<?> current;//当前结点

	//初始化一个空链表
	public SinglyLinkList(){
		this.head=new Node<Object>(null);
		this.size=0;
		this.current=head;
	}

	//判断链表是否为空
	public boolean isEmpty(){
		return size==0;
	}
	//打印链表
	public void traverse(){
		if(isEmpty()){
			System.out.println("null");
		}else{
			for(Node<?> p=head.next;p!=null;p=p.next){
				System.out.print(p.data+",");
			}
			System.out.println();
		}
	}
	//从头结点增加结点
	public void addFromHead(T value){
		Node<T> node=new Node<T>(value,null);
		node.next=head.next;
		head.next=node;
		size++;
	}

	//从尾结点增加结点
	public void addFromTail(T value){
		Node<T> node=new Node<T>(value,null);
		this.current=head.next;

		if(current==null){
			head.next=node;
			size++;
		}else{
			while(current.next!=null){//将当前结点定位到尾结点
				current=current.next;
			}
			current.next=node;
			size++;
		}
	}

	//从头结点删除
	public void removeFromHead(){
		if(isEmpty()){//判断链表是否为空
			throw new RuntimeException("链表为空");
		}

		current=head.next;//当前结点
		head.next=current.next;
		current.next=null;
		size--;
	}

	//从尾结点删除
	public void removeFromTail(){
		if(isEmpty()){//判断链表是否为空
			throw new RuntimeException("链表为空");
		}

		Node<?> prev=null;//前一个结点
		this.current=head.next;
		while(current.next!=null){//将当前结点定位到尾结点
			prev=current;
			current=current.next;
		}
		prev.next=null;
		size--;
	}

	//在第index后面插入一个结点
	public void insert(int index,T value){
		if(index<0||index>size){
			throw new RuntimeException("参数index有误");
		}

		Node<T> node=new Node<T>(value,null);

		if(index==0){
			node.next=head.next;
			head.next=node;
			size++;
		}else{
			int count=0;//计数器
			Node<?> prev=null;//前一个结点
			current=head.next;
			while(current!=null&&count!=index){//将当前结点定位到第index个结点处
				prev=current;
				current=current.next;
				count++;
			}

			node.next=current;
			prev.next=node;
			size++;
		}

	}

	//删除任意位置index位置的某个结点
	public void remove(int index){
		if(index<0||index>size){
			throw new RuntimeException("参数index有误");
		}

		if(index==0){
			current=head.next;
			head.next=current.next;
			size--;
		}else{
			int count=0;//计数器
			Node<?> prev=null;//前一个结点
			current=head.next;
			while(current!=null&&count!=index){//将当前结点定位到第index个结点处
				prev=current;
				current=current.next;
				count++;
			}

			prev.next=current.next;
			current=null;
			size--;
		}
	}

	//根据value值删除结点,当有多个相同的value值时,只删除第一个
	public void removeByValue(T value){
		if(isEmpty()){
			throw new RuntimeException("链表为空");
		}

		int count=0;//计数器
		Node<?> prev=null;//前一个结点
		current=head.next;
		while(current!=null&¤t.data!=value){//将当前结点定位到值为value的第一个结点处
			prev=current;
			current=current.next;
			count++;
		}

		if(count>size){
			throw new RuntimeException("不存在值为value的结点");
		}

		prev.next=current.next;
		current=null;
		size--;
	}
}
时间: 2024-08-07 02:35:00

Java数据结构系类之——链表(1):单链表及相关常用操作的相关文章

Java数据结构系类之——链表(2):单向循环链表及相关常用操作

package LinkList.onewaycircular; public class Node { public int data; public Node next; //头结点初始化 public Node(Node next){ this.next=next; } //一般结点初始化 public Node(int data,Node next){ this.data=data; this.next=next; } } ********************************

Java数据结构系类之——链表(3):双向链表及相关常用操作

package LinkList.doublelinklist; public class Node { public int data;//数据域 public Node next;//结点域,下一个结点 public Node prve;//结点域,前一个结点 //头结点初始化 public Node(Node next){ this.data=data; this.next=next; this.prve=null; } //非头结点初始化 public Node(int data,Nod

java数据结构与算法之顺序表与链表深入分析

转载请注明出处(万分感谢!): http://blog.csdn.net/javazejian/article/details/52953190 出自[zejian的博客] 关联文章: java数据结构与算法之顺序表与链表设计与实现分析 java数据结构与算法之双链表设计与实现 ??数据结构与算法这门学科虽然在大学期间就已学习过了,但是到现在确实也忘了不少,因此最近又重新看了本书-<数据结构与算法分析>加上之前看的<java数据结构>也算是对数据结构的进一步深入学习了,于是也就打算

Java数据结构和类有哪些?

Java数据结构和类有哪些?很多java初学者可能记不住或者记不全那么多,今天西安java培训小编为大家整理了Java数据结构和类的知识,希望对大家有所帮助. 一.Vector类 Vector类似于一个数组,但与数组相比在使用上有以下两个优点. 1.使用的时候无需声明上限,随着元素的增加,Vector的长度会自动 增加. 2.Vector提供额外的方法来增加.删除元素,比数组操作高效. Vector类有三个构造函数,分别如下: public Vector(); 该方法创建一个空的Vector.

数据结构 链表_单链表的接口定义

链表可以说是一种最为基础的数据结构.链表由一组元素以一种特定的顺序组合或链接而成,在维护数据的集合时很有用.这一点同我们常用的数组很相似.然而,链表在很多情况下比数组更有优势.特别是在执行插入和删除操作时链表拥有更高的效率.链表需要动态的开辟存储空间,也就是存储空间是在程序运行时分配的.由于在很多应用中数据的大小在编译时并不能确定,因此这种动态分配空间的特性也是链表的一个优点. 单链表介绍 单链表(简称为链表)由各个元素之间通过一个指针彼此链接起来而组成.每个元素包含两个部分:数据成员和一个称为

JAVA实现具有迭代器的线性表(单链表)

一,迭代器的基本知识: 1,为什么要用迭代器?(迭代:即对每一个元素进行一次“问候”) 比如说,我们定义了一个ADT(抽象数据类型),作为ADT的一种实现,如单链表.而单链表的基本操作中,大部分需要用到依次遍历单链表中的每一个元素.一般而言,我们就是用for循环来实现遍历,这样,当你新增一个对单链表的操作并需要使用遍历时,你就得重新写一个for循环而实现遍历.那么,为什么不将迭代(遍历)作为一种基本的ADT操作(基本的ADT操作如:新增一个元素.删除一个元素)呢?于是,迭代器就出场了. 2,鉴于

JAVA模拟顺序表新增,模拟单链表新增

最近在回顾大学学的数据结构,这里给大家用java模拟顺序表和单链表的新增 1顺序表新增 /** * 顺序表 * * @author cjd * */ public class ArrayList { private Object[] elementData; // 底层是一个数组,目前还没有确定长度 private int size; // 不是数组分配了几个空间,而是元素的个数 public ArrayList() { this(4); } public ArrayList(int initi

Python与数据结构[0] -&gt; 链表[0] -&gt; 单链表与带表头单链表的 Python 实现

单链表 / Linked List 目录 单链表 带表头单链表 链表是一种基本的线性数据结构,在C语言中,这种数据结构通过指针实现,由于存储空间不要求连续性,因此插入和删除操作将变得十分快速.下面将利用Python来完成单链表的实现. 1 单链表 不带表头的单链表通常形式如下, node_1 -> node_2 -> node_3 -> node_4 完整代码 1 class Node: 2 def __init__(self, val=None, nxt=None): 3 self.v

数据结构与算法系列四(单链表)

1.引子 1.1.为什么要学习数据结构与算法? 有人说,数据结构与算法,计算机网络,与操作系统都一样,脱离日常开发,除了面试这辈子可能都用不到呀! 有人说,我是做业务开发的,只要熟练API,熟练框架,熟练各种中间件,写的代码不也能“飞”起来吗? 于是问题来了:为什么还要学习数据结构与算法呢? #理由一: 面试的时候,千万不要被数据结构与算法拖了后腿 #理由二: 你真的愿意做一辈子CRUD Boy吗 #理由三: 不想写出开源框架,中间件的工程师,不是好厨子 1.2.如何系统化学习数据结构与算法?