C 中的列表实现
List implementation in C
我是 C 初学者,我尝试用 C 实现列表,但是当我编译代码时收到错误 "request for member 'words' in something that not in a structure or union"。我的代码如下所示:
#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;
}
函数实现有几个错误
例如这个 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;
而且循环后电流会等于NULL。因此,即使您正确编写 current->next
而不是 current.next
.
,您也可能无法访问下一个数据成员
您的第一个错误来自 if 条件
if(!Node) || (!word) return;
你应该写成
if( !Node || !word ) return;
而不是使用点运算符你应该使用“->”
正如您提到的,您是初学者,我想向您展示一个简单的链表 implementation.I 我也是初学者,我以这种方式实现了链表,这非常简单。
#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;
}
我建议您在开始实现数据结构之前了解动态指针、内存分配和结构。
我是 C 初学者,我尝试用 C 实现列表,但是当我编译代码时收到错误 "request for member 'words' in something that not in a structure or union"。我的代码如下所示:
#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;
}
函数实现有几个错误
例如这个 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;
而且循环后电流会等于NULL。因此,即使您正确编写 current->next
而不是 current.next
.
您的第一个错误来自 if 条件
if(!Node) || (!word) return;
你应该写成
if( !Node || !word ) return;
而不是使用点运算符你应该使用“->”
正如您提到的,您是初学者,我想向您展示一个简单的链表 implementation.I 我也是初学者,我以这种方式实现了链表,这非常简单。
#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;
}
我建议您在开始实现数据结构之前了解动态指针、内存分配和结构。