#include <stdio.h>
#include <conio.h>
struct node {
int data;
struct node* next;
};
int main() {
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = (struct node*)malloc(sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node*)malloc(sizeof(struct node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
struct node* temp;
temp = head;
while (temp != NULL) { // for printing the linked list
printf("%d ",temp->data);
temp = temp->next;
}
struct node* new1;
struct node* temp1;
temp1 = head;
while (temp1 != NULL) { // for traversing to the end
temp1 = temp1->next;
}
new1 = (struct node*)malloc(sizeof(struct node));
new1->data = 5;
new1->next = NULL;
temp1->next = new1;
while (temp1 != NULL) { // again for printing the list
printf("%d ",temp1->data);
temp1 = temp1->next;
}
return 0;
}
Я пытаюсь вставить узел в конец связанного списка, но он не работает. Я создал связанный список успешно, но я не могу вставить узел в конец. Это то, чему меня учил мой учитель, но он не работает.Вставка узла в конец связанного списка
Что именно «не работает»? – Codor
После цикла 'while (temp1! = NULL)' 'temp1' будет' NULL', чтобы вы не могли его разыменовать. – interjay
Не бросайте 'malloc' в C. – crashmstr