单链表的定义以及增删改查

package linkedList;

public class LinkedList {
	//定义节点,内部类只为其外部类使用
	//要创建嵌套类的对象,并不需要其外围类的对象,直接用.next
	static class ListNode{
		int val;
		ListNode next;		//指针,指向下一个节点的位置
		public ListNode(int val){
			this.val = val;
		}
	}
	ListNode head;
	ListNode tail;
	int size;
	//定义链表
	public LinkedList(){
		head = null;
		tail = null;
		size = 0;
	}
	
	public static void main(String[] args){
		LinkedList newList = new LinkedList();
		newList.insert(0, 1);
		newList.append(9);
		newList.insert(2, 2);
		newList.append(7);
		newList.display();
		//查找
		System.out.println("--------------------------------");
		System.out.println(newList.search(1));
		System.out.println(newList.search(0));
		System.out.println("--------------------------------");
		
		//修改
		newList.update(2, 9);
		newList.display();
		
	}
	
	//增加新元素在末尾
	public void append(int number){
		ListNode newNode = new ListNode(number);
		if(tail == null){
			tail = newNode;
		}else{
			tail.next = newNode;
			tail = newNode;
			
		}
		size++;
		
	}
	
	//插入节点(位置,新的值)
	public void insert(int position, int number){
		if(position > size){
			return;
		}
		ListNode newNode = new ListNode(number);
		
		//在0处插入节点时,该节点指针指向当前头节点,再替换称为新的头节点,如果就一个节点,那么尾节点也更新下
		if(position == 0){
			newNode.next = head;
			head = newNode;
			if(tail == null){
				tail = newNode;
			}
			size++;
		}else if(position == size){
			this.append(number);
		}else{
			ListNode prev = head;
			//找到要插入位置前一个节点
			for(int i = 0; i < position -1; i++){
				prev = prev.next;
			}
			ListNode now = prev.next;
			newNode.next = now;
			now = newNode;
			size++;
		}
	}
	//打印节点
	public void display(){
		ListNode cur = head;
		while(cur != null){
			System.out.print(cur.val + ",");
			cur = cur.next;
		}
		System.out.println();
	}
	
	//删除节点,将要删除的节点前一个节点指向被删除节点的下一个节点
	public void delete(int number){
		//删除头结点只要让第二个称为头结点即可
		if(head != null && head.val ==number){
			head = head.next;
			size--;
			//删掉称为空链表
			if(size == 0){
				tail = head;
			}else{
				ListNode prev = head;
				ListNode cur = head;
				while(prev != null && cur != null){
					if(cur.val == number){
						if(cur ==tail){
							tail = prev;
						}
						prev.next = cur.next;
						size--;
						return;
					}
					//循环遍历
					prev = cur;
					cur = cur.next;
				}
			}
		}
	}
	//查找,从头结点开始遍历,找到与目标值对应的节点返回其位置即可
	public int search(int number){
		ListNode cur = head;
		for(int index = 0; cur != null; index++){
			if(cur.val ==number){
				return index;
			}
			cur = cur.next;
		}
		return -1;
	}
	
	//修改某一节点的值,先找到对应的节点,改变节点的值即可。
	public int update(int oldValue, int newValue){
		ListNode cur = head;
		for(int index = 0; cur !=null; index++){
			if(cur.val == oldValue){
				cur.val = newValue;
				return index;
			}
			cur = cur.next;
		}
		return -1;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一蓑烟雨渡平生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值