KiKi学习了结构体和指针,他了解了结构体类型可以定义包含多个不同类型成员,而指针本质是内存地址,是引用数据的另外一种方式。现在他想将多个输入的数据通过结构体和指针的方式连接在一起,形成一个单向链表,即:每个结点是结构体类型,包括整型数据成员(data)和结构体指针类型成员(next),每个结点的指针部分指向下一个输入的结点。具体建立过程如下:先输入n个整数,按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。输出删除后的单链表信息。
包括三行:第一行输入数据个数n (3≤n≤100);第二行依次输入n个整数,用空格分隔;
第三行输入欲删除数据m。
包括两行:第一行输出完成删除后的单链表长度;
第二行依次输出完成删除后的单链表数据。
5 1 2 2 3 4 2
3 1 3 4
import java.util.Scanner;
import java.util.LinkedList;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
class LinkNode {
public int val;
public LinkNode next;
public LinkNode (int val) {
this.val = val;
}
public LinkNode(int val, LinkNode next) {
this.val = val;
this.next = next;
}
}
class MyLinkedList {
public LinkNode head;
public LinkNode last;
public Integer size = 0;
public void addList(int val) {
if (head == null) {
this.head = new LinkNode(val);
this.last = head;
this.size = 1;
return;
} else {
LinkNode cur = new LinkNode(val);
this.last.next = cur;
this.last = cur;
this.size++;
}
}
public void removeAll(int key) {
if (this.head == null) return;
LinkNode cur = this.head.next;
LinkNode prev = this.head;
while (cur != null) {
if (cur.val == key) {
prev.next = cur.next;
this.size--;
} else {
prev = cur;
}
cur = cur.next;
}
if (this.head.val == key) {
this.head = this.head.next;
this.size--;
}
}
public void display() {
LinkNode cur = this.head;
while (cur != null) {
System.out.printf("%d ", cur.val);
cur = cur.next;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n = in.nextInt();
MyLinkedList list = new MyLinkedList();
for (int i = 0; i < n; i++) {
list.addList(in.nextInt());
}
list.removeAll(in.nextInt());
System.out.println(list.size);
list.display();
}
} //模拟实现单链表
import java.util.Scanner;
class ListNode {
public int val;
public ListNode next;
public ListNode (int val) {
this.val = val;
}
public ListNode (int val, ListNode next) {
this.val = val;
this.next = next;
}
}
class MyLinkedList {
public ListNode head;
public void addLast(int val) {
if (this.head == null) {
this.head = new ListNode(val);
return;
}
ListNode cur = head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = new ListNode(val);
}
public int getSize() {
ListNode cur = head;
int size = 0;
while (cur != null) {
size++;
cur = cur.next;
}
return size;
}
public void removeAllKey(int key) {
if (this.head == null) return;
ListNode cur = this.head.next;
ListNode prev = this.head;
while (cur != null) {
if (cur.val == key) {
prev.next = cur.next;
} else {
prev = cur;
}
cur = cur.next;
}
if (this.head.val == key) {
this.head = this.head.next;
}
}
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
MyLinkedList list = new MyLinkedList();
int len = scan.nextInt();
while (len != 0) {
len--;
list.addLast(scan.nextInt());
}
list.removeAllKey(scan.nextInt());
System.out.println(list.getSize());
list.display();
}
} //纯数组解法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
int rm = in.nextInt();
int k = 0;
for (int i = 0; i < n; i++) {
while (i+k < n && arr[i+k] == rm){
k++;
}
if (k+i < n){
arr[i] = arr[k+i];
}
}
System.out.println(arr.length - k);
for (int i = 0; i < arr.length - k; i++) {
System.out.print(arr[i] + " ");
}
}
} package com.kingwan.niuke;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
/**
* Created by kingwan on 2020/10/9.
* 说明:链表的迭代方式
*/
public class Main {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
LinkedList<Integer> list = new LinkedList();
//填充链表
while (n>0){
list.add(scanner.nextInt());
n--;
}
int del = scanner.nextInt();
//使用迭代器迭代--注意:链表不是数组,使用for循环会抛异常的
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()){
if(iterator.next()==del){
iterator.remove();//符合条件的删除
}
}
//按要求输出
System.out.println(list.size());
for (Integer integer : list) {
if(integer!=del)
System.out.print(integer+" ");
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
//输入需要输入数字个总个数
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
list.add(sc.nextInt());
}
//输入删除的数字
int m = sc.nextInt();
//循环遍历集合中的数字
for (int index = 0; index < list.size(); ) {
if(m == list.get(index)){
list.remove(list.get(index));
//如果集合里面有和输入的数字相同就减去。集合索引不增加
}else{
//如果集合里面没有相同数字,不进行操作,增加索引值。
index++;
}
}
//打印输出删除后集合的长度
System.out.println(list.size());
//将集合转换为数组,用于遍历
Object[] obj = list.toArray();
for (int i = 0; i < obj.length; i++) {
if(i != obj.length){
System.out.print(obj[i]+" ");
}else{
System.out.print(obj[i]);
}
}
}
}