java语法4:链表

方法的重载

方法的重载**允许在同一个类中创建具有相同名称但具有不同参数列表的多个方法。**这些方法根据参数的数量、类型或顺序进行重载。

方法的重载具有以下的特点:

  • 方法名相同:重载的方法必须具有相同的名称。
  • 参数列表不同:重载的方法必须具有不同的参数列表,体现在参数数量、参数类型或参数顺序的不同。
  • 返回类型可以相同也可以不同:返回类型通常不影响方法重载的合法性,只要参数列表不同。

比如下面的例子:

// 重载的方法1:接受两个整数参数
public int add(int a, int b) {
    return a + b;
}

// 重载的方法2:接受三个整数参数
public int add(int a, int b, int c) {
    return a + b + c;
}

// 重载的方法3:接受两个浮点数参数
public double add(double a, double b) {
    return a + b;
}

上面三个方法中,方法1和方法2参数个数不同,方法1和方法3参数类型不同,它们都可以写在一个类中。

LinkedList

在Java中,已经实现了LinkedList, 这是一个双向链表数据结构,每个节点都包含数据和两个指向相邻节点的引用,即向前和向后,特别适合高效插入和删除操作的情况,这种结构在后面学习到的栈和队列中将会得到使用

当使用 LinkedList 时,通常会涉及以下常见操作:

  • add()添加元素 : 将元素添加到 LinkedList 的末尾
  • add(index, Element): 可以将元素插入到指定位置。这是 LinkedList 在插入操作上非常高效的地方。·
  • get(index): 获取指定索引处的元素
  • remove(index | Element): 删除特定位置或特定的元素。
LinkedList<String> names = new LinkedList<>(); // 创建一个LinkedList对象
list.add("zs");
list.add("li"); // 添加元素
list.add(1,'ww');
list.get(1); // 获取元素
list.remove(1); // 移除索引处的元素
list.remove("zs"); // 移除特定的元素

链表的基础操作I

题目描述

构建一个单向链表,链表中包含一组整数数据。输出链表中的所有元素。

要求:
1. 使用自定义的链表数据结构
2. 提供一个 linkedList 类来管理链表,包含构建链表和输出链表元素的方法
3. 在 main 函数中,创建一个包含一组整数数据的链表,然后调用链表的输出方法将所有元素打印出来

输入描述

包含多组测试数据,输入直到文件尾结束。 

每组的第一行包含一个整数 n,表示需要构建的链表的长度。 

接下来一行包含 n 个整数,表示链表中的元素。

输出描述

每组测试数据输出占一行。
按照顺序打印出链表中的元素,每个元素后面跟一个空格。

输入示例
5
1 2 3 4 5
6
3 4 5 6 7 8
输出示例
1 2 3 4 5
3 4 5 6 7 8
提示信息

数据范围:

1 <= n <= 1000;

代码 

import java.util.Scanner;
class LinkList{
	public static class Node{
		int data;
		Node next;
		// 构造方法,初始化节点对象,参数为一个整数,表示节点的data字段
		public Node(int data){
			this.data = data;
			this.next = null;
		}
	}
	
	private int length;// 私有变量,存储链表长度
	private Node headNode;// 私有变量,存储链表头节点
	// 链表类的构造方法,用于初始化链表对象
	public LinkList(){
		this.headNode = null;
		 // 没有初始化 length, 使用默认值 0,表示链表长度为0
	}
	//在表尾插入数据
	public Node insert(int data) {
		this.length++;
		Node newNode = new Node(data);
        //没有头结点的情况:头结点为新节点
		if(this.headNode == null) {
			this.headNode = newNode;
			return this.headNode;
		}
        //存在头结点的情况:找到尾结点,接在后面
		else {
			Node currentNode = this.headNode;
			while(currentNode.next!=null) {
				currentNode = currentNode.next;
			}
			currentNode.next = newNode;
			return newNode;
		}
	}
	//打印所有元素
	public void printLinkList() {
		Node currentNode = this.headNode;
		while(currentNode!=null) {
			System.out.printf("%d%c",currentNode.data,currentNode.next==null?'\n':' ');
			currentNode = currentNode.next;
		}
	}


public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		while(sc.hasNextInt()) {
            //初始化新链表
			LinkList linkList = new LinkList();
            //表尾插入n个元素
			int n = sc.nextInt();
			for(int i = 0;i<n;i++) {
				linkList.insert(sc.nextInt());
			}
            //打印所有元素
			linkList.printLinkList();
		}
		sc.close();
	}
}

