질문이 해결되었습니다.
링크드 목록 코드에 어떤 문제가 있습니까?
내가 뭘 잘못하고 있는지 설명하는 데 도움이되기를 바랍니다.
미리 감사드립니다.
코드 :
데이터를 입력하십시오 N1 :
데이터를 입력하십시오 :이
요소의 수를 입력하십시오 : 여기
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char *data;
struct node * previous;
struct node * next;
} node, *nodePTR;
/* Insert into list */
void insert(char * buf, nodePTR tail) {
nodePTR myNode;
myNode = (node *)malloc(sizeof(node));
myNode->data = malloc(sizeof(char) * 10);
strcpy(myNode->data,buf);
myNode->next = NULL;
myNode->previous = tail;
tail->next = myNode;
//tail = tail->next;
}
void printlist(nodePTR head, int numElements) {
nodePTR tmpNode;
tmpNode = head;
printf("\n\n");
while(tmpNode!=NULL) {
printf("Node data: %s\n", tmpNode->data);
tmpNode = tmpNode->next;
}
}
int main(void) {
/* Variables */
int numElements;
int i;
char buf[10];
nodePTR head, tail;
tail = (node *)malloc(sizeof(node));
head = (node *)malloc(sizeof(node));
tail->data = "EMPTY\0";
tail->next = NULL;
tail->previous = NULL;
head = tail;
printf("Please enter the number of elements:\n");
scanf("%d", &numElements);
/* Build the list */
for(i = 0; i < numElements; i++) {
printf("Please enter the data:");
scanf("%s", buf);
insert(buf, tail);
tail = tail->next;
}
printlist(head, numElements);
return 0;
}
데이터를 입력하십시오 : n3
노드 데이터 : EMPTY
노드 데이터 :
* 관련된 * 코드를 여기에 (링크가 아닌) 게시하고 어떤 디버깅 단계를 수행했는지, 어디서 멈췄는지 설명하십시오. –
여기에 코드를 직접 게시하는 것이 좋습니다. 디버거를 통해 프로그램을 실행 해 보시기 바랍니다. 리눅스 시스템을 사용하고 있다면 GDB에 대해 익숙해 지길 권합니다 –