0% found this document useful (0 votes)
16 views9 pages

Assignment 7

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

Assignment 7

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

ASSIGNMENT 7

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
struct Node* prev;
};

struct Node* head = NULL;

void createNode(int value) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;

if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
void insertAtBeginning(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
newNode->prev = NULL;

if (head != NULL) {
head->prev = newNode;
}
head = newNode;
}

void insertAtEnd(int value) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

if (head == NULL) {
newNode->prev = NULL;
head = newNode;
return;
}

struct Node* temp = head;


while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}

void deleteNode(int value) {


struct Node* temp = head;

while (temp != NULL && temp->data != value) {


temp = temp->next;
}

if (temp == NULL) 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);
}

void traverseForward() {
struct Node* temp = head;
printf("Traversal in forward direction: ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

void traverseBackward() {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
printf("Traversal in backward direction: ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->prev;
}
printf("NULL\n");
}

int main() {
int choice, value;

do {
printf("\nMenu:\n");
printf("1. Create Node\n");
printf("2. Insert at Beginning\n");
printf("3. Insert at End\n");
printf("4. Delete Node\n");
printf("5. Traverse Forward\n");
printf("6. Traverse Backward\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to create the node: ");
scanf("%d", &value);
createNode(value);
break;
case 2:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 3:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 4:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(value);
break;
case 5:
traverseForward();
break;
case 6:
traverseBackward();
break;
case 7:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 7);

return 0;
}
OUTPUT:

You might also like