线性链表 java实现「建议收藏」

线性链表 java实现「建议收藏」publicclassLinkList{ classNode{//定义Node节点 privateTdata; privateNodenext; publicNode(){} publicNode(Tdata,Nodenext){ this.data=data; this.next=next; } } privateNodehea

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

public class LinkList<T> {
	class Node{//定义Node节点
		private T data;
		private Node next;
		public Node(){}
		public Node(T data,Node next){
			this.data=data;
			this.next=next;
		}	
	}
	private Node header;//头指针
	private Node tail;//尾指针
	private int size;//线性表的长度
	public LinkList(){//构造空的线性链表
		this.header=null;
		this.tail=null;
		this.size=0;
	}
	public LinkList(T element){//构造一个节点的线性链表
		this.header=new Node(element,null);
		this.tail=this.header;
		this.size++;
	}
	public int length(){//返回线性链表的长度
		return size;
	}
	public void add(T element){//尾插法添加节点
		if(header==null){
			header=new Node(element,null);
			tail=header;
		}else{
			Node newNode=new Node(element,null);
			tail.next=newNode;
			tail=newNode;
		}
		size++;
	}
	public void addAtHeader(T element){//头插法尾链表添加新节点
		header=new Node(element,header);
		if(tail==null)
			tail=header;
		
		size++;
	}
	public Node getNodeByIndex(int index){//得到节点
		if(index<0||index>size-1){
			throw new IndexOutOfBoundsException("线性表索引越界");
		}
		Node current=header;
		for(int i=0;i<size && current!=null;current=current.next,i++){
			if(i==index)
				return current;
		}
		return null;
	}
	public int locate(T element){
		Node current=header;
		for(int i=0;i<size && current!=null;i++,current=current.next){
			if(current.data==element)
				return i;
		}		
		return -1;
	}
	public void insert(T element,int index){
		if(index<0||index>size-1){
			throw new IndexOutOfBoundsException("线性表索引越界");
		}else{
			if(index==0){
				addAtHeader(element);
			}else{
				Node prev=getNodeByIndex(index-1);
				prev.next=new Node(element,prev.next);
				size++;
			}
		}
	}
	public T delete(int index){//删除节点
		if(index<0||index>size-1){
			throw new IndexOutOfBoundsException("线性表索引越界");
		}
		Node del=null;
		if(index==0){
			del=header;
			header=header.next;
		}else{
			Node prev=getNodeByIndex(index-1);
			del=prev.next;
			prev.next=del.next;
			del.next=null;
		}
		size--;
		return del.data;	
	}
	public boolean empty(){//判断链表是否为空
		return size==0;
	}
	public void clear(){//清空链表
		header=null;
		tail=null;
		size=0;
	}
	public String toString(){
		if(empty()){
			return "[]";
		}else{
			StringBuilder sb=new StringBuilder("[");
			for(Node current=header;current!=null;current=current.next){
				sb.append(current.data.toString()+",");
			}
			int len=sb.length();
			return sb.delete(len-1,len).append("]").toString();
		}
	}
	public static void main(String []args){
		LinkList<String> list=new LinkList<String>();
		list.add("zhang san");
		list.add("li si");
		list.add("wang wu");
		list.add("zhang long");
		list.add("zhao hu");
		System.out.println("打印单链表"+list);
		System.out.println("li si"+"["+list.locate("li si")+"]");
		list.delete(4);
		System.out.println("删除元素后的单链表"+list);
	}
}

Jetbrains全家桶1年46,售后保障稳定


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234681.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 导入pfx证书

    导入pfx证书打开MicrsoftManangeConsole,你可以按住“Windows+R”,并输入mmc,打开MMC界面。一:添加管理单元(snap-in)从File主菜单中选择“Add/RemoveSnap-in”,打开“AddorRemoveSnap-ins”对话框从左侧的“Availablesnap-ins”列表中选择Certificates,点击”Add>”,打开“Certificatessnap-in”对话框,选择“Computeraccount”:选择snap-

    2022年6月7日
    96
  • 2014阿里巴巴web前实习生项目分析(1)

    2014阿里巴巴web前实习生项目分析(1)

    2022年1月12日
    48
  • 【原】Github系列之一:一起做仿天气类应用中的实时模糊效果LiveBlur

    【原】Github系列之一:一起做仿天气类应用中的实时模糊效果LiveBlur

    2021年9月4日
    54
  • 数据绑定中的一个问题”pages enableEventValidation=”true””[通俗易懂]

    数据绑定中的一个问题”pages enableEventValidation=”true””[通俗易懂]我们在用vs2005做数据绑定的时候运行出来的回发或回调参数无效。在配置中使用<pagesenableEventValidation=”true”/>或在页面中使用<%@PageEnableEventValidation=”true”%>启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据…

    2022年7月14日
    37
  • 教育邮箱申请pycharm_python注册用户名和密码登录

    教育邮箱申请pycharm_python注册用户名和密码登录学生注册教育邮箱的方法不知道如何注册教育邮箱的看我上篇文。注册之后都会给登录的网址和初始密码。接下来进入这个网址JetBrains学生注册点击learnmore后,一直下拉点击applynow他会有一个信息表的填写进行申请后会发两个邮件,如下图,点击联接进行操作即可,网上说这个申请比较慢,我觉得还可以,可能是因为早上的关系,出现这个界面就说明注册成功了直接安装好pycharm的专业版在登录的时候直接填写我们的账号和密码就成功了,会显示使用终止…

    2022年8月28日
    3
  • floyed「建议收藏」

    floyed「建议收藏」#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>usingnamespacestd;constintmaxn=405;constintinf=4000005;intd1[maxn][m…

    2022年6月15日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号