链表的基础操作II

题目描述

请编写一个程序,实现以下操作: 

构建一个单向链表,链表中包含一组整数数据,输出链表中的第 m 个元素(m 从 1 开始计数)。 

要求:
1. 使用自定义的链表数据结构
2. 提供一个 linkedList 类来管理链表,包含构建链表、输出链表元素以及输出第 m 个元素的方法
3. 在 main 函数中,创建一个包含一组整数数据的链表,然后输入 m,调用链表的方法输出第 m 个元素

输入描述

第一行包含两个整数 n 和 k,n 表示需要构建的链表的长度,k 代表输入的 m 的个数。 

接下来一行包含 n 个整数,表示链表中的元素。 

接下来一行包含 k 个整数,表示输出链表中的第 m 个元素。

输出描述

测试数据输出占 k 行。 

每行输出链表中的第 m 个元素。如果 m 位置不合法,则输出“Output position out of bounds.”。

输入示例
5 5
1 2 3 4 5
4 3 2 9 0
输出示例
4
3
2
Output position out of bounds.
Output position out of bounds.

 代码

import java.util.Scanner;
class LinkList{
	public static class Node{
		int data;
		Node next;
		public Node(int data){
			this.data = data;
			this.next = null;
		}
	}
	
	private int length;
	private Node headNode;
	public LinkList(){
		this.headNode = null;
	}
	
	public Node insert(int data) {
		this.length++;
		Node newNode = new Node(data);
		if(this.headNode == null) {
			this.headNode = newNode;
			return this.headNode;
		}
		else {
			Node currentNode = this.headNode;
			while(currentNode.next!=null) {
				currentNode = currentNode.next;
			}
			currentNode.next = newNode;
			return newNode;
		}
	}
	
	public void printLinkList() {
		Node currentNode = this.headNode;
		while(currentNode!=null) {
			System.out.printf("%d%c",currentNode.data,currentNode.next==null?'\n':' ');
			currentNode = currentNode.next;
		}
	}
    //在长度为n的链表中,打印第m个节点的data
	public void search(int n,int m) {
		if(m>n||m<=0) {
			System.out.println("Output position out of bounds.");
		}
		else {
			Node currentNode = this.headNode;
			for(int i = 0;i<m-1;i++) {
				currentNode = currentNode.next;
			}
			System.out.println(currentNode.data);
		}
	}
}
public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		LinkList linkList = new LinkList();
		int n = sc.nextInt();
		int k = sc.nextInt();
		for(int i = 0;i<n;i++) {
			linkList.insert(sc.nextInt());
		}
		 //在长度为n的链表中,打印第m个节点的data,循环k次
		for(int i = 0;i<k;i++) {
			linkList.search(n, sc.nextInt());
		}
		sc.close();
		
	}
}

链表的基础操作III

题目描述

请编写一个程序,实现以下链表操作:构建一个单向链表,链表中包含一组整数数据。

1. 实现在链表的第 n 个位置插入一个元素,输出整个链表的所有元素。
2. 实现删除链表的第 m 个位置的元素,输出整个链表的所有元素。

要求:

1. 使用自定义的链表数据结构。
2. 提供一个 linkedList 类来管理链表,包含构建链表、插入元素、删除元素和输出链表元素的方法。
3. 在 main 函数中,创建一个包含一组整数数据的链表,然后根据输入的 n 和 m,调用链表的方法插入和删除元素,并输出整个链表的所有元素。

输入描述

每次输出只有一组测试数据。

每组的第一行包含一个整数 k,表示需要构建的链表的长度。

第二行包含 k 个整数,表示链表中的元素。

第三行包含一个整数 S,表示后续会有 S 行输入,每行两个整数,第一个整数为 n,第二个整数为 x ,代表在链表的第 n 个位置插入 x。

S 行输入...

在 S 行输入后,后续会输入一个整数 L,表示后续会有 L 行输入,每行一个整数 m,代表删除链表中的第 m 个元素。

L 行输入...

输出描述

包含多组输出。

每组第一行输出构建的链表,链表元素中用空格隔开,最后一个元素后没有空格。

然后是 S 行输出,每次插入一个元素之后都将链表输出一次,元素之间用空格隔开,最后一个元素后没有空格;

