数据结构——支持删除链表中任意节点(java实现)

跟上一篇博文相比,这里实现了删除任意节点的功能,删除任意节点的的函数设计时可以从一下三个方面考虑,便于打开思路;

1、删除的节点是头节点

2、删除的节点是为节点

3、删除的节点是除头结点和尾节点之外的其他节点

定义链表:


class Node
{
	int IDnumber;
	int score;
	String name;
	Node next;
	public Node(int IDnumber, String name, int score)
	{
		this.IDnumber = IDnumber;
		this.name = name;
		this.score = score;
		this.next = null;
	}
}
public class LinkedList {

	public Node first;
	public Node last;
	public boolean isEmpty()
	{
		return first == null;
	}
	public void print()
	{
		Node current = first;
		while(current != null)
		{
			System.out.println("["+current.IDnumber+" " + current.name+" "+current.score+"]");
			current = current.next;
		}
		System.out.println();
	}
	
	/*
	 * 该函数设计的功能只能依次插入,不能随便位置插入
	 * */
	public void insert(int IDnumber,String name, int score)
	{
		Node newNode=new Node(IDnumber, name, score);
		if(this.isEmpty())
		{
			first = newNode;
			last=newNode;
		}
		else {
			last.next=newNode;
			last=newNode;
		}
	}
	
	/*
	 * 删除单向链表中任意节点
	 * */
	public void delete(Node delNode)
	{
		Node newNode,tmp;
		if(first.IDnumber==delNode.IDnumber)
		{
			first=first.next;
		}
		else if(last.IDnumber==delNode.IDnumber)
		{
			newNode = first;
			while(newNode.next!=last)
			{
				newNode = newNode.next;
			}
			newNode.next=last.next;//如果直接用newNode=null呢?试了一下,效果一样的!但依然不排除这里面有陷阱的可能性
			last = newNode;
		}
		else 
		{
			newNode = first;
			tmp = first;
			while(newNode.IDnumber != delNode.IDnumber)
			{
				tmp = newNode;
				newNode = newNode.next;
			}
			tmp.next = delNode.next;
		}
	}
}

测试链表:


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Random;


public class LinkedListTest {

	public static void main(String[] args) throws IOException
	{
		BufferedReader buf;
		Random rand = new Random();
		buf = new BufferedReader(new InputStreamReader(System.in));
		LinkedList list = new LinkedList();
		int i,j,findword=0,IDnumber[][] = new int[12][10];
		String[] name = new String[]{"Allen","Scott","Marry","Jon","Mark","Ricky",
				"Lisa","Jasica","Hanson","Amy","Bob","Jack"};
		System.out.println("学号成绩学号成绩学号成绩学号成绩\n");
		for(i=0; i<12; i++)
		{
			IDnumber[i][0] = i+1;
			IDnumber[i][1]=(int)(rand.nextInt(50)+50);
			list.insert(IDnumber[i][0], name[i], IDnumber[i][1]);
		}
		
		/*此双层循环保证了分行输入,还是有一定技巧性的!
		 * */
		for(i=0;i<3;i++)
		{
			for(j=0;j<4;j++)
			{
				System.out.print("["+IDnumber[j*3+i][0]+"]["+IDnumber[j*3][1]+"] ");
			}
			System.out.println("");
		}
		
		while(true)
		{
			System.out.println("输入要删除成绩的学号,结束请输-1:");
			findword=Integer.parseInt(buf.readLine());
			if(findword==-1)
			{break;}
			else 
			{
				Node current = new Node(list.first.IDnumber, list.first.name, list.first.score);
				current.next = list.first.next;
				while(current.IDnumber!=findword)
				{
					current=current.next;
					
				}
				list.delete(current);
				System.out.println("删除后的成绩列表:");
				list.print();
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值