0% found this document useful (0 votes)
8 views7 pages

DAY 04_ akshata

Uploaded by

bavegi8097
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)
8 views7 pages

DAY 04_ akshata

Uploaded by

bavegi8097
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/ 7

DAY 04:

Akshata ashok kumbar


DSA Assignments

1. Implement doubly linked list operations .

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

// Node structure for Doubly Linked List


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

// Function to create a new node


struct DNode* createDNode(int data) {
struct DNode* newNode = (struct DNode*)malloc(sizeof(struct DNode));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

// Insert at the end of the doubly linked list


void insertAtEnd(struct DNode** head, int data) {
struct DNode* newNode = createDNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct DNode* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}

// Delete a node with given data


void deleteNode(struct DNode** head, int data) {
struct DNode* temp = *head;
while (temp != NULL) {
if (temp->data == data) {
if (temp->prev != NULL) {
temp->prev->next = temp->next;
} else {
*head = temp->next; // Updating head if needed
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
return;
}
temp = temp->next;
}
}

// Print the doubly linked list


void printDList(struct DNode* head) {
struct DNode* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

// Main function to demonstrate operations


int main() {
struct DNode* head = NULL;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);

printf("Doubly Linked List: ");


printDList(head);

deleteNode(&head, 20);
printf("After deleting 20: ");
printDList(head);

return 0;
}
2. Implement circular linked list operations
#include <stdio.h>
#include <stdlib.h>

// Node structure for Circular Linked List


struct CNode {
int data;
struct CNode* next;
};

// Function to create a new node


struct CNode* createCNode(int data) {
struct CNode* newNode = (struct CNode*)malloc(sizeof(struct CNode));
newNode->data = data;
newNode->next = newNode; // Pointing to itself
return newNode;
}

// Insert at the end of the circular linked list


void insertAtEnd(struct CNode** head, int data) {
struct CNode* newNode = createCNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct CNode* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head; // Pointing new node to head
}
}

// Delete a node with given data


void deleteNode(struct CNode** head, int data) {
if (*head == NULL) return;

struct CNode *current = *head, *prev = NULL;

do {
if (current->data == data) {
if (prev != NULL) {
prev->next = current->next;
} else {
// Deleting head
struct CNode* tail = *head;
while (tail->next != *head) {
tail = tail->next;
}
tail->next = current->next;
*head = current->next;
}
free(current);
return;
}
prev = current;
current = current->next;
} while (current != *head);
}

// Print the circular linked list


void printCList(struct CNode* head) {
if (head == NULL) return;

struct CNode* temp = head;


do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}

// Main function to demonstrate operations


int main() {
struct CNode* head = NULL;
int choice, data;

while (1) {
printf("1. Insert at End\n2. Delete Node\n3. Print List\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
break;
case 3:
printf("Circular Linked List: ");
printCList(head);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}

return 0;
}

You might also like