5a
#include
#include
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return NULL;
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void traverseForward(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("List Empty.\n");
return;
}
printf("Forward: NULL <-> ");
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void insertBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
newNode->prev = last;
}
void insertAfter(struct Node* head, int key, int data) {
struct Node* current = head;
while (current != NULL && current->data != key) {
current = current->next;
}
if (current == NULL) return;
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
newNode->next = current->next;
newNode->prev = current;
if (current->next != NULL) {
current->next->prev = newNode;
}
current->next = newNode;
}
void deleteBeginning(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
}
void deleteEnd(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
if (temp->next == NULL) {
free(*head);
*head = NULL;
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL;
free(temp);
}
void deleteByKey(struct Node** head, int key) {
struct Node* current = *head;
while (current != NULL && current->data != key) {
current = current->next;
}
if (current == NULL) return;
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
*head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
}
int main() {
struct Node* head = NULL;
insertEnd(&head, 10);
insertBeginning(&head, 5);
insertEnd(&head, 30);
traverseForward(head);
insertAfter(head, 5, 7);
traverseForward(head);
deleteBeginning(&head);
traverseForward(head);
deleteEnd(&head);
traverseForward(head);
insertEnd(&head, 40);
insertEnd(&head, 50);
traverseForward(head);
deleteByKey(&head, 40);
traverseForward(head);
return 0;
}
5b
#include
#include
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) return NULL;
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (newNode == NULL) return;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
newNode->prev = last;
}
void traverseForward(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("List Empty.\n");
return;
}
printf("Forward: NULL <-> ");
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void traverseReverse(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("List Empty.\n");
return;
}
while (current->next != NULL) {
current = current->next;
}
printf("Reverse: NULL <-> ");
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->prev;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertEnd(&head, 10);
insertEnd(&head, 20);
insertEnd(&head, 30);
insertEnd(&head, 40);
traverseForward(head);
traverseReverse(head);
return 0;
}
Sunday, 5 October 2025
Linked list 5
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment