0% found this document useful (0 votes)
12 views15 pages

Doubly Circular Linked List

The document is a C++ program that implements a circular doubly linked list for managing student records. It provides functionalities to insert, delete, search, and update student roll numbers at various positions in the list. The program includes a user interface for interacting with these operations through a menu-driven approach.

Uploaded by

bscs22f22
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)
12 views15 pages

Doubly Circular Linked List

The document is a C++ program that implements a circular doubly linked list for managing student records. It provides functionalities to insert, delete, search, and update student roll numbers at various positions in the list. The program includes a user interface for interacting with these operations through a menu-driven approach.

Uploaded by

bscs22f22
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/ 15

#include <iostream>

using namespace std;

struct student {

int rollno;

student *next;

student *prev;

};

student *head = NULL;

void insertathead() {

student *newNode = new student();

cout << "Enter a value: ";

cin >> newNode->rollno;

if (head == NULL) {

head = newNode;

newNode->next = head;

newNode->prev = head;

} else {

student *tail = head->prev;

newNode->next = head;

newNode->prev = tail;

head->prev = newNode;

tail->next = newNode;

head = newNode;

}
void insertatend() {

student *newNode = new student();

cout << "Enter a value: ";

cin >> newNode->rollno;

if (head == NULL) {

head = newNode;

newNode->next = head;

newNode->prev = head;

} else {

student *tail = head->prev;

newNode->next = head;

newNode->prev = tail;

tail->next = newNode;

head->prev = newNode;

void insertatanyposition() {

int position;

cout << "Enter the position to insert the new node: ";

cin >> position;

if (position <= 0) {

cout << "Invalid position!" << endl;

return;

}
student *newNode = new student();

cout << "Enter roll number for the new node: ";

cin >> newNode->rollno;

if (head == NULL) {

head = newNode;

newNode->next = head;

newNode->prev = head;

return;

if (position == 1) {

insertathead();

return;

student *temp = head;

int count = 1;

while (temp->next != head && count < position - 1) {

temp = temp->next;

count++;

if (count == position - 1) {

newNode->next = temp->next;

newNode->prev = temp;

temp->next->prev = newNode;

temp->next = newNode;
} else {

cout << "Position out of bounds!" << endl;

delete newNode;

void print() {

if (head == NULL) {

cout << "List is empty." << endl;

return;

student *temp = head;

do {

cout << "Roll number of student: " << temp->rollno << endl;

temp = temp->next;

} while (temp != head);

void deletefromhead() {

if (head == NULL) {

cout << "List is empty." << endl;

return;

if (head->next == head) {

delete head;

head = NULL;

} else {

student *tail = head->prev;


student *temp = head;

head = head->next;

head->prev = tail;

tail->next = head;

delete temp;

cout << "Node deleted from head." << endl;

void deletefromend() {

if (head == NULL) {

cout << "List is empty." << endl;

return;

if (head->next == head) {

delete head;

head = NULL;

} else {

student *tail = head->prev;

student *newTail = tail->prev;

newTail->next = head;

head->prev = newTail;

delete tail;

cout << "Node deleted from end." << endl;

void deletefromanyposition() {
int position;

cout << "Enter the position to delete the node: ";

cin >> position;

if (position <= 0 || head == NULL) {

cout << "Invalid position or list is empty." << endl;

return;

if (position == 1) {

deletefromhead();

return;

student *temp = head;

int count = 1;

while (temp->next != head && count < position - 1) {

temp = temp->next;

count++;

if (count == position - 1 && temp->next != head) {

student *nodeToDelete = temp->next;

temp->next = nodeToDelete->next;

nodeToDelete->next->prev = temp;

delete nodeToDelete;

cout << "Node deleted from position " << position << "."<<endl;

}
}

void searchElement() {

int roll;

cout << "Enter the roll number to search: ";

cin >> roll;

student *temp = head;

int position = 1;

while (temp != NULL) {

if (temp->rollno == roll) {

cout << "Student with roll number " << roll << " found at position " << position << "." << endl;

return;

temp = temp->next;

position++;

cout << "Student with roll number " << roll << " not found in the list." << endl;

void updateElement() {

int position, newRoll;

cout << "Enter the position to update the roll number: ";

cin >> position;

if (position <= 0) {

cout << "Invalid position!" << endl;

return;
}

cout << "Enter the new roll number: ";

cin >> newRoll;

student *temp = head;

int count = 1;

while (temp != NULL && count < position) {

temp = temp->next;

count++;

if (temp == NULL) {

cout << "Position out of bounds!" << endl;

} else {

temp->rollno = newRoll;

cout << "Roll number at position " << position << " updated to " << newRoll << "." << endl;

int main() {

int ch;

do {

cout << "\nEnter choice according to the function you want to execute:" << endl;

cout << "1 - Insert at Head" << endl;

cout << "2 - Insert at End" << endl;


cout << "3 - Insert at Any Position" << endl;

cout<< " 4 - delete at Head :" << endl;

cout<< " 5- delete at end :" << endl;

cout<<" 6 - delete at any position :" << endl;

cout<<" 7- search a node: " << endl;

cout<< "8 - update a node :" << endl;

cout << "0 - Exit" << endl;

cout << "Your choice: ";

cin >> ch;

switch (ch) {

case 1:

insertathead();

cout << "List after inserting at head:" << endl;

print();

break;

case 2:

insertatend();

cout << "List after inserting at end:" << endl;

print();

break;

case 3:

insertatanyposition();

cout << "List after inserting at any position:" << endl;

print();

break;

case 4:
deletefromhead();

cout << "List after deleting from head:" << endl;

print();

break;

case 5:

deletefromend();

cout << "List after deleting from end:" << endl;

print();

break;

case 6:

deletefromanyposition();

cout << "List after inserting at any position:" << endl;

print();

break;

case 7:

searchElement();

cout << "List after inserting at any position:" << endl;

print();

break;

case 8:

updateElement();

cout << "List after inserting at any position:" << endl;

print();

break;

case 0:

cout << "Exiting program." << endl;

break;
default:

cout << "Invalid choice, please try again." << endl;

} while (ch != 0);

return 0;

Output :

Insert at head:

Insert at end:
Insert at any position:

Delete at head:
Delete at end:

Delete at any position:


Search :

Update:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy