LinkedList练习

//Node类
package com.pra0710.base;

import java.util.Scanner;

public class Node {
	//只对属性申请空间
	public int value;
	public Node next;
	private Node head;
	
	public Node() {
		
	}

	public Node(int value,Node node) {
		this.value=value;
		this.next=node;
	}
	
	public Node createLink(Scanner sc) {
		
		int m=sc.nextInt();
		while(m>0) {
			//每次进来一个新节点!!
			//动态存储
			Node p=new Node();
			p.value=m;
			p.next=this.head;
			this.head=p;
			//接受键盘的下一个值
			m=sc.nextInt();
		}
		return this.head;
	}
	
	public Node createLinkBytail(Scanner sc) {
		int m=sc.nextInt();
		Node tail=null;
		while(m>0) {
			//创建一个新节点
			Node p=new Node();
			p.value=m;
			if(this.head==null)
				tail=head=p;
			//尾节点抓住新节点
			tail.next=p;
			tail=p;
			m=sc.nextInt();
		}
		return this.head;
	}
	
	
	public void visit() {
		Node p=this.head;
		while(p!=null) {
			System.out.print(p.value+" ");
			p=p.next;
		}
	}
	
	
	public int getValue() {
		return value;
	}

	public Node getHead() {
		return this.head;
	}
	public Node getNext() {
		return next;
	}

}


//LinkedList类
package com.pra0710.base;

public class LinkedList {
	private Node head;
	private Node tail;
	private int size;
	
	public LinkedList() {
		
	}
	//既要更新头节点、长度,还要更新尾节点
	//分四种情况
	//头为空时添加、头不为空时在头节点添加、在尾部添加(先执行!!)、在中间添加
	//特殊情况要先执行
	public boolean add(int index,int value) {
		//索引不合法
		if(index<0||index>this.size)return false;
		Node p=new Node();
		p.value=value;
		Node prev=this.head;
		Node cur=this.head;
		//如果头为空,新节点作为头节点
		if(this.head==null) {
			this.head=this.tail=p;
			this.size++;
		}
		else{
			//不为空
			//索引在0处添加
			if(index==0){
				p.next=this.head;
				this.head=p;
				this.size++;
				return true;
//				//调用函数头插
//				this.addByhead(value);
			}
			
			//索引不在0处
			//找到当前节点,和前一个节点
			prev=this.getIndex(index-1);
			cur=this.getIndex(index);
			//如果在结尾用尾插法!!
			//此时当前引用为空!!
			if(cur==null){
				this.tail.next=p;
				//更新结尾
				this.tail=p;
				this.size++;
				return true;
//				this.addBytail(value);
			}
			
			//不在结尾
			//连接新节点
			prev.next=p;
			p.next=cur;
			this.size++;
			
		}
		return true;
	}
	//头插法
	//只需要更新头节点
	public boolean addByhead(int value) {
//		Node p=new Node();
//		p.value=value;
//		//为空
//		if(this.head==null)
//		{
//			this.head=this.tail=p;
//			this.size++;
//			return true;
//		}
//		p.next=this.head;
//		this.head=p;
//		this.size++;
		this.add(0, value);
		return true;
	}
	//尾插法
	//只需要更新尾节点
	public boolean addBytail(int value) {
//		Node p=new Node ();
//		p.value=value;
//		if(this.head==null)
//			{
//			this.tail=this.head=p;
//			//一定不要忘记大小要增加!!
//			this.size++;
//			return true;
//			}
//		this.tail.next=p;
//		this.tail=p;
//		this.size++;
		this.add(this.size,value);
		return true;
	}
	//假如成员变量没有this.tail
	//只需要分三种情况
	//在头节点添加,在中间添加,在尾部添加
	public boolean add2(int index,int value) {
		Node p=new Node();
		p.value=value;
		if(index<0||index>this.size)return false;
		//添加在头节点
		if(index==0)
		{
			p.next=this.head;
			this.head=p;
			this.size++;
		}else {
			//添加在其他节点
			//找到前一节点和当前节点
			Node prev=this.getIndex(index-1);
			Node cur=this.getIndex(index);
			//添加在尾节点
			if(cur==null) {
				prev.next=p;
				this.size++;
				return true;
			}
				
			//连接
			prev.next=p;
			p.next=cur;
			this.size++;
			
		}
		return true;
	}

	
	//五种情况
	//头为空,头删(长度是否为1),尾删,删中间
	public boolean remove(int value) {
		//头节点值相等
		if(this.head==null)return false;
		if(this.head.value==value) {
			this.head=this.head.next;
			this.size--;
			//减完了
			if(this.size==0)
				this.tail=this.head;
		}else {
			//不在头节点
			int index=this.indexOf(value);
			Node prev=this.getIndex(index-1);
			//删了尾巴
			if(index==(this.size-1))
			{
				prev.next=null;
				this.tail=prev;
				this.size--;
				return true;
			}
			//其他情况
			Node cur=this.getIndex(index);
			prev.next=cur.next;
			this.size--;
		}
		return true;
	}
	
	public boolean set(int index,int value) {
		if(index<0||index>this.size-1)return false;
		Node cur=this.getIndex(index);
		cur.value=value;
		return true;
		
	}
	
	public void clear() {
		this.size=0;
		this.head=this.tail=null;
	}
	
	public int indexOf(int value) {
		Node p=this.head;
		int index=0;
		while(p!=null) {
			if(p.value==value)
				return index;
			index++;
			p=p.next;
		}
//值没有!!
		return -1;
	}
	
	public Node getIndex(int index) {
		Node cur=this.head;
		for(int i=0;i<index;i++)
			cur=cur.next;
		
		return cur;
	}
	
	public void visit() {
		Node p=this.head;
		while(p!=null) {
			System.out.print(p.value+" ");
			p=p.next;
		}
	}
	
	public Node getHead() {
		return head;
	}

	
	public Node getTail() {
		return tail;
	}

	
	public int getSize() {
		return size;
	}

	
}

//测试
package com.pra0710.base;

import java.util.Scanner;

public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//*****测试Node类,用JAVA写链表,用头插和尾插法创建链表(键盘键入)*************//
//		Node node=new Node();
//		Node node1=new Node(2,node);
//		System.out.println(node1.getHead());
//
////		Scanner sc=new Scanner(System.in);
//		node.createLinkBytail(sc);
//		node.visit();
//		System.out.println();
//		node.add(3);
//		node.visit();
//		System.out.println();
		
//		node.add(2);
//		node.visit();
//		System.out.println();
//		node.add(0,3);
//		node.visit();
//		System.out.println();
//		System.out.println(node.getHead().value);
	
//*******测试LinkedList类****************************//	
		LinkedList list=new LinkedList();
		list.addByhead(7);
		list.addBytail(8);
		System.out.println(list.add(2,2));
		list.add(3, 3);
		list.add2(1, 4);
		list.add2(3, 5);
		list.addBytail(0);
		list.addByhead(0);
		list.visit();
		System.out.println();
		
		list.remove(3);
		list.visit();
		System.out.println();

		list.set(0, 3);
		list.visit();
		System.out.println();
		
		System.out.println("size:"+list.getSize());
		System.out.println("head:"+list.getHead().value);
		System.out.println("tail:"+list.getTail().value);
		
//		System.out.println("index.value:"+list.getIndex(2).value);
		System.out.println("index:"+list.indexOf(3));
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值