- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有一些链接列表已经可用,我想将每个单独列表的第一个节点存储到另一个列表中,这样我就可以调用这个列表来显示原始列表。我认为我们必须使用两种不同的结构。我已经成功地保留了原始列表并使用单个列表的第一个节点的数组来显示它们,但我想创建一个单个列表的链表来实现相同的。它具有预期的输出,但正如我所说,我想使用链表而不是节点数组。
现在这就是我试图解决将链表数组替换为第一个节点的链表的问题的方法,每当我尝试调试代码时都会崩溃。请帮助我。
#include <stdio.h>
#include <stdlib.h>
struct node{
int number;
struct node*next;
};
typedef struct node Node;
Node* insertValue(Node * list, int value);
void display(Node*);
struct list_of_nodes {
Node *list;
struct list_of_nodes *next;
};
typedef struct list_of_nodes ListNode;
ListNode* insertNode(ListNode* head,Node* node);
int main()
{
ListNode *head=NULL;
Node *globalList = NULL, *lists[100];
int nbrOfLists, listNo, nbrOfVal, valNo, val,i=0,k;
CHECKER:
printf("\n\n Enter the number of lists (1 to 100):");
scanf("%d", &nbrOfLists);
if(nbrOfLists <= 0 || nbrOfLists > 100) //handling exceptional cases
{
printf("\n \n Number of Lists should be between 1 to 100"); // since array of node pointers contains 100 elements
goto CHECKER;
}
for(listNo = 0; listNo < nbrOfLists; listNo++)
{
printf("\n\n Enter the number of inputs to the list %d: \n ",listNo+1);
scanf("%d", &nbrOfVal);
lists[listNo] = NULL;
for(valNo = 0; valNo < nbrOfVal; valNo++) // to enter values in each individual list
{
printf("Enter node value %d:", valNo+1);
scanf("%d", &val);
// Here we insert the value in both lists
lists[listNo]= insertValue(lists[listNo], val); // original list has to be retained so storing in array lists
globalList = insertValue(globalList, val); // inserting node in combined list. This prevents an extra loop and merges the list elements into one.
}
head=insertNode(head,lists[listNo]); // CRASHING HERE
printf("\n The list %d is: ",listNo+1);
display(lists[listNo]); // display each list after input
}
printf("\n\n\n THE FINAL LIST IS: ");
display(globalList); //display combined list
printf("\n\n THE LISTS WERE: ");
while(i<nbrOfLists){ //original lists displayed
k=i+1;
printf("\n\n The list %d is: ",k);
display(lists[i]);
i++;
}
printf("\n\n");
return 0;
}
ListNode* insertNode(ListNode* head, Node* node){
ListNode *newNode, *m;
newNode = malloc(sizeof(ListNode));
newNode->list=node;
if(newNode == NULL)
{
newNode->next=NULL; // inserting first node
return newNode;
}
m = head;
while(m->next) // checking for right position in ordered list for new node
{
m = m->next;
}
newNode->next = m->next; // inserting new node
m->next = newNode;
return head;
}
Node* insertValue(Node * list, int value) // function to insert node in ordered manner into list
{
Node *newNode, *m;
newNode = malloc(sizeof(Node));
newNode->number=value;
if(list == NULL)
{
newNode->next=NULL; // inserting first node
return newNode;
}
if(value < list->number)
{
newNode->next = list; // inserting in end
return newNode;
}
m = list;
while(m->next) // checking for right position in ordered list for new node
{
if(value < m->next->number)
break;
m = m->next;
}
newNode->next = m->next; // inserting new node
m->next = newNode;
return list;
}
void display(Node*nodex){ // display node values in list
printf("%d ->",nodex->number);
nodex=nodex->next;
if(nodex)
return display(nodex);
else
return 0;
}
这是显示预期结果但带有节点数组的代码:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node{
int number;
struct node*next;
};
typedef struct node Node;
Node* insertValue(Node *list, int value);
void display(Node*);
int main()
{
Node *globalList = NULL, *lists[100];
int nbrOfLists, listNo, nbrOfVal, valNo, val, i = 0, k;
CHECKER:
printf("\n\n Enter the number of lists (1 to 100):");
scanf("%d", &nbrOfLists);
if(nbrOfLists <= 0 || nbrOfLists > 100) //handling exceptional cases
{
printf("\n \n Number of Lists should be between 1 to 100"); // since array of node pointers contains 100 elements
goto CHECKER;
}
for(listNo = 0; listNo < nbrOfLists; listNo++)
{
printf("\n\n Enter the number of inputs to the list %d: \n ", listNo + 1);
scanf("%d", &nbrOfVal);
lists[listNo] = NULL;
for(valNo = 0; valNo < nbrOfVal; valNo++) // to enter values in each individual list
{
printf("Enter node value %d:", valNo + 1);
scanf("%d", &val);
// Here we insert the value in both lists
lists[listNo] = insertValue(lists[listNo], val); // original list has to be retained so storing in array lists
globalList = insertValue(globalList, val); // inserting node in combined list. This prevents an extra loop and merges the list elements into one.
}
printf("\n The list %d is: ", listNo + 1);
display(lists[listNo]); // display each list after input
}
printf("\n\n\n THE FINAL LIST IS: ");
display(globalList); //display combined list
printf("\n\n THE LISTS WERE: ");
while(i < nbrOfLists){ //original lists displayed
k = i + 1;
printf("\n\n The list %d is: ", k);
display(lists[i]);
i++;
}
printf("\n\n");
return 0;
}
Node* insertValue(Node *list, int value) // function to insert node in ordered manner into list
{
Node *newNode, *m;
newNode = malloc(sizeof(Node));
newNode->number = value;
if(list == NULL)
{
newNode->next = NULL; // inserting first node
return newNode;
}
if(value < list->number)
{
newNode->next = list; // inserting in end
return newNode;
}
m = list;
while(m->next) // checking for right position in ordered list for new node
{
if(value < m->next->number)
break;
m = m->next;
}
newNode->next = m->next; // inserting new node
m->next = newNode;
return list;
}
void display(Node *nodex){ // display node values in list
printf("%d ->", nodex->number);
nodex = nodex->next;
if(nodex)
return display(nodex);
else
return 0;
}
如果您不明白这个问题,请告诉我。
最佳答案
经过大量讨论 chat ,我最终使用了与问题中的最后一个版本密切相关的代码:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int number;
struct node *next;
};
typedef struct node Node;
Node *insertValue(Node *list, int value);
void display(Node *);
struct list_of_nodes
{
Node *list;
struct list_of_nodes *next;
};
typedef struct list_of_nodes ListNode;
ListNode *insertNode(ListNode *head, Node *node);
int main(void)
{
ListNode *head = NULL;
Node *globalList = NULL, *lists[100];
int nbrOfLists, listNo, nbrOfVal, valNo, val, i = 0, k;
CHECKER:
printf("\n\n Enter the number of lists (1 to 100):");
scanf("%d", &nbrOfLists);
if (nbrOfLists <= 0 || nbrOfLists > 100)
{
printf("\n \n Number of Lists should be between 1 to 100");
goto CHECKER;
}
for (listNo = 0; listNo < nbrOfLists; listNo++)
{
printf("\n\n Enter the number of inputs to the list %d: \n ", listNo + 1);
scanf("%d", &nbrOfVal);
lists[listNo] = NULL;
for (valNo = 0; valNo < nbrOfVal; valNo++)
{
printf("Enter node value %d:", valNo + 1);
scanf("%d", &val);
lists[listNo] = insertValue(lists[listNo], val);
globalList = insertValue(globalList, val);
}
head = insertNode(head, lists[listNo]);
printf("\n The list %d is: ", listNo + 1);
display(lists[listNo]);
}
printf("\n\n\n THE FINAL LIST IS: ");
display(globalList);
printf("\n\n THE LISTS WERE: ");
while (i < nbrOfLists)
{
k = i + 1;
printf("\n\n The list %d is: ", k);
display(lists[i]);
i++;
}
printf("\n\n");
return 0;
}
ListNode *insertNode(ListNode *head, Node *node)
{
ListNode *newNode, *m;
newNode = malloc(sizeof(ListNode));
newNode->list = node;
newNode->next = NULL;
if (newNode == NULL)
{
fprintf(stderr, "Out of memory in %s\n", __func__);
exit(1);
}
if (head == NULL)
return newNode;
m = head;
while (m->next)
{
m = m->next;
}
newNode->next = m->next;
m->next = newNode;
return head;
}
Node *insertValue(Node *list, int value)
{
Node *newNode, *m;
newNode = malloc(sizeof(Node));
newNode->number = value;
newNode->next = NULL;
if (list == NULL)
return newNode;
if (value < list->number)
{
newNode->next = list;
return newNode;
}
m = list;
while (m->next)
{
if (value < m->next->number)
break;
m = m->next;
}
newNode->next = m->next;
m->next = newNode;
return list;
}
void display(Node *nodex)
{
printf("%d ->", nodex->number);
nodex = nodex->next;
if (nodex)
display(nodex);
}
使用示例数据文件(ll7.data
):
3
6 26 22 83 96 89 69
10 87 33 5 36 85 34 0 25 57 99
5 49 44 27 75 82
我编译了上面的ll7.c
:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror ll7.c -o ll7
$
并在 valgrind
下运行它它注意到代码像筛子一样泄漏(因为看不到免费的),但在其他方面给了它一个干净的健康证明。
$ valgrind --suppressions=suppressions ./ll7 < ll7.data
==7696== Memcheck, a memory error detector
==7696== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7696== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==7696== Command: ./ll7
==7696==
--7696-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--7696-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--7696-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
Enter the number of lists (1 to 100):
Enter the number of inputs to the list 1:
Enter node value 1:Enter node value 2:Enter node value 3:Enter node value 4:Enter node value 5:Enter node value 6:
The list 1 is: 22 ->26 ->69 ->83 ->89 ->96 ->
Enter the number of inputs to the list 2:
Enter node value 1:Enter node value 2:Enter node value 3:Enter node value 4:Enter node value 5:Enter node value 6:Enter node value 7:Enter node value 8:Enter node value 9:Enter node value 10:
The list 2 is: 0 ->5 ->25 ->33 ->34 ->36 ->57 ->85 ->87 ->99 ->
Enter the number of inputs to the list 3:
Enter node value 1:Enter node value 2:Enter node value 3:Enter node value 4:Enter node value 5:
The list 3 is: 27 ->44 ->49 ->75 ->82 ->
THE FINAL LIST IS: 0 ->5 ->22 ->25 ->26 ->27 ->33 ->34 ->36 ->44 ->49 ->57 ->69 ->75 ->82 ->83 ->85 ->87 ->89 ->96 ->99 ->
THE LISTS WERE:
The list 1 is: 22 ->26 ->69 ->83 ->89 ->96 ->
The list 2 is: 0 ->5 ->25 ->33 ->34 ->36 ->57 ->85 ->87 ->99 ->
The list 3 is: 27 ->44 ->49 ->75 ->82 ->
==7696==
==7696== HEAP SUMMARY:
==7696== in use at exit: 43,752 bytes in 471 blocks
==7696== total heap usage: 551 allocs, 80 frees, 49,880 bytes allocated
==7696==
==7696== LEAK SUMMARY:
==7696== definitely lost: 32 bytes in 2 blocks
==7696== indirectly lost: 688 bytes in 43 blocks
==7696== possibly lost: 0 bytes in 0 blocks
==7696== still reachable: 29,998 bytes in 310 blocks
==7696== suppressed: 13,034 bytes in 116 blocks
==7696== Rerun with --leak-check=full to see details of leaked memory
==7696==
==7696== For counts of detected and suppressed errors, rerun with: -v
==7696== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$
当输入来自文件时,并不是真正需要提示输出,这也是看不到数字的原因。当您在终端上键入时,终端驱动程序会将您键入的内容回显到屏幕上。当数据来自文件时,您看不到读取的字符。
抑制文件列出并抑制了 Mac OS X 10.10.3 Yosemite 运行时系统的各种泄漏。这就是为什么也有这么多内存在使用的原因;运行时系统使用大量内存。
如果全部是我的代码,会有很多不同的做法。应该添加很多错误检查和可以/应该完成的重构(尤其是“提取函数”),但这些更改并不是为了保留与问题中发布的代码的一些相似之处。
关于c - 在链接列表的末尾插入节点而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29719445/
我正在使用 JavaFX 8 创建一个应用程序。我使用拖/放动态更改网格 Pane 的内容。我希望每行或每行/列迭代 GridPane 内容。JavaFX 允许通过指定行和列在 GridPane 中添
我正在尝试将图像拖放到div上。图像没有被拖到div上并给出以下错误 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': pa
我正在 android studio 中创建内部构建 AR 导航。我正在寻找一种方法将 anchor 与其他 anchor 或 anchor 节点/节点“连接”起来。我不确定使用哪一个。基于我将强制用
我在 Hive 上运行一些作业:首先是 4 节点,然后是 2 节点。令我惊讶的是,我的 2 节点性能比我的 4 节点更好。 首先,我在一个 4 节点(4 个事件节点)上运行查询,然后关闭 2 个节点(
我有 Node* current ,我在其中存储指向列表“顶部”当前节点的指针。当我将一个新节点设置为当前节点时,出现错误: '=' : cannot convert from 'CircularDo
我是 dcos Mesos 的新手,在本地 Ubuntu 机器上安装了 dc os。 我可以查看 dcos 仪表板。 但我无法使用 dcos node ssh --master-proxy --lea
在 JavaFX 中,是否有类似 setLayout(); 的东西?或 setBounds(); ? 例如,我想将按钮定位到我想要的位置。 最佳答案 JavaFX 场景图上的所有内容都是 Node .
我正在开发一个 JavaFX 应用程序,其中我开发的类(从 javafx.scene.Parent 扩展)是根据用户在 ListView 控件中单击的条目动态创建的。 只是要清楚这个节点,它不是使用像
我正在尝试为节点-边缘关系创建一个类图,因为它可以在有向图中找到。我想传达的是,Nodes 引用了 Edges,Edges 也引用了 Nodes。每个 Edge 都恰好需要两个 Node(源和目标)。
在mapreduce作业期间,单个任务将在随机节点上运行,是否有任何方法限制应在其中运行任务的节点? 最佳答案 Hadoop不会选择节点来随机运行任务。考虑到数据局部性,否则将有很多网络开销。 任务与
有什么区别: a) nodetool 重建 b) nodetool 修复 [-pr] 换句话来说,各个命令到底是做什么的? 最佳答案 nodetool重建:类似于引导过程(当您向集群添加新节点时),但
我已将第一个 OneToMany 关系添加到我的 hibernate 3.6.10 项目中。这是一个类: /** * */ package com.heavyweightsoftware.leal
是否有可能找到正在监听触发当前函数的事件的元素? 在下面的代码中,event.target 返回 #xScrollPane 和 event.currentTarget 和 event 的最低子节点.f
我正在尝试覆盖我数据库中的一些数据。结构很简单,就是: recipes { user_1{ recipe_1{data} recipe_2{data} } user_2{
我使用 setInterval 来运行该函数,但它会多次执行函数 2... 如何在输入中插入一个值后执行函数 第一个输入与其余输入的距离不同 如何在插入 val(tab 选项)后将插入从 1 个输入移
我不知道代码有什么问题,但在 visual studio 中不断收到这些错误消息。 Error 18 error C1903: unable to recover from previous e
我正在尝试从其类中获取 SharePoint 搜索导航节点的对象。 var nodes = $("div.ms-qSuggest-listItem"); 我正在获取节点对象,现在想要获取“_promp
D:\nodeP>node main.js module.js:327 抛出错误; ^ 错误:在 Function.Module 的 Function.Module._resolveFilename
struct node{ int key, prior, cnt, val; node *l, *r; node(){} node(int nkey) : key(nkey),
我有以下代码使用迭代器将项目插入双链表。这就是我们被要求这样做的方式。代码有效,但问题是我有 24 字节的绝对内存泄漏。 NodeIterator insert(NodeIterator & itrP
我是一名优秀的程序员,十分优秀!