首页 > 试题广场 >

KiKi学结构体和指针

[编程题]KiKi学结构体和指针
  • 热度指数:9930 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
KiKi学习了结构体和指针,他了解了结构体类型可以定义包含多个不同类型成员,而指针本质是内存地址,是引用数据的另外一种方式。现在他想将多个输入的数据通过结构体和指针的方式连接在一起,形成一个单向链表,即:每个结点是结构体类型,包括整型数据成员(data)和结构体指针类型成员(next),每个结点的指针部分指向下一个输入的结点。具体建立过程如下:先输入n个整数,按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。输出删除后的单链表信息。


输入描述:
包括三行:
第一行输入数据个数n (3≤n≤100);

第二行依次输入n个整数,用空格分隔;

第三行输入欲删除数据m。



输出描述:
包括两行:

第一行输出完成删除后的单链表长度;

第二行依次输出完成删除后的单链表数据。


示例1

输入

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();
    }
}

发表于 2023-05-02 12:11:34 回复(0)
//模拟实现单链表
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();
    }
}

发表于 2021-10-25 20:25:41 回复(0)
//纯数组解法
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] + " ");
        }
    }
}

发表于 2021-10-24 12:30:08 回复(0)
 import java.util.Scanner;
         import java.util.ArrayList;
public class Main
{
   
    public static void main(String[] args)
    {
        ArrayList<Integer> arr=new ArrayList<>();
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        for(int i=0;i<n;i++)
        {
            arr.add(s.nextInt());
        }
        int m=s.nextInt();
        for(int i=0;i<n;i++)
        {
            if(arr.get(i)==m)
            {
                arr.remove(i);
                i--;
            }
        }
        System.out.println(arr.size());
        for(int i:arr)
        {
            System.out.print(i+" ");
        }
        
        
    }
}
运行不了?
发表于 2020-11-09 18:22:44 回复(0)
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+" ");
        }
    }
}

发表于 2020-10-09 19:17:41 回复(0)
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]);
            }

        }
    }
}
编辑于 2020-07-06 09:53:36 回复(0)
import java.util.*;
public class Main{
public static void main(String[] args){

ArrayList<Integer> stu=new ArrayList<Integer>();
Scanner sc=new Scanner(System.in);

int num=sc.nextInt();
int[] temp=new int[num];
for(int i=0;i<temp.length;i++){
temp[i]=sc.nextInt();
stu.add(temp[i]);
}

int num2=sc.nextInt();//num2为要删除的数据
Iterator<Integer> iterator = stu.iterator();//使用迭代器
while (iterator.hasNext()){
int s=iterator.next();
if(s==num2){
iterator.remove();
}
}
System.out.println(stu.size());
for(int s:stu){
System.out.print(s+" ");
}
}
}
编辑于 2020-04-13 23:00:46 回复(0)

问题信息

上传者:牛客309119号
难度:
8条回答 4491浏览

热门推荐

通过挑战的用户

查看代码
KiKi学结构体和指针