#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; return newNode; } void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d", temp->data); if (temp->next != NULL) printf(" <-> "); temp = temp->next; } printf(" -> NULL\n"); } int main() { Node* head = createNode(10); Node* second = createNode(20); Node* third = createNode(30); head->next = second; second->prev = head; second->next = third; third->prev = second; printList(head); return 0; }
The code creates three nodes with values 10, 20, and 30. It links them in order with proper prev and next pointers, so the list prints as 10 <-> 20 <-> 30 -> NULL.
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; return newNode; } void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d", temp->data); if (temp->next != NULL) printf(" <-> "); temp = temp->next; } printf(" -> NULL\n"); } int main() { Node* head = createNode(10); Node* second = createNode(20); head->next = second; second->prev = head; Node* newHead = createNode(5); newHead->next = head; head->prev = newHead; head = newHead; printList(head); return 0; }
The new node with value 5 is added before the current head (10). The prev pointer of old head points to new node, and new node's next points to old head. So the list is 5 <-> 10 <-> 20 -> NULL.
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; Node* createNode(int data) { Node* newNode = malloc(sizeof(Node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; return newNode; } int main() { Node* node = createNode(10); printf("%d\n", node->data); free(node); return 0; }
The code correctly allocates memory and initializes the node. Casting malloc is optional in C and stdlib.h is included, so it prints 10 without errors.
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; return newNode; } void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d", temp->data); if (temp->next != NULL) printf(" <-> "); temp = temp->next; } printf(" -> NULL\n"); } int main() { Node* head = createNode(10); Node* second = createNode(20); Node* third = createNode(30); head->next = second; second->prev = head; second->next = third; third->prev = second; // Remove second node head->next = third; third->prev = head; free(second); printList(head); return 0; }
The node with value 20 is removed by linking 10 directly to 30 and updating pointers accordingly. So the list prints as 10 <-> 30 -> NULL.
In C, nodes must be dynamically allocated with malloc to persist beyond function scope, and each allocated node must be freed manually to avoid memory leaks. Local variables do not persist after function returns, and freeing one node does not free others linked to it.
