单链表操作--java

        单链表的简单操作,包括新增头结点、删除头结点、在任意位置新增节点、在任意位置删除节点、通过节点位置删除节点、通过数据域中的数删除节点。

       注意事项:

1、添加操作:若该链表为空,则不应该插入非头节点,若该链表不为空,不允许插入超出链表长度的数据,如链表长度为2,则链表位置为0,1,此时插入数据最大位置为2,不允许插入数据位置设为3.

2、删除操作:若该链表为空,不允许进行删除操作,若该链表不为空,不允许哎删除操作时传值位置超出链表长度,利用数值查询进行删除,若传值的数据不存在,无法进行删除。

      具体代码如下:有什么写的不周全的欢迎指正。

package List;

public class LinkList {

	public Node first;   //定义一个头结点
	public int temp=0;    //头结点的位置
	int len=0;
	 
	//构造函数,头结点初始状态的的指针域指向空
	public LinkList()
	{
		this.first=null;
	}
	
	//插入一个头结点
	public void addFirstNode(int data)
	{
		Node node=new Node(data);
		node.next=first;   //新建的节点的指针域点指向原头结点
		first=node;        //头结点变为node
		len++;
	}
	
	//删除一个头结点,并返回头结点
	public Node deleteFirstNode()
	{
		Node temp=null;
		if(first==null)
			System.out.println("删除失败,该链表为空,不可执行删除操作");
		else
		{
		temp=first;
		first=temp.next;
		len--;
		}
		return temp;
	}
	
	//在任意位置插入节点,该位置由传参index决定,新增节点为index位置
	public void add(int index,int data)
	{
		Node node=new Node(data);
		Node previous=first;
		Node current=first;
		if(index>len)
		{
			System.out.println("插入失败,插入数据位置超出该链表长度");
			return;
		}
		while(temp!=index)
		{
			previous=current;
			current=current.next;
			temp++;
		}
		if(index==0)
		{
			node.next=first;
			first=node;
		}
		else
		{
		node.next=current;
		previous.next=node;
		}
	    temp=0;
	    len++;
	}
	
	
	//删除任意位置的节点,并返回该节点
	public Node delete(int index)
	{
		Node current=first;
		Node previous=first;
		if(index>len)
		{
			System.out.println("删除失败,该节点位置不存在链表中,超出链表");
			return null;
		}
		while(temp!=index)
		{
			previous=current;
			current=current.next;
			temp++;
		}
		if(current==first)
			first=first.next;
		else
			previous.next=current.next;
		temp=0;
		len--;
		return current;
	}
	
	//根据data删除该节点,并返回该节点
	public Node deleteByData(int data)
	{
		Node current=first;
		Node previous=first;
		while(current.data!=data)
		{
			if(current.next==null)
			{
				System.out.println("删除失败,该数据不存在与该链表中");
				return null;
			}
			previous=current;
			current=current.next;
			
		}
		if(current==first)
		{
			first=current.next;
		}
		else
			previous.next=current.next;	
		len--;
		return current;	
		}
	
	//显示所有的节点信息
	public void displayALL()
	{
		Node current=first;
		while(current!=null)
		{
			current.display();
			current=current.next;
		}
		
	}
	
	public static void main(String[] args)
	{
		LinkList list=new LinkList();
		list.displayALL();
		list.addFirstNode(1);
		list.addFirstNode(10);
		list.add(2,2);
		list.add(2,3);
		list.add(3,4);
//		list.add(5,13);
//		list.deleteFirstNode();
//		list.delete(8);
//		list.deleteByData(3);
		list.displayALL();
	}

}



package List;

public class Node {
 protected Node next;
 protected int data;
 
 public Node(int data)
 {
	 this.data=data;
 }

public void display()
 {
	 System.out.print(data + " ");
 }
 
}





内容概要:本文档详细介绍了Android高级控件的使用方法及其应用场景。首先讲解了下拉列Spinner,包括其两种现形式(下拉列形式和对话框形式),并介绍了适配器Adapter的基础概念及其三种主要类型:数组适配器ArrayAdapter、简单适配器SimpleAdapter和基本适配器BaseAdapter,重点阐述了它们各自的特点和使用步骤。接着,文档对列视图ListView进行了深入探讨,涉及分隔线样式、按压背景等属性的设置方式。随后,描述了网格视图GridView,详细解释了其拉伸模式的效果及取。对于翻页视图ViewPager,不仅介绍了基本概念,还展示了翻页标签栏PagerTabStrip的具体应用,特别是用于创建启动引导页。最后,文档介绍了碎片Fragment的概念,强调了其在大屏设备上的优势,以及与ViewPager结合使用的实战案例——记账本应用。 适合人群:有一定Android开发基础,希望深入了解并掌握高级控件使用的开发者。 使用场景及目标:①掌握下拉列、列视图、网格视图、翻页视图等高级控件的实现细节;②理解适配器的作用及其不同类型的使用场景;③学会使用Fragment优化应用界面布局,提高用户体验;④通过具体案例(如记账本),将所学控件应用于实际开发中。 阅读建议:本文档内容详实,涵盖多种高级控件的理论知识与实践技巧。建议读者在学习过程中结合官方文档或相关资料进行对比研究,同时动手实践,以便更好地理解和掌握这些控件的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值