포인터 및 주소 개념을 향상시키기 위해 연결된 목록을 만들려고합니다. 다음과 같은 방법으로 연결된 목록을 만들어야합니다.링크 된 목록은 인쇄시 첫 번째 노드 요소 만 표시합니다.
(1) 모든 노드를 터미널에서 동시에 읽습니다.
(2) 그런 다음 마지막으로 형성된 최종 연결 목록을 표시하십시오.
어떻게 그렇게하려고합니까? 먼저 연결된 목록의 크기 (입력 할 총 노드 수)를 읽습니다. 그럼 내가 루프를 do-while
루프에서 하나씩 모든 노드를 읽어보십시오. 모든 노드를 읽은 후 연결된 목록을 만들려고합니다. 노드가 첫 번째 노드 일 때 count
변수에 의해 대소 문자가 구별됩니다.이 노드는 다른 루프에있을 때 노드가 첫 번째 노드 일 때 count=0
이됩니다.
는 다음과 내가 얻을 출력은 같이
는enter the size of node
4
start entering the number of elements until your size
2
3
4
5
Printing linked list
2-> //It don't print the other nodes, Just first one
[email protected]:~/Desktop/pointer$
그렇게 할 내 전체 코드는 다음과 같습니다
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct node
{
int freq;
struct node * next;
};
typedef struct node node;
node * tree;
void main()
{
int size, data;
int count = 0; //this count flag is to check is it's first node or not inside the do-while loop.
tree = NULL;
printf("enter the size of node\n");
scanf("%d", & size);
printf("start entering the number of elements until your size\n");
node * temp3 = tree;
node * prev;
//Problem creating area is below
do
{
scanf("%d", & data);
if (count == 0)
{
node * temp;
temp = (node *) malloc(sizeof(node));
temp-> freq = data;
temp-> next = NULL;
prev = temp;
}
else if (count != 0)
{
node * temp;
temp = (node *) malloc(sizeof(node));
temp-> freq = data;
temp-> next = NULL;
prev-> next = temp;
}
size--;
++count;
}
while (size > 0);
printf("Printing linked list\n");
node * temp1;
temp1 = prev;
//there may be problem here
while (temp1-> next != NULL)
{
printf("%d-> ", temp1-> freq);
temp1 = temp1-> next;
}
printf("\n");
}
Couldanyone 나에게 에러를 지정하여 전체 연결 목록을 인쇄에서 저를 도와주세요 그게 해결책이야?
여기에 몇 개의 주석이 있습니다 : main은 무언가를 리턴하거나 무효로 설정해야합니다 (트리, temp3 및 저장소), 그리고 내 경우에는 prev 노드를 temp로 설정하고 temp를위한 메모리를 할당하고 기본적으로 가리키는 대상을 덮어 쓴다. –
디버거, debgugger, 디버거 ... –
@JamesHostick 내 편집에 void main()을 유지했습니다. 하지만 문제는 내가 모든 노드를 함께 읽고 있기 때문에 노드를 덮어 쓰는 것을 피하는 방법이다. – user252990