0% found this document useful (0 votes)
34 views6 pages

Implement Singly, Doubly and Circularly Connected Linked Lists Illustrating

Uploaded by

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

Implement Singly, Doubly and Circularly Connected Linked Lists Illustrating

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

// Node structure for singly linked list


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

// Node structure for doubly linked list


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

// Node structure for circularly linked list


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

// Function to create a new node for singly linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to create a new node for doubly linked list


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

// Function to create a new node for circular linked list


struct CircularNode* createCircularNode(int data) {
struct CircularNode* newNode = (struct CircularNode*)malloc(sizeof(struct
CircularNode));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to add a node at the beginning of the singly linked list


struct Node* addToBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);
newNode->next = head;
return newNode;
}

// Function to add a node at the end of the singly linked list


struct Node* addToEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}

struct Node* current = head;


while (current->next != NULL) {
current = current->next;
}

current->next = newNode;
return head;
}

// Function to delete a node with the given data from the singly linked list
struct Node* deleteNode(struct Node* head, int data) {
if (head == NULL) {
return NULL;
}

if (head->data == data) {
struct Node* temp = head;
head = head->next;
free(temp);
return head;
}

struct Node* current = head;


while (current->next != NULL && current->next->data != data) {
current = current->next;
}

if (current->next == NULL) {
// Node with the given data not found
return head;
}

struct Node* temp = current->next;


current->next = current->next->next;
free(temp);
return head;
}

// Function to print elements of the singly linked list


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

// Function to add a node at the beginning of the doubly linked list


struct DoublyNode* addToBeginningDoubly(struct DoublyNode* head, int data) {
struct DoublyNode* newNode = createDoublyNode(data);
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
}
return newNode;
}

// Function to add a node at the end of the doubly linked list


struct DoublyNode* addToEndDoubly(struct DoublyNode* head, int data) {
struct DoublyNode* newNode = createDoublyNode(data);
if (head == NULL) {
return newNode;
}

struct DoublyNode* current = head;


while (current->next != NULL) {
current = current->next;
}

current->next = newNode;
newNode->prev = current;
return head;
}

// Function to delete a node with the given data from the doubly linked list
struct DoublyNode* deleteNodeDoubly(struct DoublyNode* head, int data) {
if (head == NULL) {
return NULL;
}

if (head->data == data) {
struct DoublyNode* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
return head;
}

struct DoublyNode* current = head;


while (current != NULL && current->data != data) {
current = current->next;
}

if (current == NULL) {
// Node with the given data not found
return head;
}

if (current->prev != NULL) {
current->prev->next = current->next;
}

if (current->next != NULL) {
current->next->prev = current->prev;
}

free(current);
return head;
}

// Function to print elements of the doubly linked list


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

// Function to add a node at the beginning of the circular linked list


struct CircularNode* addToBeginningCircular(struct CircularNode* head, int data) {
struct CircularNode* newNode = createCircularNode(data);
if (head == NULL) {
newNode->next = newNode; // Circular link to itself
} else {
newNode->next = head;
struct CircularNode* current = head;
while (current->next != head) {
current = current->next;
}
current->next = newNode;
}
return newNode;
}

// Function to add a node at the end of the circular linked list


struct CircularNode* addToEndCircular(struct CircularNode* head, int data) {
struct CircularNode* newNode = createCircularNode(data);
if (head == NULL) {
newNode->next = newNode; // Circular link to itself
return newNode;
}

struct CircularNode* current = head;


while (current->next != head) {
current = current->next;
}

current->next = newNode;
newNode->next = head;
return head;
}

// Function to delete a node with the given data from the circular linked list
struct CircularNode* deleteNodeCircular(struct CircularNode* head, int data) {
if (head == NULL) {
return NULL;
}

struct CircularNode* current = head;


struct CircularNode* prev = NULL;

// Find the node to be deleted and its previous node


do {
if (current->data == data) {
break;
}
prev = current;
current = current->next;
} while (current != head);
if (current == head) {
// Node with the given data not found
return head;
}

if (prev != NULL) {
prev->next = current->next;
}

if (current == head) {
head = head->next;
}

free(current);
return head;
}

// Function to print elements of the circular linked list


void printListCircular(struct CircularNode* head) {
if (head == NULL) {
printf("NULL (empty list)\n");
return;
}

struct CircularNode* current = head;


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

int main() {
// Singly linked list
struct Node* singlyHead = NULL;

singlyHead = addToBeginning(singlyHead, 10);


singlyHead = addToEnd(singlyHead, 20);
singlyHead = addToEnd(singlyHead, 30);

printf("Singly Linked List:\n");


printList(singlyHead);

singlyHead = deleteNode(singlyHead, 20);

printf("After deleting node with data 20:\n");


printList(singlyHead);

// Doubly linked list


struct DoublyNode* doublyHead = NULL;

doublyHead = addToBeginningDoubly(doublyHead, 40);


doublyHead = addToEndDoubly(doublyHead, 50);
doublyHead = addToEndDoubly(doublyHead, 60);

printf("\nDoubly Linked List:\n");


printListDoubly(doublyHead);

doublyHead = deleteNodeDoubly(doublyHead, 50);


printf("After deleting node with data 50:\n");
printListDoubly(doublyHead);

// Circular linked list


struct CircularNode* circularHead = NULL;

circularHead = addToBeginningCircular(circularHead, 70);


circularHead = addToEndCircular(circularHead, 80);
circularHead = addToEndCircular(circularHead, 90);

printf("\nCircular Linked List:\n");


printListCircular(circularHead);

circularHead = deleteNodeCircular(circularHead, 80);

printf("After deleting node with data 80:\n");


printListCircular(circularHead);

return 0;
}

You might also like