解释这行代码Node = int(NodeData[i,0]-1)
时间: 2024-06-05 07:08:51 浏览: 8
这行代码的作用是根据输入的节点数据 `NodeData` 中的第 `i` 行,提取该行的第一个元素(即该行对应的节点编号),并将其减一后转换为整数类型,赋值给变量 `Node`。其中减一是因为在 Python 中,列表和数组的编号是从0开始的,而节点编号一般从1开始,所以需要将节点编号减去1,以便在后续的处理中正确地索引节点的信息。
相关问题
#include <stdio.h> #include <stdlib.h> struct link { int data; struct link *next; }; struct link *AppendNode(struct link *head,int data); void DisplyNode(struct link *head); void DeleteMemory(struct link *head); struct link * DeleteNode(struct link *head,int data); struct link *InsertNode(struct link *head, int nodeData); int main() { char c; int data = 0; struct link head = NULL; / 链表头指针 / while (1) { scanf("%d",&data); if (data==-1) break; head = AppendNode(head,data);/ 向head为头指针的链表末尾添加节点 / } DisplyNode(head); / 显示当前链表中的各节点信息 / scanf("%d",&data); head = InsertNode(head,data); DisplyNode(head); / 显示当前链表中的各节点信息 / DeleteMemory(head); / 释放所有动态分配的内存 */ return 0; } struct link *AppendNode(struct link *head,int data){ struct link pnew,ptail; pnew=head; if(head==NULL){ head=(struct link)malloc(sizeof(struct link)); head->data=data; head->next=NULL; }else{ while(pnew!=NULL){ ptail=pnew; pnew=pnew->next; } pnew=(struct link)malloc(sizeof(struct link)); pnew->data=data; ptail->next=pnew; pnew->next=NULL; } return head; }; void DisplyNode(struct link *head){ struct link *p; p=head; while(p!=NULL){ if(p->next !=NULL) printf("%d->",p->data); else{ printf("%d\n",p->data); break; } p=p->next; } } struct link *InsertNode(struct link *head, int nodeData){ struct link *p,q,t; p=head; if(head->data>nodeData){ head=(struct link)malloc(sizeof(struct link)); head->data=nodeData; head->next=p; }else{ while(1){ if(p->next->data>nodeData) { q=(struct link)malloc(sizeof(struct link)); t=p->next; p->next=q; q->data=nodeData; q->next=t; break; } p=p->next; } } return head; }; void DeleteMemory(struct link *head) { free (head); }
这段代码存在几个问题:
1. 在结构体中,head应该是指向struct link的指针类型,而不是直接使用struct link类型。
2. 在AppendNode函数中,pnew和ptail应该都是指向struct link的指针类型,而不是直接使用struct link类型;同时,在pnew指向最后一个节点的情况下,需要把新节点pnew挂到ptail的后面,而不是直接把pnew赋值给head。
3. 在InsertNode函数中,q和t也应该是指向struct link的指针类型,而不是直接使用struct link类型;同时,在链表中插入节点时,需要特别注意插入位置是在头节点前面还是后面。
4. 在DeleteMemory函数中,应该释放整个链表,而不是只释放头节点。
5. 在代码中缺少头文件的换行,需要添加一些换行符来让代码更清晰易读。
下面是修改后的代码:
帮我重新修改一下刚才给的nodeInformation的代码:代码名为nodeInformation,public int[] nodeX = new int[1000];//节点的X坐标 public int[] nodeY = new int[1000];//节点的Y坐标 public int[,] chilernNode = new int[1000, 6];//节点链接的节点名称 public int[] nodeType = new int[1000];//节点的类型 public int[] nodename = new int[1000];//节点的名称 这是开头的基本定义,IJSO保存在项目的Asset里面
好的,以下是修改后的代码:
```csharp
using;
using System.IO;
System;
public class NodeInformation : MonoBehaviour
{
public int[] nodeX = new int[1000];
public int[]Y = new int[1000];
public int[,] chilernNode = new int[1000, 6];
public int[] nodeType = new int[1000];
public int[] nodename = new int[1000];
private string savePath;
private void Awake()
{
savePath = Application.dataPath + "/nodeData.json";
LoadNodeData();
}
private void OnApplicationQuit()
{
SaveNodeData();
}
private void LoadNodeData()
{
if (File.Exists(savePath))
{
string jsonData = File.ReadAllText(savePath);
NodeData savedData = JsonUtility.FromJson<NodeData>(jsonData);
nodeX = savedData.nodeX;
nodeY = savedData.nodeY;
chilernNode = savedData.chilernNode;
nodeType = savedData.nodeType;
nodename = savedData.nodename;
}
else
{
// 创建新的保存文件
nodeX = new int[1000];
nodeY = new int[1000];
chilernNode = new int[1000, 6];
nodeType = new int[1000];
nodename = new int[1000];
SaveNodeData();
}
}
private void SaveNodeData()
{
NodeData nodeData = new NodeData();
nodeData.nodeX = nodeX;
nodeData.nodeY = nodeY;
nodeData.chilernNode = chilernNode;
nodeData.nodeType = nodeType;
nodeData.nodename = nodename;
string jsonData = JsonUtility.ToJson(nodeData);
File.WriteAllText(savePath, jsonData);
}
}
[Serializable]
public class NodeData
{
public int[] nodeX;
public int[] nodeY;
public int[,] chilernNode;
public int[] nodeType;
public int[] nodename;
}
```
请确保将 `nodeData.json` 文件保存在项目的 `Asset` 文件夹中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)