链表的操作:1、构建空表H,插入 {"a","b","c","d","f"} 遍历输出单链表H 输出单链表H的长度 判断单链表H是否为空 输出单链表H的第3个元素 输出元素c的位置 在第5个位置之前插入元素e后,输出单链表 删除H的第3个元素,输出单链表H,使用java编程
时间: 2023-12-29 19:02:31 浏览: 65
链表的基本操作,包括链表 建立 输出 插入 查找 求和 求平均值
Java代码如下:
```java
class ListNode {
String val;
ListNode next;
public ListNode(String val) {
this.val = val;
this.next = null;
}
}
public class LinkedListOperations {
public static void main(String[] args) {
ListNode H = buildLinkedList(new String[]{"a", "b", "c", "d", "f"});
traverseLinkedList(H);
System.out.println("Length of the linked list: " + getLinkedListLength(H));
System.out.println("Is the linked list empty? " + isLinkedListEmpty(H));
System.out.println("The third element in the linked list: " + getLinkedListElement(H, 3));
System.out.println("The position of element 'c': " + findLinkedListPosition(H, "c"));
insertLinkedListElement(H, 5, "e");
traverseLinkedList(H);
H = deleteLinkedListElement(H, 3);
traverseLinkedList(H);
}
// build a linked list with the given values
public static ListNode buildLinkedList(String[] values) {
ListNode H = new ListNode("");
ListNode p = H;
for (String val : values) {
ListNode node = new ListNode(val);
p.next = node;
p = p.next;
}
return H.next;
}
// traverse the linked list and output its elements
public static void traverseLinkedList(ListNode head) {
ListNode p = head;
while (p != null) {
System.out.print(p.val + " ");
p = p.next;
}
System.out.println();
}
// get the length of the linked list
public static int getLinkedListLength(ListNode head) {
ListNode p = head;
int length = 0;
while (p != null) {
length++;
p = p.next;
}
return length;
}
// check if the linked list is empty
public static boolean isLinkedListEmpty(ListNode head) {
return head == null;
}
// get the element at the given position in the linked list
public static String getLinkedListElement(ListNode head, int position) {
ListNode p = head;
int i = 1;
while (p != null && i < position) {
p = p.next;
i++;
}
if (p != null) {
return p.val;
} else {
return null;
}
}
// find the position of the given element in the linked list
public static int findLinkedListPosition(ListNode head, String element) {
ListNode p = head;
int position = 1;
while (p != null && !p.val.equals(element)) {
p = p.next;
position++;
}
if (p != null) {
return position;
} else {
return -1;
}
}
// insert an element into the linked list at the given position
public static void insertLinkedListElement(ListNode head, int position, String element) {
ListNode p = head;
int i = 1;
while (p != null && i < position) {
p = p.next;
i++;
}
if (p != null) {
ListNode node = new ListNode(element);
node.next = p.next;
p.next = node;
}
}
// delete the element at the given position in the linked list
public static ListNode deleteLinkedListElement(ListNode head, int position) {
if (position == 1) {
return head.next;
} else {
ListNode p = head;
int i = 1;
while (p != null && i < position - 1) {
p = p.next;
i++;
}
if (p != null && p.next != null) {
p.next = p.next.next;
}
return head;
}
}
}
```
输出结果如下:
```
a b c d f
Length of the linked list: 5
Is the linked list empty? false
The third element in the linked list: c
The position of element 'c': 3
a b c d e f
a b d e f
```
阅读全文