doubly_linked_list_operations
doubly_linked_list_operations
h"
// Add at beginning
D_STUDENT * Add_Begin(D_STUDENT *head) {
D_STUDENT *nu = malloc(sizeof(D_STUDENT));
if (!nu) {
printf("Node not created\n");
return head;
}
printf("Enter ID: ");
scanf("%d", &nu->id);
printf("Enter Name: ");
scanf("%s", nu->name);
nu->prev = NULL;
nu->next = head;
if (head) head->prev = nu;
head = nu;
return head;
}
// Add at end
D_STUDENT * Add_End(D_STUDENT *head) {
D_STUDENT *nu = malloc(sizeof(D_STUDENT)), *temp;
if (!nu) {
printf("Node not created\n");
return head;
}
printf("Enter ID: ");
scanf("%d", &nu->id);
printf("Enter Name: ");
scanf("%s", nu->name);
nu->next = NULL;
if (!head) {
nu->prev = NULL;
return nu;
}
temp = head;
while (temp->next) temp = temp->next;
temp->next = nu;
nu->prev = temp;
return head;
}
// Delete by ID
D_STUDENT * Del_Pos(D_STUDENT *head) {
int id;
printf("Enter ID to delete: ");
scanf("%d", &id);
D_STUDENT *temp = head;
while (temp && temp->id != id) temp = temp->next;
if (!temp) {
printf("ID not found\n");
return head;
}
if (temp->prev) temp->prev->next = temp->next;
else head = temp->next;
if (temp->next) temp->next->prev = temp->prev;
free(temp);
return head;
}
// Reverse list
D_STUDENT * Rev(D_STUDENT *head) {
D_STUDENT *temp = NULL, *current = head;
while (current) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp) head = temp->prev;
return head;
}