使用C语言的链表
我试图创建一个链表,在每个节点上都有一个指向不同类型的单独链表的指针。这就是问题所在,第一个列表(X)创建得很好,但我创建的是一个链接列表(Y),X的所有节点都指向该列表,而不是X的每个节点都指向一个唯一的Y。我希望这是有意义的。以下是我的代码:使用C语言的链表,c,list,linked-list,C,List,Linked List,我试图创建一个链表,在每个节点上都有一个指向不同类型的单独链表的指针。这就是问题所在,第一个列表(X)创建得很好,但我创建的是一个链接列表(Y),X的所有节点都指向该列表,而不是X的每个节点都指向一个唯一的Y。我希望这是有意义的。以下是我的代码: struct PathNodeStruct { char CurrentPathMovement; struct PathNodeStruct * prev; struct PathNodeStruct * next; }; typedef
struct PathNodeStruct {
char CurrentPathMovement;
struct PathNodeStruct * prev;
struct PathNodeStruct * next;
};
typedef struct PathNodeStruct PathNode;
typedef struct PathNodeStruct * PathNodePtr;
struct PathNodeStruct * head = NULL;
struct RoomNodeStruct {
int roomNumber;
struct PathNode * pathStart;
struct RoomNodeStruct * next;
};
typedef struct RoomNodeStruct RoomNode;
typedef struct RoomNodeStruct * RoomNodePtr;
struct RoomNodeStruct * roomHead = NULL;
void insertPathNode(char move){
if(head == NULL){ //case if first node in list
head = (PathNodePtr)malloc(sizeof(PathNode));
if(head != NULL){
head->CurrentPathMovement = move; //sets data in node
head->prev = NULL; //sets links up
head->next = NULL;
} else{
printf("ERROR");
}
} else{
PathNodePtr cursorPtr = head;
while(cursorPtr->next != NULL){ //cursors to end of list
cursorPtr = cursorPtr->next;
}
PathNodePtr newPathNodePtr = (PathNodePtr)malloc(sizeof(PathNode)); //allocates for node to be added
if(newPathNodePtr != NULL){
cursorPtr->next = newPathNodePtr;
newPathNodePtr->prev = cursorPtr; //sets links up
newPathNodePtr->next = NULL;
newPathNodePtr->CurrentPathMovement = move; //sets data in node
} else{
printf("ERROR");
}
}
void insertRoomNode(int value){
if(roomHead == NULL){ //case if first node in list
roomHead = (RoomNodePtr)malloc(sizeof(RoomNode));
if(roomHead != NULL){
roomHead->roomNumber = value; //sets data in node
roomHead->next = NULL;
roomHead->pathStart = head;
} else{
printf("ERROR ALLOCATING IN INSERT ROOM NODE.");
}
} else{
RoomNodePtr cursorPtr = roomHead;
while(cursorPtr->next != NULL){ //cursors to end of list
cursorPtr = cursorPtr->next;
}
RoomNodePtr newRoomNodePtr = (RoomNodePtr)malloc(sizeof(RoomNode)); //allocates for node to be added
if(newRoomNodePtr != NULL){
cursorPtr->next = newRoomNodePtr;
newRoomNodePtr->next = NULL;
newRoomNodePtr->roomNumber = value; //sets data in node
newRoomNodePtr->pathStart = head;
} else{
printf("ERROR ALLOCATING IN INSERT ROOM NODE.");
}
}
这是用于MUD服务器的吗?
roomHead->pathStart=head代码>我认为这是没有必要的。因为head
是全局变量。不需要作为单独的成员保留。对于MUD服务器不需要