2) Berilgan tugundan keyin tugun qo'shish: (7 bosqichli jarayon) Bizga tugunga ko'rsatgich prev_node sifatida beriladi va yangi tugun berilgan tugundan so'ng kiritiladi.
3) tugunni oxiriga qo'shish: (7 bosqichli jarayon) Yangi tugun har doim berilgan bog'langan ro'yxatning oxirgi tugunidan keyin qo'shiladi. Masalan, agar berilgan Ikkitalik bog’langan ro’yxatning 510152025 bo'lsa va biz oxirida 30-bandni qo'shsak, u holda Ikkitalik bog’langan ro’yxat 51015202530 bo'ladi. Bog'langan ro'yxat odatda uning boshlig'i tomonidan taqdim etilganligi sababli biz ro'yxatni oxirigacha bosib o'tib, keyingisini o'zgartirishimiz kerak. oxirgi tugunning yangi tugunga.
void append(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); struct Node* last = *head_ref; /* used in step 5*/ new_node->data = new_data; new_node->next = NULL; if (*head_ref == NULL) { new_node->prev = NULL; *head_ref = new_node; return; } while (last->next != NULL) last = last->next; last->next = new_node; new_node->prev = last; return; }
Yuqoridagi barcha jarayonlar:
#include #include struct Node { int data; struct Node* next;