linked-list 相关问题

链表是一种数据结构,其中元素包含对下一个(以及可选的前一个)元素的引用。链接列表提供O(1)插入和删除具有已知内存位置的任何元素,O(1)列表并置,以及前(和可选后)位置的O(1)访问以及O(1)下一个元素访问。随机访问和随机索引插入/移除具有O(n)复杂性并且通常是未实现的。

XML 脚本运行以打印所需的输出

我有一个示例 XML 输入文件,其中包含以下内容: 我有一个示例 XML 输入文件,其中包含以下内容: <?xml version="1.0" encoding="utf-8"?> <infobases> <infobase author="Chartered Professional Accountants of Canada" levelDefOrder="Level 1,Level 2,Level 3,Level 4,Level 5,Level 6,Level 7,Level 8,Level 9,Level 10,Level 11,Level 12,Level 13,Normal Level" levels="Level 1,Level 2,Level 3,Level 4,Level 5,Level 6,Level 7,Level 8,Level 9,Level 10,Level 11,Level 12,Level 13" name="info_a" title="CPA Canada Standards and Guidance Collection"> <file level="Level 1" heading="XYZ1-L1">XYZ1-L1 <file level="Level 2" heading="XYZ1-L12">XYZ1-L12 <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> </file> </file> <file level="Level 1" heading="XYZ2-L1">XYZ2-L1</file> <file level="Level 1" heading="XYZ2-L1">XYZ2-L1 <file level="Level 2" heading="XYZ2-L12">XYZ2-L12</file> <file level="Level 2" heading="XYZ2-L123">XYZ2-L123 <file level="Level 3" heading="XYZ1-L123">XYZ1-L123 <file level="Level 4" heading="XYZ1-L123">XYZ1-L123</file> </file> </file> </file> </infobase> </infobases> 我想编写一个脚本来识别文件元素级别属性,并在打印文件元素的标题时给出适当的缩进。文件元素标题将获得 .ditamap 扩展名,该扩展名将其他文件元素作为子元素并忽略其他子元素(不在示例 xml 文件中)。如果文件元素没有子文件元素,则会添加 .dita 扩展名。 我用 javascript 编写了一个脚本,它倾向于进行正确的缩进,但分配的扩展名不正确。我得到所有文件元素标题的 .dita。这是代码: const fs = require('fs'); const XmlStream = require('xml-stream'); // Create a readable stream from the XML file const stream = fs.createReadStream('input1.xml'); // Create a writable stream to the output text file const outputStream = fs.createWriteStream('output.txt'); // Create a new XML stream parser const xmlParser = new XmlStream(stream); // Function to print headings with proper indentation function printHeadingsToFile(file, indentation = '') { // Calculate the indentation based on the level of the file const levelIndentation = ' '.repeat(parseInt(file.$.level.substr(6)) - 1); // Determine file extension based on child elements const fileExtension = file.file ? 'ditamap' : 'dita'; // Write the heading with indentation to the output file outputStream.write(`${indentation}${levelIndentation}${file.$.heading}.${fileExtension}\n`); // Check if there are nested files if (file.file) { // If nested files exist, recursively print their headings with increased indentation file.file.forEach(nestedFile => { printHeadingsToFile(nestedFile, `${indentation}${levelIndentation}`); }); } } // Event listener for when a new XML element is encountered xmlParser.on('startElement: file', function(element) { // Print headings for each file printHeadingsToFile(element); }); // Event listener for when the parsing ends xmlParser.on('end', function() { console.log('Parsing finished.'); // Close the output stream after finishing writing outputStream.end(); }); // Event listener for any errors during parsing xmlParser.on('error', function(err) { console.error('Error during parsing:', err); // Close the output stream if there is an error outputStream.end(); }); 我在这里得到的输出如下: XYZ1-L1.dita XYZ1-L12.dita XYZ1-L123.dita XYZ1-L123.dita XYZ1-L123.dita XYZ2-L1.dita XYZ2-L1.dita XYZ2-L12.dita XYZ2-L123.dita XYZ1-L123.dita XYZ1-L123.dita 预期输出: XYZ1-L1.ditamap XYZ1-L12.ditamap XYZ1-L123.dita XYZ1-L123.dita XYZ1-L123.dita XYZ2-L1.dita XYZ2-L1.ditamap XYZ2-L12.dita XYZ2-L123.ditamap XYZ1-L123.ditamap XYZ1-L123.dita 找到了使用不同 xml 解析器包的解决方案。 const fs = require('fs'); const sax = require('sax'); // Create a SAX parser const parser = sax.parser(true); // Create a write stream to a text file const outputStream = fs.createWriteStream('output.txt'); // Array to store file attributes let fileAttributes = []; // Event handlers for the SAX parser parser.onopentag = function (node) { // Check if the current node is a <file> element if (node.name === 'file') { // Extract attributes and push them to the array fileAttributes.push(node.attributes); } }; parser.onend = function () { // Parsing ends, log the final output //console.log('File attributes:', fileAttributes); // Function to determine the file extension function getFileExtension(currentLevel, nextLevel) { if (currentLevel < nextLevel) { return '.ditamap'; } else { return '.dita'; } } // Iterate through the fileAttributes array for (let i = 0; i < fileAttributes.length; i++) { const currentFile = fileAttributes[i]; const nextFile = fileAttributes[i + 1]; // Get the level numbers const currentLevel = parseInt(currentFile.level.match(/\d+/)[0]); const nextLevel = nextFile ? parseInt(nextFile.level.match(/\d+/)[0]) : 0; // Get the file extension const extension = getFileExtension(currentLevel, nextLevel); // Indentation based on the level const indentation = ' '.repeat((currentLevel - 1) * 4); // Prepare the text to write const textToWrite = indentation + currentFile.heading + extension + '\n'; // Write the text to the output stream outputStream.write(textToWrite); } // Close the output stream when done outputStream.end(); }; // Read XML file const xmlData = fs.readFileSync('input.xml', 'utf8'); // Parse XML data parser.write(xmlData).close(); 结果输出: L1.ditamap L2.ditamap L3.dita L3.dita L3.dita L1.dita L1.ditamap L2.dita L2.ditamap L3.ditamap L4.ditamap Level5Text.dita

