Untitled
unknown
plain_text
3 years ago
3.8 kB
10
Indexable
/*
Coded by: David Roman
Date: 06.04.2023
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Define the Node struct
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Function to add a node at the end of the list
void addNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
// Function to print the list
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
// Function to remove duplicates in the doubly linked list by sorting and removing adjacent elements with the same value
void removeDuplicates(struct Node** head) {
// Sort the list
struct Node* current = *head;
while (current->next != NULL) {
struct Node* temp = current->next;
while (temp != NULL) {
if (current->data > temp->data) {
int tempData = current->data;
current->data = temp->data;
temp->data = tempData;
}
temp = temp->next;
}
current = current->next;
}
// Remove adjacent elements with the same value
current = *head;
while (current != NULL && current->next != NULL) {
if (current->data == current->next->data) {
struct Node* temp = current->next;
current->next = current->next->next;
if (current->next != NULL) {
current->next->prev = current;
}
free(temp);
} else {
current = current->next;
}
}
}
// Function to remove duplicates in the doubly linked list by comparing each element with the remaining elements
void removeDuplicates2(struct Node** head) {
struct Node* current = *head;
while (current != NULL) {
struct Node* temp = current->next;
while (temp != NULL) {
if (current->data == temp->data) {
struct Node* toDelete = temp;
temp = temp->next;
if (toDelete->prev != NULL) {
toDelete->prev->next = toDelete->next;
} else {
*head = toDelete->next;
}
if (toDelete->next != NULL) {
toDelete->next->prev = toDelete->prev;
}
free(toDelete);
} else {
temp = temp->next;
}
}
current = current->next;
}
}
// Main function
int main() {
struct Node* head = NULL;
// Generate random integers and add them to the list
for (int i = 0; i < 200; i++) {
addNode(&head, rand() % 50);
}
printf("Original List:\n");
printList(head);
// Remove duplicates using the first method (sorting and removing adjacent elements)
removeDuplicates(&head);
printf("List after removing duplicates using the first method:\n");
printList(head);
// Remove duplicates using the second method (comparing each element with the remaining elements)
removeDuplicates2(&head);
printf("List after removing duplicates using the second method:\n");
printList(head);
return 0;
}
Editor is loading...