列表的C实现
我是一名C初学者,我尝试过用C实现列表,但是当我编译代码时,我收到了一个错误“请求在一些不在结构或联合中的内容中输入成员‘单词’”。我的代码如下所示:列表的C实现,c,pointers,data-structures,linked-list,doubly-linked-list,C,Pointers,Data Structures,Linked List,Doubly Linked List,我是一名C初学者,我尝试过用C实现列表,但是当我编译代码时,我收到了一个错误“请求在一些不在结构或联合中的内容中输入成员‘单词’”。我的代码如下所示: #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct data { char* word; struct data* next; } DataT; typedef struct node {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct data {
char* word;
struct data* next;
} DataT;
typedef struct node {
int wordLength;
struct node* prev, *next;
DataT* words;
} NodeT;
typedef struct list {
int length;
NodeT* first , *last;
} ListT;
void insertWord(NodeT* Node, char* word) {
if(!Node) || (!word) return;
if(Node.words.word == NULL)
Node.words.word = word;
DataT* current = Node.words.next;
while(current)
{
current = current.next;
}
(*current.next) = word;
}
#包括
#包括
#包括
typedef结构数据{
字符*字;
结构数据*下一步;
}DataT;
类型定义结构节点{
int字长;
结构节点*prev,*next;
DataT*字;
}节点;
类型定义结构列表{
整数长度;
节点*第一个,*最后一个;
}利斯特;
void insertWord(节点集*节点,字符*字){
如果(!Node)| |(!word)返回;
if(Node.words.word==NULL)
Node.words.word=单词;
DataT*current=Node.words.next;
while(当前)
{
当前=当前。下一步;
}
(*当前.下一个)=字;
}
函数实现中有几个错误
例如,这个if语句在语法上是错误的
if(!Node) || (!word) return;
我想你是说
if( !Node || !word ) return;
此声明
if(Node.words.word == NULL)
Node.words.word = word;
这也是错误的。应该是
if(Node->words->word == NULL)
Node->words->word = word;
还是无效代码
DataT* current = Node.words.next;
应该有
DataT* current = Node->words->next;
这种说法是错误的
current = current.next;
应该是这样的
current = current->next;
这种说法是错误的
(*current.next) = word;
此外,回路电流将等于零。因此,即使您将正确写入
current->next
而不是current,您也可能无法访问数据成员next。next
您的第一个错误与if条件一起出现
if(!Node) || (!word) return;
你应该把它写成
if( !Node || !word ) return;
而不是使用点运算符,你应该使用“->”
正如你提到的,你是一个初学者,我想向你展示一个简单的链表实现。我也是一个初学者,我用这种简单的方式实现了链表
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *tail; //Which points to the last node
struct node *current;//used for traversing the list
struct node *new_node; //Newly allocated block of memory(node)
struct node *head; //Head of list
void create(int number){
new_node=(struct node *)malloc(sizeof(struct node));
new_node->next=NULL;
new_node->data=number;
if (head==NULL){
head=new_node;
tail=new_node;
}
else
tail->next=new_node;
tail=new_node;
current=head;
}
int display(struct node *current){
current=head;
printf("\n");
while(current!=NULL){
printf("%d-->",current->data);
current=current->next;
}printf("NULL\n");
return 0;
}
int main(int argc, char const *argv[]){
int number,choice;
while(1){
printf("\n1.Creae Node\n");
printf("\n2.Display\n");
printf("\n3.Exit\n\n");
scanf("%d",&choice);
switch (choice){
case 1: printf("Enter number-->\t");
scanf("%d",&number);
create(number);
break;
case 2: display(current);
break;
case 3: exit(-1);
}
}
return 0;
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
结构节点*尾部//哪个指向最后一个节点
结构节点*当前//用于遍历列表
结构节点*新节点//新分配的内存块(节点)
结构节点*头部//名单之首
无效创建(整数){
新节点=(结构节点*)malloc(sizeof(结构节点));
新建节点->下一步=空;
新建_节点->数据=编号;
if(head==NULL){
head=新的_节点;
tail=新的_节点;
}
其他的
tail->next=新建_节点;
tail=新的_节点;
电流=水头;
}
int显示(结构节点*当前){
电流=水头;
printf(“\n”);
while(当前!=NULL){
printf(“%d-->”,当前->数据);
当前=当前->下一步;
}printf(“NULL\n”);
返回0;
}
int main(int argc,char const*argv[]{
整数,选择;
而(1){
printf(“\n1.Creae节点\n”);
printf(“\n2.Display\n”);
printf(“\n3.Exit\n\n”);
scanf(“%d”,选择(&C);
开关(选择){
案例1:printf(“输入编号-->\t”);
scanf(“%d”和编号);
创建(编号);
打破
情况2:显示(当前);
打破
案例3:出口(-1);
}
}
返回0;
}
我建议您在开始实现数据结构之前,先检查指针动态、内存分配和结构。
Node
是指针,所以您需要Node->words
如果(!Node)|(!word)返回代码>无法编译。请再试一次。我建议如果您是初学者,只使用第一个DataT
struct定义实现一个简单的链表。当这起作用时,你可以继续进行你正在尝试的更复杂的案例。这将使您至少能够获得正确的语法,而不必费劲地处理太多的概念。例如,另一个语法错误(请参见joop)是使用
语法从结构
指针访问字段,您应该在该指针处使用->
请参见Vlad提供的有用答案。