回答 1 投票 0

向仅包含一个节点的循环链表添加一个节点

几周前我才听说链表,现在我开始关注循环链表。问题很简单:在只有一个元素的循环列表中,下一个节点就是它自己,即……

回答 2 投票 0

在 C 中合并两个链表时出现问题

我应该编写一个函数来合并(将一个放在另一个的末尾)两个单链表。用户在控制台输入一串数字,例如:1 2 3 4 0(0表示结束...

回答 2 投票 0

删除链表节点的函数不会在另一个函数中返回修改后的哨兵

这个deleteNode函数之前可以工作,但是minScore函数被设置为返回最大分数而不是最小值。我已经调试了6个小时,似乎当它进入计算时......

回答 1 投票 0

C 程序中链表释放的内存管理问题

我目前正在应对 2022 年代码来临第 9 天挑战,在释放 C 程序中动态分配的链表时遇到了意外问题。虽然我的代码运行顺利...

回答 1 投票 0

Python链表按索引号将节点插入喜欢列表时出现问题

我正在尝试将节点插入到喜欢列表中的特定索引中,尽管我让它适用于边界之外的索引以及前面和末尾的索引,但我似乎无法插入节点。 .

回答 1 投票 0

查找链表的中间

我正在尝试找到单链表的中间位置。这直接来自leetcode问题。 我知道如何使用列表来计算它,但我想知道为什么我的解决方案不起作用

回答 2 投票 0

向链表插入新节点时应该考虑哪些潜在错误?

我是 Go 新手。 我编写了一个函数来在链表中的给定索引处插入新节点。 除了超出范围的错误之外,我还应该考虑在此函数中处理哪些其他潜在错误?

回答 1 投票 0

在 while 循环中重用已释放的链表时出现问题

当我运行代码时,只有外循环的第一次迭代给出了有意义的输出。输出总数是正确的,但除第一个输出之外的所有剩余输出都打印“...

回答 1 投票 0

卡在Leetcode的案例3“两个和相加”

目前我正在每天进行Leetcode streak,我不知道为什么我的代码无法通过案例3。(我已经完成了代码的修改和追踪,但可能我有点笨,无法通过)找出错误...

回答 1 投票 0

realloc():在 while 循环中使用 realloc 和 free 时,旧大小无效并中止(核心转储)

我正在开发一个程序,该程序使用实现为的链表 typedef 结构节点{ 字符*字; 整数频率; 结构节点*下一个; }节点; 类型定义结构{ 节点*头; }二; 并且有

回答 1 投票 0

我的打印链接列表的功能没有打印它

我有一个代码,我正在尝试做一些歌曲的播放列表。它们将位于通过链接列表连接的结构上,其中包含歌曲名称和时间。 在程序中,有一个函数执行...

回答 1 投票 0

LinkedList merge_sorted无限循环

在对链接列表使用函数 merge_sorted 时,我不断陷入无限循环。请帮忙。谢谢 :) 我检查了 merge_sort 函数,看起来没问题。它更新 curr1 和

回答 1 投票 0

用链表的哈希集去重,看不懂代码的工作流程

问题是从未排序的链表中删除重复项。使用哈希集可以跟踪该集中已有的值。 我不明白 if, else 块在做什么。 尤其是那个...

回答 1 投票 0

如何从表格数据生成列表

我是 php 和 mysql 的新手。我正在尝试创建一个页面,该页面将从将显示为列表的表中检索数据。 抱歉,我刚刚开始自学php和mysql。我真的很感激...

回答 1 投票 0

递归反转链表

我在链表中定义了一个节点: typedef 结构 abc { 整数ID; 结构 abc *下一个; }节点; 我想递归地反转链表。我将头指针传递给 fu...

回答 8 投票 0

从排序链表中删除重复的节点。为什么我的输出是错误的?

给定一个已排序的链表,删除所有具有重复数字的节点,只留下与原始列表不同的数字。 例子: 给定 1->2->3->3->4->4->5->null,ret...

回答 2 投票 0

遍历 raylib 中的链表会出现分段错误(核心已转储)

我正在尝试在 Raylib 应用程序中绘制文本。当我遍历链接列表时,其中的内容要么应用程序不打印任何内容,要么给出分段错误(核心转储) 案例 1:分段错误...

回答 1 投票 0

整数文件无法正确打开

我很抱歉,这可能是一个解释非常简单的问题,但我似乎找不到它。我目前正在尝试用 C 构建一个基本数据库。我需要保存一个结构链接...

回答 1 投票 0

Go 中的 For 循环无限运行,同时将节点添加到链表

我是Golang新手,正在尝试实现其中的一些数据结构和算法, 我写了一个 go 函数,将数据添加到列表中,但由于某种原因,它进入无限循环,我有

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.