【发布时间】:2021-01-31 17:17:15
【问题描述】:
我正在尝试在 C 中实现 List ADT,但我在互联网上找不到太多帮助,因为似乎非常示例是在 C++ 中,而我对此一无所知。我可以完全理解数据结构(至少,我想我理解了),但我无法将其作为 ADT、分离文件等。
尝试实现附加功能时,在遍历列表的循环中,我收到如下错误:
member reference base type 'MOVE' (aka 'struct s_move *')is not a structure or union
我知道问题出在指针上,并且通过简化我的数据,因为这对于我正在解决的问题来说显然是多余的,我想让它以这种方式工作以用于学习目的。
移动.h
// This is the node of the list
#ifndef MOVE_H
#define MOVE_H
typedef struct s_move *MOVE;
/* Initializes a new move */
MOVE move_init(int i, int j);
#endif
move.c
#include <stdlib.h>
#include <stdio.h>
#include "move.h"
struct s_move {
int i;
int j;
MOVE *next;
};
MOVE move_init(int i, int j) {
MOVE m;
m->i = i;
m->j = j;
m->next = NULL;
return m;
}
moves.h
#ifndef MOVES_H
#define MOVES_H
#include "move.h"
typedef struct s_moves *MOVES;
/* Initializes the list of moves */
MOVES moves_init();
/* Appends a new move at the end of the list */
void moves_append(MOVES moves, MOVE move);
#endif
moves.c
#include <stdlib.h>
#include "moves.h"
#include "move.h"
struct s_moves {
MOVE *head;
};
MOVES moves_init() {
MOVES m;
m->head = (MOVE *)malloc(sizeof(MOVE));
m->head = NULL;
return m;
}
void moves_append(MOVES moves, MOVE move) {
MOVE *ptr;
//***********************************
//HERE I GET THE ERROR ON ptr->next
//***********************************
for(ptr = moves->head; ptr->next != NULL; ptr = ptr->next) {
//do stuff
}
}
执行此操作的正确方法是什么?对不起,如果我重复自己的话,我想在不简化结构的情况下使用 ADT。谢谢!
【问题讨论】:
标签: c linked-list abstract-data-type