list *add_item(list *l, int value){
list *temp;
temp = malloc(sizeof(list));
temp->data = value;
temp->next = l;
l = temp;
return l;}
이것은 목록의 앞부분에 삽입하는 코드입니다. 내가 어떻게 바꿀 수 있을까 ?? 끝 부분에 삽입하고 싶습니다. 감사합니다.끝에 노드 추가 (C의 구조 노드)
list *add_item(list *l, int value){
list *temp;
temp = malloc(sizeof(list));
temp->data = value;
temp->next = l;
l = temp;
return l;}
이것은 목록의 앞부분에 삽입하는 코드입니다. 내가 어떻게 바꿀 수 있을까 ?? 끝 부분에 삽입하고 싶습니다. 감사합니다.끝에 노드 추가 (C의 구조 노드)
이 될 수있다 : * (L)가 목록의 머리
list *add_item(list *l, int value){
list *temp;
temp = malloc(sizeof(list));
temp->data = value;
temp->next = 0;
list* tmp = l;
while(tmp->next) tmp = tmp->next;
tmp->next = temp;
return l;}
temp->next == NULL
까지 반복하여 노드를 추가해야합니다. 반복하지 않으려면 last
참조를 마지막 요소에 보관할 수 있습니다. 또한 l = temp
줄은 아무 것도하지 않습니다.
'반환 리터를 시도'않습니다 어떤 것. 따라서 두 번째 노드 대신 첫 번째 노드를 반환합니다. – mch
그냥'return temp; '라고 쓸 수 있습니다. –
트래버스 목록을 통해
list *add_item(list *l, int value){
list *temp;
temp = malloc(sizeof(list));
temp->data = value;
temp->next = 0;
l->next = temp;
return l;}
경우, 난 당신이 목록의 마지막에 null
를 가리키는 list->next
을 가정합니다. 해당 위치를 찾고 새로 만든 요소를 참조하십시오. `라인`L = 임시;
당신이 목록의 첫 번째 요소를 가리키는 머리가 가정
List *temp = head;
List *newElement = malloc(sizeof(List));
List *currNode;
//some assignment
while(temp!= null)
{
currNode = temp;
temp = temp->next;
}
currNode->next = newElement;
코드가 segfault를 일으킬 수 있습니다. while 루프를 벗어나면 'temp == null'이 유지됩니다. – HuStmpHrrr
편집 할 때 세그먼트 화 오류가 발생하지 않는다고 생각합니다. – saikumarm
이 때문에
list *add_item(list *l, int value) {
list *head, *temp;
head = l;
temp = malloc(sizeof(list));
temp->data = value;
temp->next = NULL;
while(l->next != NULL) {
l = l->next;
}
l->next = temp;
return head;
}
* l이리스트의 선두입니다. 나는 머리라고 불리는리스트조차 만들었습니다. list * head = NULL; list * create_list (int 값) { \t head = malloc (sizeof (list)); \t head-> data = value; \t head-> next = NULL; \t return head; } –
두 번째 부분이 귀하의 케이스에서 작동해야합니다. – Adem