0% found this document useful (0 votes)
4 views

17

The document presents a C program that implements a circular doubly linked list with functionalities to create nodes, append new nodes, count the number of nodes, and print the list. It includes a main function that allows user input for the number of elements and their values, demonstrating the list's operations. A test case is provided to illustrate the program's output when three elements are added to the list.

Uploaded by

Tech- 420
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)
4 views

17

The document presents a C program that implements a circular doubly linked list with functionalities to create nodes, append new nodes, count the number of nodes, and print the list. It includes a main function that allows user input for the number of elements and their values, demonstrating the list's operations. A test case is provided to illustrate the program's output when three elements are added to the list.

Uploaded by

Tech- 420
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/ 2

#include <stdio.

h>
#include <stdlib.h>

// Define the structure for a circular doubly linked list node


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 = newNode; // Point to itself (circular)
newNode->prev = newNode; // Point to itself (circular)
return newNode;
}

// Function to append a node to the circular doubly linked list


void append(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* tail = (*head)->prev;

// Update links to insert the new node at the end


tail->next = newNode;
newNode->prev = tail;
newNode->next = *head;
(*head)->prev = newNode;
}

// Function to count the number of nodes in the circular doubly linked list
int countNodes(struct Node* head) {
if (head == NULL) return 0; // Empty list

int count = 0;
struct Node* temp = head;

do {
count++;
temp = temp->next;
} while (temp != head); // Stop when we return to the head

return count;
}

// Function to print the circular doubly linked list


void printList(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


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

// Main function to test the implementation


int main() {
struct Node* head = NULL;
int n, data;

// User-defined input
printf("Enter the number of elements in the list: ");
scanf("%d", &n);

printf("Enter the elements of the list:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &data);
append(&head, data);
}

printf("Circular Doubly Linked List: ");


printList(head);

int count = countNodes(head);


printf("Number of nodes in the circular doubly linked list: %d\n", count);

return 0;
}
-----------------------------------------------------------------------------------
----------
Test case:
Enter the number of elements in the list: 3
Enter the elements of the list:
13
23
14
Circular Doubly Linked List: 13 23 14
Number of nodes in the circular doubly linked list: 3

You might also like