java链表的操作:1、构建空表H,插入 {a,b,,,f} 遍历输出单链表H 输出单链表H的长度
时间: 2023-12-17 09:05:33 浏览: 157
Java中链表的操作可以通过LinkedList类实现。具体的操作如下:
1. 构建空表H
```java
LinkedList<String> H = new LinkedList<String>();
```
2. 插入 {a,b,,,f}
```java
H.add("a");
H.add("b");
H.add(null);
H.add(null);
H.add(null);
H.add("f");
```
注意:因为是单链表,所以需要使用add()方法进行尾部插入,而不能使用set()方法进行指定位置插入。
3. 遍历输出单链表H
```java
for(String s : H){
System.out.print(s + "->");
}
```
输出结果为:a->b->null->null->null->f->
4. 输出单链表H的长度
```java
System.out.println("链表H的长度为:" + H.size());
```
输出结果为:链表H的长度为:6
相关问题
链表的操作:1、构建空表H,插入 {"a","b",",","f"} 遍历输出单链表H 输出单链表H的长度 判断单链表H是否为空 输出单链表H的第3个元素 输出元素c的位置 在第5个位置之前插入元素e后,输出单链表 删除H的第了个元素,输出单链表H用java编程
//定义节点类
class ListNode{
String val;
ListNode next;
ListNode(String val){
this.val = val;
}
}
public class LinkedListOperations {
public static void main(String[] args) {
//构建空表H
ListNode H = new ListNode(null);
//插入 {"a","b",",","f"}
ListNode p = H;
String[] arr = {"a","b",",","f"};
for(int i = 0; i < arr.length; i++){
ListNode node = new ListNode(arr[i]);
p.next = node;
p = node;
}
//遍历输出单链表H
p = H.next;
while(p != null){
System.out.print(p.val + " ");
p = p.next;
}
System.out.println();
//输出单链表H的长度
int length = getLength(H);
System.out.println("单链表H的长度为:" + length);
//判断单链表H是否为空
boolean isEmpty = isEmpty(H);
System.out.println("单链表H是否为空:" + isEmpty);
//输出单链表H的第3个元素
String val = getVal(H, 3);
System.out.println("单链表H的第3个元素为:" + val);
//输出元素c的位置
int index = getIndex(H, "c");
System.out.println("元素c的位置为:" + index);
//在第5个位置之前插入元素e后,输出单链表
insert(H, 5, "e");
p = H.next;
while(p != null){
System.out.print(p.val + " ");
p = p.next;
}
System.out.println();
//删除H的第了个元素,输出单链表
delete(H, 1);
p = H.next;
while(p != null){
System.out.print(p.val + " ");
p = p.next;
}
}
//获取单链表长度
public static int getLength(ListNode H){
int length = 0;
ListNode p = H.next;
while(p != null){
length++;
p = p.next;
}
return length;
}
//判断单链表是否为空
public static boolean isEmpty(ListNode H){
if(H.next == null){
return true;
}else{
return false;
}
}
//获取单链表的第index个元素
public static String getVal(ListNode H, int index){
ListNode p = H.next;
int i = 1;
while(p != null && i < index){
p = p.next;
i++;
}
if(p == null || i > index){
return null;
}else{
return p.val;
}
}
//获取元素val在单链表中的位置
public static int getIndex(ListNode H, String val){
ListNode p = H.next;
int index = 1;
while(p != null){
if(p.val.equals(val)){
return index;
}
p = p.next;
index++;
}
return -1;
}
//在第index个位置之前插入元素val
public static void insert(ListNode H, int index, String val){
ListNode p = H;
int i = 1;
while(p != null && i < index){
p = p.next;
i++;
}
if(p == null || i > index){
return;
}
ListNode node = new ListNode(val);
node.next = p.next;
p.next = node;
}
//删除第index个元素
public static void delete(ListNode H, int index){
ListNode p = H;
int i = 1;
while(p != null && i < index){
p = p.next;
i++;
}
if(p == null || i > index || p.next == null){
return;
}
p.next = p.next.next;
}
}
链表的操作:1、构建空表H,插入 {"a","b","c","d","f"} 遍历输出单链表H 输出单链表H的长度 判断单链表H是否为空 输出单链表H的第3个元素 输出元素c的位置 在第5个位置之前插入元素e后,输出单链表 删除H的第3个元素,输出单链表H,使用java编程
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
```
阅读全文