DLL
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* head = NULL;
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void insertNode(int value) {
struct Node* newNode = createNode(value);
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
printf("Inserted %d\n", value);
}
void deleteNode(int value) {
struct Node* temp = head;
while (temp != NULL && temp->data != value)
temp = temp->next;
if (temp == NULL) {
printf("Value not found!\n");
return;
}
if (temp->prev != NULL)
temp->prev->next = temp->next;
else
head = temp->next;
if (temp->next != NULL)
temp->next->prev = temp->prev;
free(temp);
printf("Deleted %d\n", value);
}
void traverseList() {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
return;
}
printf("Doubly Linked List: \n");
while (temp != NULL) {
printf("[ ");
if (temp->prev != NULL)
printf("%d <- ", temp->prev->data);
else
printf("NULL <- ");
printf("%d", temp->data);
if (temp->next != NULL)
printf(" -> %d ]", temp->next->data);
else
printf(" -> NULL ]");
printf(" ");
temp = temp->next;
}
printf("\n");
}
int main() {
int choice, value;
while (1) {
printf("\n--- Doubly Linked List Menu ---\n");
printf("1. Insert Node\n");
printf("2. Delete Node\n");
printf("3. Traverse List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insertNode(value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(value);
break;
case 3:
traverseList();
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
--------------
tree
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
struct Node* insertNode() {
int data;
scanf("%d", &data);
if (data == -1)
return NULL;
struct Node* root = createNode(data);
root->left = insertNode();
root->right = insertNode();
return root;
}
void inorder(struct Node* root) {
if (root == NULL)
return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
void preorder(struct Node* root) {
if (root == NULL)
return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct Node* root) {
if (root == NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
int main() {
struct Node* root = insertNode();
inorder(root);
printf("\n");
preorder(root);
printf("\n");
postorder(root);
printf("\n");
return 0;
}