算法之如何实现单链表插入删除?(Java)

本次实现单链表基本功能共分为

1. 节点类
这里写图片描述
.
.
.
2. 链表类
这里写图片描述
.
.
.
3. 测试类
.
.
.
.

=============================================================

****Node.java****
package com.ash.www;

//节点类
public class Node {
	//数据域与指针域
	protected Node next;
	protected int data;
	
	//构造函数
	public Node(int data){
		this.data = data;
	}
	
	//打印节点
	public void display(){
		System.out.print(data + " ");
	}
	
}

.
.
.
.
.
.
.
.

****LinkedList.java****
package com.ash.www;

public class LinkedList {
	public Node first;	//
	public int positon = 0;
	
	public LinkedList(){
		this.first = null;
	}

	// 插入一个头节点 
	public void addFirstNode(int data){
		Node node = new Node(data);
		node.next = first;
		first = node;
		System.out.println("Add completed!");
	}
	
	// 删除一个头结点
	public void deleteFirstNode(){
		first = first.next;
		System.out.println("Delete completed!");
	}
	
	// 在任意位置插入节点, index表示插入后的位置
	public void add(int index, int data){
		Node node = new Node(data);
		Node current = first;
		Node previous = first;
		
		while(positon != index){	
			if(current.next == null){
				System.out.println("Sorry, index "+ index +" is not found!");
				break;
			}
			previous = current;
			current = current.next;
			positon++;
		}
		if(positon == index){
			node.next = current;
			previous.next = node;
			System.out.println("Add completed!");
		}
		positon = 0;
	}
	
	
	// 删除任意位置的节点, index表示插入后的位置
	public void delete(int index){
		Node current = first;
		Node previous = first;
		
		while(positon != index){
			if(current.next == null){
				System.out.println("Sorry, index "+ index +" is not found!");
				break;
			}
			previous = current;
			current = current.next;
			positon++;
		}
		if(positon == index){
			if(current == first){
				first = first.next;
				System.out.println("Delete completed!");
			}
			else {
				System.out.println("Delete completed!");
				previous.next = current.next;
			}
		}
		positon = 0;
	}
	
	// 根据节点的data删除节点
	public void deleteByData(int data){
		Node previous = first;
		Node current = first;
		
		while(current.data != data){
			if(current.next == null){
				System.out.println("Sorry, data "+ current.data +" is not found!");
				break;
			}
			previous = current;
			current = current.next;;
		}
		if(current.data == data){
			previous.next = current.next;
			System.out.println("Delete completed!");
		}
	}
	
	// 显示出所有的节点信息  
	public void displayAllNode(){
		Node current = first;
		while(current != null){
			System.out.print(current.data + " ");
			current = current.next;
		}
		System.out.println(" ");
	}
	
	// 根据位置查找节点信息 
	public void findbyPos(int index){
		Node previous = first;
		Node current = first;
		
		while(positon != index){
			if(current.next == null){
				System.out.println("Sorry, index "+ index +" is not found!");
				break;
			}
			previous = current;
			current = current.next;;
			positon++;
		}
		if(positon == index){
			System.out.println("Search Completed! \nData is "+ current.data +".");
		}
		positon = 0;
	}
	
	// 根据数据查找节点信息 
	public void findByData(int data){
		Node previous = first;
		Node current = first;
	
		while(current.data != data){
			if(current.next == null){
				System.out.println("Sorry, data "+ current.data +" is not found!");
				break;
			}
			previous = current;
			current = current.next;;
			positon++;
		}
		if(current.data == data){
			System.out.println("Search Completed! \nData position is "+ positon +".");
		}
		positon = 0;
	}
}

.
.
.
.
.
.
.
.

//测试类
****Text****
package com.ash.www;

public class Text {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		LinkedList linkedList = new LinkedList();
		//add FirstNode
		System.out.println();
		linkedList.addFirstNode(30);	//10
		linkedList.addFirstNode(20);	//10, 20
		linkedList.addFirstNode(10);	//10, 20, 30
		System.out.println();
		linkedList.displayAllNode();	//10, 20, 30
		System.out.println("-------------------------------------------------------------");
		
		
		System.out.println();
		//add index
		linkedList.add(1, 1);	//10, 1, 20, 30
		linkedList.add(2, 2);	//10, 1, 2, 20, 30
		linkedList.add(4, 33);	//10, 1, 2, 20, 33, 30
		linkedList.add(7, 7);	//not found
		System.out.println();
		linkedList.displayAllNode();	
		System.out.println("-------------------------------------------------------------");
		
		
		System.out.println();
		//delete FirstNode
		linkedList.deleteFirstNode();	
		linkedList.deleteFirstNode();	 
		System.out.println();
		linkedList.displayAllNode();	//2, 20, 33, 30
		System.out.println("-------------------------------------------------------------");
		
		
		System.out.println();
		//add FirstNode
		linkedList.addFirstNode(300);	
		linkedList.addFirstNode(200);	//200, 300, 2, 20, 33, 30
		System.out.println("-------------------------------------------------------------");
	
		
		System.out.println();
		//delete by index
		linkedList.delete(3);	//200, 300, 2, 33, 30
		linkedList.delete(1);	//200, 2, 33, 30
		linkedList.delete(4);	//not found
		System.out.println();
		linkedList.displayAllNode();	//200, 2, 33, 30
		System.out.println("-------------------------------------------------------------");
		
		
		System.out.println();
		linkedList.addFirstNode(100);	//100, 200, 2, 33, 30
		linkedList.addFirstNode(50);	//50, 100, 200, 2, 33, 30
		//delete by data
		linkedList.deleteByData(100);	//50, 200, 2, 33, 30
		linkedList.deleteByData(2);		//50, 200, 33, 30
		linkedList.deleteByData(500);	//not found
		System.out.println();
		linkedList.displayAllNode();	//50, 200, 33, 30
		System.out.println("-------------------------------------------------------------");
		
		
		System.out.println();
		//find by Pos
		linkedList.findbyPos(2);	//33
		linkedList.findbyPos(0);	//50
		linkedList.findbyPos(5);	//not found
		System.out.println("-------------------------------------------------------------");
		
		
		System.out.println();
		//find by Data
		linkedList.findByData(30);	//3
		linkedList.findByData(100);	//not found
		System.out.println("-------------------------------------------------------------");
	}
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验一 1. 使用记事本和命令行程序编写Java应用程序,打印出所有的水仙花数。 2. 程序设计(开发环境不限): 1) 打印出100以内的素数 2) 求1!+2!+……+20! 3) 课后习题2.6 4) 编写程序,命令行窗口输出希腊字母表。(希腊字母表开始为α,最后一个为ω) 实验二 1、设计一个名为figure的图形软件包(package)。包中包含三角形、矩形、圆三个类。要求:(1)每个类都要构造方法并为成员设置get和set方法;(2)每个类都要有计算周长和面积的成员方法;(3)完成该软件包后的编码后,在另一个包的含有main方法的类中编写代码,分别使用图形软件包中的三个类,生成三个对象,并打印出其周长和面积。 2、编写类Factorial,为其添加两个静态方法(方法名自定义)。其中一个使用递归计算n的阶乘,一个使用非递归计算n的阶乘。构造main方法进行测试。 3、按照要求使用Java进行编码。 1) 设计一个教师类Teacher,属性有编号(no)、姓名(name)、年龄(age)、所属学院(seminary),为这些属性设置相应的get和set方法; 2) 为Teacher类添加方法equals;(当两个教师对象的no相同时返回true) 3) 为Teacher类添加方法toString,通过该方法可以返回“编号为**、姓名为**、年龄为**的**学院老师”形式的字符串。 4) 构造main方法进行测试。 4、设计一个带表头的单链表(链表中的元素属于同一类型对象,但对象的类型可以随意),提供以下操作:(1)insert:在某个位置插入对象;(2)delete:在某个位置删除对象;(3)delete:删除链表中与x相同的元素;(4)size:返回当前链表中对象的个数;(5)isEmpty:判断链表是否为空;(6)traverse:遍历链表,打印出所有的元素;(7)getData:取得某个位置的对象。构造main函数进行测试。 实验三 1、按照要求使用Java进行编码。 1) 编写一个抽象类Shape,其中有抽象方法getArea()和getPerimeter() 2) 在Shape类的基础上派生出Rectangle和Circle类,二者都实现了计算面积的方法getArea()和计算周长的方法getPerimeter(); 3) 构造main函数,生成Rectangle和Circle对象,并用Shape类型的变量调用Rectangle和Circle对象的getArea()和getPerim()方法。 2、以电话为父类,移动电话和固定电话为两个子类,并使移动电话实现接口:可移动。固定电话又有子类:无绳电话。定义接口及各类,明确他们的继承关系。 3、在实验2中所实现的Teacher类的基础上,修改Teacher类的代码,要求:由多个Teacher对象所形成的数组可以使用Arrays.sort方法进行排序(编号由低到高排序)。 实验四 1、在main方法中创建一个含有10个元素的int型数组,进行以下操作:(1)将数组元素按照从小到大的顺序排列;(2)对排好序的数组使用折半查找(使用递归和非递归两种形式分别实现)查找某一个int元素。 2、使用一维数组编码实现一个栈(Stack)类,要求提供以下操作:(1)boolean isEmpty():判断栈当前是否为空;(2)入栈操作void push(obj):把数据元素obj插入堆栈;(3)出栈操作Object pop():出栈,并返回删除的数据元素;(4)Object getTop():取堆栈当前栈顶的数据元素并返回。编写代码测试所形成的Stack类,然后利用Stack类实现以下功能:输入一个正整数,输出该整数所对应的二进制数。 3、按照要求使用Java编码。 1) 以类型int[][]声明一个叫matrix的二维数组变量,将矩阵初始化为一个5个元素的数组。 2) 以下列方式为matrix的内部元素赋值:matrix从零开始循环到其长度值;例如索引为i,在每次迭代中,将matrix[i]指向一个新的整数数组,其长度为i。然后用索引变量j,对数组中的每一个元素进行循环。在每次内部循环中,将matrix[i][j]赋值为(i*j)。 3) 通过循环打印matrix中的所有元素,结果为: <> <0> <0 2> <0 3 6> <0 4 8 12> 4、利用二维数组实现一个矩阵类:Matrix。要求提供以下操作:(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩阵的列数;(4)height():返回矩阵的行数;(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的结果矩阵;(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的结果矩阵。(7)print():打印出当前矩阵的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值