- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的C代码是我自己写的原始链表的方法。它使用一个名为 lnode 的结构。我知道这不是最好/最有效的方法,但我的想法是:创建基节点,使用“迭代器”指针,这里是 q,它指向列表中的最后一个节点,然后添加一个新节点.
以下代码将无法编译。我找不到原因,但它讨厌这一行
struct lnode *q= malloc(sizeof(struct lnode));
关于实现这个想法有什么建议吗?提前致谢。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
struct lnode{
int value;
struct lnode *nextnode;
};
int main(){
struct lnode *startnode = malloc(sizeof(struct lnode));
startnode->value=0;
startnode->nextnode=NULL;
struct lnode *q= malloc(sizeof(struct lnode));
int i = 0;
for(i=0;i<10;i++){
struct lnode *p = malloc(sizeof(struct lnode));
p= q->nextnode;
p->value=i;
p->nextnode=NULL;
q=p;
}
return 0;
}
我想指出我是新手。我正在使用 Watcom 编译器(为什么?我的电脑很旧,我只需要这些练习程序)日志输出是
structure1.c(17): Error! E1063: Missing operand structure1.c(17):
Warning! W111: Meaningless use of an expression structure1.c(17):
Error! E1009: Expecting ';' but found 'struct' structure1.c(17):
Error! E1011: Symbol 'lnode' has not been declared structure1.c(17):
Error! E1011: Symbol 'q' has not been declared structure1.c(17):
Error! E1014: Left operand must be an 'lvalue' structure1.c(19):
我按照给出的建议更改了代码,新代码是这样的:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
struct lnode{
int value;
struct lnode *nextnode;
};
int main(){
struct lnode *startnode = (struct lnode *)malloc(sizeof(struct lnode));
struct lnode *q;
startnode->value=0;
startnode->nextnode=NULL;
q = malloc(sizeof(struct lnode));
doLoop(q);
return 0;
}
void doLoop(struct lnode *q){
int i = 0;
for(i=0;i<10;i++){
struct lnode *p = (struct lnode *)malloc(sizeof(struct lnode));
q->nextnode=p;
p->value=i;
p->nextnode=NULL;
printf("%i, %i\n",p->value,q->value);
q=p;
}
}
我打印了列表中每个节点的“值”值以及先前的值。除了第一次迭代会产生奇怪的输出外,它可以正常工作。
最佳答案
我怀疑编译器(例如 Microsoft 编译器)仅支持 C89 标准,不允许混合代码和声明。将 q
的声明移至范围顶部:
int main(){
struct lnode *startnode = (struct lnode *)malloc(sizeof(struct lnode));
struct lnode *q
startnode->value=0;
startnode->nextnode=NULL;
q = malloc(sizeof(struct lnode));
关于C LinkedList 示例无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9687457/
我想添加 LinkedList (我们称之为列表 A)到 LinkedList> (称之为列表 B)。执行此操作后,我需要更改列表 A 的值并将其再次添加到列表 B,但不更改已存储在列表 B 中的值。
更新:感谢所有的回答。我发现的最干净的解决方案是这个: if ( k(Arrays.asList(new LinkedList<>())); 我有一个递归方法,可以从列表中生成所有“n 选 k”组合。
在我的 Java 应用程序中,以下两个都将编译和运行,并产生所需的结果。 //"Rotate" the list items one place to the left. myLinkedList.a
我写了一个LinkedList接受 Nodes 的类存储 Integers . 然后我创建了一个 LinkedList stack = new LinkedList() ,并添加了 Node s 如果
这个问题在这里已经有了答案: What does it mean to "program to an interface"? (33 个答案) 关闭 9 年前。 新手 Java 问题: 谁能解释一下
我有一个问题。我无法并排输出我的 LinkedList。我问了这个问题,但遗憾的是我的老师告诉我不要更改方法头或使用 java 库(如日历)。我得到了很多关于使用它或更改方法头的建议。我是根据年级而定
这里有什么问题?。我正在尝试使用邻接列表,通过利用 util 包中的集合来实现图形数据结构。这里 LinkedList array which holds some integer. Each ele
这个问题已经有答案了: Reversing a linked list in Java, recursively (33 个回答) 已关闭10 年前。 如何使用 linkedList 类中的方法以相反
我需要实现一个 LinkedList,到目前为止,我已经编写了在列表中按顺序插入值的方法。我有我的节点 front 作为我的类的实例数据,当创建我的第一个值并尝试将 front 的 next 值设置为
目前,我的 LinkedList(不是 Java 的)类中有一个方法,可以将单个节点添加到 LinkedList 中,如下所示: public void add(int index, T v) {
我正在编写一个读取 XML 文件的类,该 XML 使用“sax”类进行解析。在我的 XML 文件中,我创建了“for”标签和“宏”,使 for 循环能够写入 XML,例如: Th
我正在处理一个 C++ 作业,我将在一个链表的链表上创建一个搜索引擎。根据要求,我不能使用其他库和 STL。 基本上它会是这样的(我从小列表中删除了变量,因为它们是不相关的): 我的结构是这些: st
老实说,我现在真的很困惑这个问题,并且真的不知道如何解决这个问题。我需要编写一个方法,其中给定一个字符链接列表(例如:{'a','A','d','X'})并返回仅包含大写字符的列表(返回:{'A','
我正在尝试获取可执行文件中的两个链表,并在交替位置将它们合并到一起。前任。 ListOne 1,2,3 和 ListTwo 4,5 新的 ListOne 应该是 1,4,2,5,3。 链表.h文件:
这个问题在这里已经有了答案: Is List a subclass of List? Why are Java generics not implicitly polymorphic? (19 个回答
在尝试了解如何将哈希表插入LinkedLists时,我遇到了麻烦。我失去了尝试过的不同事物的数量。我知道我可以使用ArrayList或其他东西,但是我想使它与LinkedLists一起工作,以便可以对
我一直在尝试编写一种方法,不仅可以从 LinkedList(allUsers) 中删除对象(User),还可以从所有用户拥有的单个 LinkedList 中删除。谁能向我解释为什么这是错误的?我已经包
我有一个列表结构和一个名为树的递归函数。在下面的代码中,它永远不会到达 current == null 语句,因此它将永远运行。 如果我无法使用null,解决方案是什么? private void t
这个问题在这里已经有了答案: How does one add a LinkedList to a LinkedList in C#? (3 个答案) 关闭 9 年前。 假设我有以下内容: Link
我正在尝试为 LinkedList 创建一个反向 ListIterator,并且打算将其实现为 linkedList.listIterator(linkedList. size()) 交换了 next
我是一名优秀的程序员,十分优秀!