如果插入位置不合法,则输出“Insertion position is invalid.”。

然后是 L 行输出,每次删除一个元素之后都将链表输出一次,元素之间用空格隔开,最后一个元素后没有空格;如果删除元素后链表的长度为0,则不打印链表。

如果删除位置不合法,则输出“Deletion position is invalid.”。

如果链表已经为空,执行删除操作时不需要打印任何数据。

输入示例
5
1 2 3 4 5
3
4 3
3 4
9 8
2
1
0
输出示例
1 2 3 3 4 5
1 2 4 3 3 4 5
Insertion position is invalid.
2 4 3 3 4 5
Deletion position is invalid.
提示信息

链表为空的时候,不打印

代码

import java.util.Scanner;
class LinkList{
	public static class Node{
		int data;
		Node next;
		public Node(int data){
			this.data = data;
			this.next = null;
		}
	}
	
	private int length;
	private Node headNode;
	public LinkList(){
		this.headNode = null;
	}
	//链表的表尾插入data
	public Node insert(int data) {
		this.length++;
		Node newNode = new Node(data);
		if(this.headNode == null) {
			this.headNode = newNode;
			return this.headNode;
		}
		else {
			Node currentNode = this.headNode;
			while(currentNode.next!=null) {
				currentNode = currentNode.next;
			}
			currentNode.next = newNode;
			return newNode;
		}
	}
	//链表的第 n 个位置插入 x
	public Node insert(int n,int x) {
		Node newNode = new Node(x);
		//插在头结点之前
		if(n==1) {
			this.length++;//长度加一
			newNode.next=this.headNode;
			this.headNode = newNode;
			return newNode;
		}
		//其他情况
		else {
			Node preNode = this.get(n-1);//找到前一个节点
			if(preNode!=null) {
				newNode.next = preNode.next;
				preNode.next = newNode;
				this.length++;//长度加一
				return newNode;
			}
			return null;
		}
		
	}
	
	//删除链表中的第 m 个元素
	public Node delete(int m) {
		//链表为空,或者超过链表长度
		if(this.headNode==null||m>this.length) {
			return null;
		}
		//删除头结点
		if(m==1) {
			Node deleteNode = this.headNode;
			this.headNode = this.headNode.next;
			this.length--;//长度减一
			return deleteNode;
		}
		//其他节点
		else {
			Node preNode = get(m-1);//找到前一个节点
			if(preNode!=null&&preNode.next!=null) {
				Node deleteNode = preNode.next;
				preNode.next = deleteNode.next;
				this.length--;//长度减一
				return deleteNode;
			}
		}
		return null;
		
	}
	
	public void printLinkList() {
		Node currentNode = this.headNode;
		while(currentNode!=null) {
			System.out.printf("%d%c",currentNode.data,currentNode.next==null?'\n':' ');
			currentNode = currentNode.next;
		}
	}
	
	public Node get(int n) {
		if(n>this.length||n<=0||this.headNode==null) {
			return null;
		}
		Node currentNode = this.headNode;
		for(int i = 0;i<n-1;i++) {
			currentNode = currentNode.next;
		}
		return currentNode;
	}
}


public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNextInt()) {
			LinkList linkList = new LinkList();
			int k = sc.nextInt();//输入链表长度
			//输入链表k个元素
			for(int i = 0;i<k;i++) {
				linkList.insert(sc.nextInt());
			}
			//链表的第 n 个位置插入 x
			int s = sc.nextInt();//插入s次
			//每次在链表的第 n 个位置插入 x
			for(int i = 0;i<s;i++) {
				int n = sc.nextInt();
				int x = sc.nextInt();
				LinkList.Node insertNode = linkList.insert(n, x);
				if(insertNode!=null) {
					linkList.printLinkList();
				}
				else {
					System.out.println("Insertion position is invalid.");
				}
			}
			
			
			//删除链表中的第 m 个元素
			int l = sc.nextInt();//删除l次
			//每次删除第m个数
			for(int i = 0;i<l;i++) {
				int m = sc.nextInt();
				LinkList.Node deleteNode = linkList.delete(m);
				if(deleteNode!=null) {
					linkList.printLinkList();
				}
				else {
					System.out.println("Deletion position is invalid.");
				}
			}
			
		}
		sc.close();
		
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值