Add to the beginning of a doubly linked list

  Рет қаралды 3,446

CodeVault

CodeVault

4 жыл бұрын

Check out our Discord server: / discord

Пікірлер: 6
@ahmedrodriguez7358
@ahmedrodriguez7358 3 жыл бұрын
You are the best teacher ever!!! Big thank u!!!
@sencer07
@sencer07 Жыл бұрын
let me tell you, the way you explained it, like seriouly, after watching just one video on LInkedList and DoublyLinkedList i have wriiten the whole insertion and deletion on my own, thanks to you that i have it now to tackle linkedList in any way possible.
@tarekhamdoudi3144
@tarekhamdoudi3144 2 ай бұрын
Hello sir. First I'd like to offer my many thanks to you for laying out the concepts in a simplified approach. I just have one question though. What if we just omit the whole init() function, and write this insted: int main() { struct node* tail = NULL; insert_beginning(&tail, 3); struct node* head = tail; insert_beginning(&tail, 2); insert_beginning(&tail, 1); } This way, head is updated as soon as the first node is inserted, and stays like that for as long as new nodes are being added, right? Have a good day sir.
@CodeVault
@CodeVault 2 ай бұрын
Yes, this should also work if insert_beginning is implemented properly and sets the root node when *tail is NULL
@KarmenZD
@KarmenZD Жыл бұрын
There is just a few mistakes in code on website. Like insert_beginning function takes three arguments but in main are two written. Init function malloc sizeof(int)but this i think is not problem because Node is anyway int. And in main function also for(Node* curr = tail, instead of head). Here would be right version from video: typedef struct Node { int x; struct Node* next; struct Node* prev; } Node; void deallocate(Node** tail, Node** head) { if (*tail == NULL) { return; } Node* curr = *tail; while (curr->next != NULL) { curr = curr->next; free(curr->prev); } free(curr); *tail = NULL; *head = NULL; } void insert_beginning(Node** tail, int value) { Node* new_node = malloc(sizeof(Node)); if (new_node == NULL) { exit(1); return; } new_node->x = value; new_node->prev = NULL; new_node->next = *tail; (*tail)->prev = new_node; *tail = new_node; } void init(Node** tail, Node** head, int value) { Node* new_node = malloc(sizeof(Node)); if (new_node == NULL) { exit(2); return; } new_node->x = value; new_node->prev = NULL; new_node->next = NULL; *tail = new_node; *head = new_node; } int main(int argc, char* argv[]) { Node* tail = NULL; Node* head = NULL; init(&tail, &head, 7); insert_beginning(&tail, 3); insert_beginning(&tail, 1); for (Node* curr = head; curr != NULL; curr = curr->prev) { printf("%d ", curr->x); } deallocate(&tail, &head); return 0; } Btw. thank you very much for your tutorials. The way you explain it makes learning easier, more interesting and therefore more efficient. :))
@CodeVault
@CodeVault Жыл бұрын
Should be fixed now. Thanks for pointing out the mistakes!
Add to the end of a doubly linked list
7:47
CodeVault
Рет қаралды 2 М.
What are variadic functions (va_list) in C?
13:49
CodeVault
Рет қаралды 20 М.
Happy 4th of July 😂
00:12
Pink Shirt Girl
Рет қаралды 60 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
Short introduction to doubly linked lists
9:59
CodeVault
Рет қаралды 5 М.
Global functions in multi-file projects in C
10:39
CodeVault
Рет қаралды 8 М.
Doubly Linked List - Implementation in C/C++
15:21
mycodeschool
Рет қаралды 582 М.
Doubly Linked List (Insertion between the Nodes) - Part 1
6:19
Neso Academy
Рет қаралды 112 М.
What does fork() actually return?
7:13
CodeVault
Рет қаралды 4,1 М.
Declaration vs. Definition of a variable in C
10:37
CodeVault
Рет қаралды 7 М.
Doubly Linked List (Insertion at the Beginning)
3:43
Neso Academy
Рет қаралды 167 М.
Sharing functions between files in C
9:00
CodeVault
Рет қаралды 28 М.
Happy 4th of July 😂
00:12
Pink Shirt Girl
Рет қаралды 60 МЛН