Java文件输入输出流实现链表操作

Problem 

【问题描述】输入N(3<=N<=10)个数,依次将这些数插入到结构为
typedef struct _tagDList {
   int data;
   struct _tagDList *pPre, *pNext;
} DLIST;
的双链表表头;再输入一个数num,从链表中删除值为num的结点。
【输入文件】从文件dlist.in读取输入。
第一行是一个数字n(<=n<=10),代表双链表的结点数。
第二行是n个数字,代表各结点的数值,中间以空格间隔。
第三行是一个数字,代表将要删除的结点的数值。
【输出文件】将操作后链表结点值输出到文件dlist.out,如果链表为空,输出-1。
【输入样例】
8
12 34 56 6 8 12 67
34
【输出样例】
67 12 8 6 56 12
【样例说明】删除链表中值为34的节点后链表节点输出值为67 12 8 6 56 12
【评分标准】结果正确则该测试点得满分,否则该测试点得0分。

在之前已经讲到了如何在Java中使用文件输入输出:https://blog.csdn.net/m0_73814009/article/details/134070728

链表的基本操作可以参考其他文章,我们这里不做多赘述。

代码实现如下:

import java.io.*;
import java.util.StringTokenizer;

class DLIST {
    int data;
    DLIST pPre, pNext;

    DLIST(int data) {
        this.data = data;
        pPre = null;
        pNext = null;
    }
}

public class everydayjava {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("dlist.in"));
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        DLIST head = new DLIST(Integer.parseInt(st.nextToken()));
        DLIST tail = head;

        for (int i = 1; i < n; i++) {
            int data = Integer.parseInt(st.nextToken());
            DLIST newNode = new DLIST(data);
            newNode.pPre = tail;
            tail.pNext = newNode;
            tail = newNode;
        }

        int numToDelete = Integer.parseInt(br.readLine());
        br.close();

        DLIST current = head;
        while (current != null) {
            if (current.data == numToDelete) {
                if (current.pPre != null) {
                    current.pPre.pNext = current.pNext;
                } else {
                    head = current.pNext;
                    if (head != null) {
                        head.pPre = null;
                    }
                }

                if (current.pNext != null) {
                    current.pNext.pPre = current.pPre;
                }
            }
            current = current.pNext;
        }

       
        DLIST reversedHead = reverseList(head);

        FileWriter fw = new FileWriter("dlist.out");
        if (reversedHead == null) {
            fw.write("-1\n");
        } else {
            current = reversedHead;
            while (current != null) {
                fw.write(current.data + " ");
                current = current.pNext;
            }
            fw.write("\n");
        }
        fw.close();
    }

 
    private static DLIST reverseList(DLIST head) {
        DLIST current = head;
        DLIST newHead = null;

        while (current != null) {
            DLIST temp = current.pNext;
            current.pNext = newHead;
            current.pPre = null;
            if (newHead != null) {
                newHead.pPre = current;
            }
            newHead = current;
            current = temp;
        }

        return newHead;
    }
}

其中,StringTokenizer 是 Java 标准库中的一个类,用于将字符串按照指定的分隔符切分成多个部分(标记)。通常,它用于处理包含多个值的字符串,例如从文件或用户输入中读取的一行文本,其中各个值之间用特定的分隔符(通常是空格或逗号)分隔。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XforeverZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值