0% found this document useful (0 votes)
1 views3 pages

doubly_linked_list_operations

The document contains C code for managing a doubly linked list of students, including functions to add, delete, and reverse the list. Functions include adding nodes at the beginning, end, or in sorted order, as well as deleting nodes from the beginning, end, or by ID. Memory management is handled with malloc and free, and user input is taken for student ID and name.

Uploaded by

20q91a1917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

doubly_linked_list_operations

The document contains C code for managing a doubly linked list of students, including functions to add, delete, and reverse the list. Functions include adding nodes at the beginning, end, or in sorted order, as well as deleting nodes from the beginning, end, or by ID. Memory management is handled with malloc and free, and user input is taken for student ID and name.

Uploaded by

20q91a1917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include "myheader.

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;
}

// Add by sorted position


D_STUDENT * Add_Pos(D_STUDENT *head) {
D_STUDENT *nu = malloc(sizeof(D_STUDENT)), *temp = head;
if (!nu) {
printf("Node not created\n");
return head;
}
printf("Enter ID: ");
scanf("%d", &nu->id);
printf("Enter Name: ");
scanf("%s", nu->name);
if (!head || nu->id < head->id) {
nu->prev = NULL;
nu->next = head;
if (head) head->prev = nu;
return nu;
}
while (temp->next && temp->next->id < nu->id) temp = temp->next;
nu->next = temp->next;
nu->prev = temp;
if (temp->next) temp->next->prev = nu;
temp->next = nu;
return head;
}

// Delete from beginning


D_STUDENT * Del_Begin(D_STUDENT *head) {
if (!head) {
printf("List is empty\n");
return NULL;
}
D_STUDENT *temp = head;
head = head->next;
if (head) head->prev = NULL;
free(temp);
return head;
}

// Delete from end


D_STUDENT * Del_End(D_STUDENT *head) {
if (!head) {
printf("List is empty\n");
return NULL;
}
D_STUDENT *temp = head;
if (!temp->next) {
free(temp);
return NULL;
}
while (temp->next) temp = temp->next;
temp->prev->next = NULL;
free(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;
}

You might also like