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

Doubly Circular Linklist

The document contains the code for a C program that implements a doubly circular linked list. It defines functions to create nodes, insert nodes at the beginning and end of the list, delete nodes from the beginning and end, search for a value, and display the list. The main function allows the user to test the different functions by selecting options from a menu to insert, delete, search or display the list.

Uploaded by

VIKRAMADITYA
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)
39 views9 pages

Doubly Circular Linklist

The document contains the code for a C program that implements a doubly circular linked list. It defines functions to create nodes, insert nodes at the beginning and end of the list, delete nodes from the beginning and end, search for a value, and display the list. The main function allows the user to test the different functions by selecting options from a menu to insert, delete, search or display the list.

Uploaded by

VIKRAMADITYA
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

//Name- YASH DATTATRAY WAGHMARE

//Roll_No-67

//Pgm- doubly circular Linked list

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct Node {

int data;

struct Node* next;

struct Node* prev;

};

// Function to create a new node

struct Node* createNode(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

newNode->prev = NULL;

return newNode;

// Function to insert at the beginning

void insertAtBeginning(struct Node** head, int value) {

struct Node* newNode = createNode(value);

if (*head == NULL) {

*head = newNode;
(*head)->next = *head;

(*head)->prev = *head;

} else {

newNode->next = *head;

newNode->prev = (*head)->prev;

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

(*head)->prev = newNode;

*head = newNode;

// Function to insert at last

void insertAtLast(struct Node** head, int value) {

struct Node* newNode = createNode(value);

if (*head == NULL) {

*head = newNode;

(*head)->next = *head;

(*head)->prev = *head;

} else {

newNode->next = *head;

newNode->prev = (*head)->prev;

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

(*head)->prev = newNode;

// Function to delete from the beginning


void deleteFromBeginning(struct Node** head) {

if (*head == NULL) {

printf("List is empty.\n");

} else {

struct Node* temp = *head;

(*head)->prev->next = (*head)->next;

(*head)->next->prev = (*head)->prev;

*head = (*head)->next;

free(temp);

// Function to delete from the last

void deleteFromLast(struct Node** head) {

if (*head == NULL) {

printf("List is empty.\n");

} else {

struct Node* temp = (*head)->prev;

(*head)->prev = temp->prev;

temp->prev->next = *head;

free(temp);

// Function to search for a value

void search(struct Node* head, int value) {

struct Node* current = head;


int found = 0, position = 0;

do {

position++;

if (current->data == value) {

found = 1;

break;

current = current->next;

} while (current != head);

if (found) {

printf("Value %d found at position %d.\n", value, position);

} else {

printf("Value %d not found in the list.\n", value);

// Function to display the list

void show(struct Node* head) {

if (head == NULL) {

printf("List is empty.\n");

} else {

struct Node* current = head;

do {

printf("%d ", current->data);

current = current->next;

} while (current != head);


printf("\n");

// Main function

int main() {

struct Node* head = NULL;

int choice, value;

do {

printf("\n1. Insert at Beginning\n2. Insert at Last\n3. Delete from Beginning\n");

printf("4. Delete from Last\n5. Search\n6. Show\n0. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insertAtBeginning(&head, value);

break;

case 2:

printf("Enter value to insert: ");

scanf("%d", &value);

insertAtLast(&head, value);

break;

case 3:
deleteFromBeginning(&head);

break;

case 4:

deleteFromLast(&head);

break;

case 5:

printf("Enter value to search: ");

scanf("%d", &value);

search(head, value);

break;

case 6:

show(head);

break;

case 0:

printf("Exiting the program.\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

} while (choice != 0);

return 0;

Output:-

1. Insert at Beginning

2. Insert at Last
3. Delete from Beginning

4. Delete from Last

5. Search

6. Show

0. Exit

Enter your choice: 1

Enter value to insert: 10

1. Insert at Beginning

2. Insert at Last

3. Delete from Beginning

4. Delete from Last

5. Search

6. Show

0. Exit

Enter your choice: 2

Enter value to insert: 20

1. Insert at Beginning

2. Insert at Last

3. Delete from Beginning

4. Delete from Last

5. Search

6. Show

0. Exit

Enter your choice: 1

Enter value to insert: 30


1. Insert at Beginning

2. Insert at Last

3. Delete from Beginning

4. Delete from Last

5. Search

6. Show

0. Exit

Enter your choice: 1

Enter value to insert: 30

1. Insert at Beginning

2. Insert at Last

3. Delete from Beginning

4. Delete from Last

5. Search

6. Show

0. Exit

Enter your choice: 5

Enter value to search: 30

Value 30 found at position 1.

1. Insert at Beginning

2. Insert at Last

3. Delete from Beginning

4. Delete from Last

5. Search
6. Show

0. Exit

Enter your choice: 6

30 10 20

You might also like