[go: up one dir, main page]

0% found this document useful (0 votes)
6 views11 pages

Doubly Linked List

The document contains C++ code for a doubly linked list implementation with functionalities to insert, delete, and display nodes. It also includes a doctor management system that allows appending doctors, listing them by specialization, reversing the list, and displaying the list. The main function demonstrates the usage of these functionalities through user interaction.

Uploaded by

Divya Rahane
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)
6 views11 pages

Doubly Linked List

The document contains C++ code for a doubly linked list implementation with functionalities to insert, delete, and display nodes. It also includes a doctor management system that allows appending doctors, listing them by specialization, reversing the list, and displaying the list. The main function demonstrates the usage of these functionalities through user interaction.

Uploaded by

Divya Rahane
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/ 11

#include <iostream>

using namespace std;

struct node {

int data;

node* next;

node* prev;

node(int val) : data(val), next(NULL) {} // Constructor to initialize the node

};

void insertAtBeginning(node* &head, int data){

node* n = new node(data);

n->next=head;

if(head != NULL){

head->prev=n;

head=n;

void insertAtEnd(node*& head, int data) {


node* n = new node(data);

if (head == NULL) { // If the list is empty, set the new node as the head

insertAtBeginning(head, data);

return;
}

node* last = head;

while (last->next != NULL) { // Traverse to the end of the list

last = last->next;

last->next = n; // Link the new node to the last node

n->prev = last;

void deleteAtEnd(node* head){

node* temp = head;

while(temp->next!=NULL){

temp=temp->next;

temp->prev->next = NULL;

delete temp;

void deleteAtHead(node*& head) {


if (head == NULL) {

cout << "List is empty. Nothing to delete." << endl;

return;

node* temp = head;

head = head->next;
if (head != NULL) { // If list has more than one node

head->prev = NULL;

delete temp;

void deleteAtMiddle(node*& head, int pos) {


if (head == NULL) {

cout << "List is empty. Nothing to delete." << endl;

return;

if (pos <= 0) {

cout << "Invalid position." << endl;

return;

if (pos == 1) { // Delete the head node

deleteAtHead(head);

return;

node* temp = head;

int count = 1;

// Traverse to the position

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

temp = temp->next;

count++;

}
if (temp == NULL) { // Position is out of bounds

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

return;

temp->prev->next = temp->next; // Link previous node to the next node

if (temp->next != NULL) { // If it's not the last node

temp->next->prev = temp->prev;

delete temp; // Delete the node at the position

void display(node* head) {


node* temp = head;

while (temp != NULL) { // Traverse the list until the end

cout << temp->data;

if (temp->next != NULL) { // Print `->` only if there is a next node

cout << " -> ";

temp = temp->next;

cout << " -> NULL" << endl; // Print NULL at the end

int main() {
node* head = NULL; // Initialize the head to NULL

insertAtEnd(head, 1);

insertAtEnd(head, 2);

insertAtEnd(head, 3);

insertAtEnd(head, 4);

display(head); // Display the linked list

insertAtBeginning(head, 7);

display(head);

cout<<"\nAfter deleting End element :";

deleteAtEnd(head);

display(head);

int pos;

cout << "Enter position to delete an element: ";

cin >> pos;

deleteAtMiddle(head, pos);

cout << "\nAfter deleting the element at position " << pos << ": ";

display(head);

return 0;

}
void traverseForward(node* head) {
cout << "Traversing forward: ";

node* temp = head;

while (temp != NULL) { // Traverse from head to tail

cout << temp->data;

if (temp->next != NULL) {

cout << " -> ";

temp = temp->next;

cout << " -> NULL" << endl;

void traverseBackward(node* head) {


if (head == NULL) {

cout << "List is empty. Nothing to traverse backward." << endl;

return;

// Traverse to the last node

node* tail = head;

while (tail->next != NULL) {

tail = tail->next;

cout << "Traversing backward: ";

// Traverse from tail to head

while (tail != NULL) {

cout << tail->data;

if (tail->prev != NULL) {

cout << " -> ";


}

tail = tail->prev;

cout << " -> NULL" << endl;

#include <iostream>

#include <string>

using namespace std;

struct Doctor {

string name;

string specialization;

string phone;

Doctor* next;

Doctor* prev;

Doctor(string n, string s, string p) : name(n), specialization(s), phone(p), next(NULL), prev(NULL) {}

};

void appendDoctor(Doctor*& head, string name, string specialization, string phone) {

Doctor* newDoctor = new Doctor(name, specialization, phone);

if (head == NULL) {

head = newDoctor;

return;

Doctor* temp = head;

while (temp->next != NULL) {


temp = temp->next;

temp->next = newDoctor;

newDoctor->prev = temp;

void listDoctorsBySpecialization(Doctor* head, string specialization) {

bool found = false;

while (head != NULL) {

if (head->specialization == specialization) {

cout << "Name: " << head->name << ", Phone: " << head->phone << endl;

found = true;

head = head->next;

if (!found) {

cout << "No doctors found with specialization: " << specialization << endl;

void reverseList(Doctor*& head) {

if (head == NULL) {

cout << "The list is empty. Nothing to reverse." << endl;

return;

Doctor* temp = NULL;

Doctor* current = head;

// Swap next and prev pointers for each node

while (current != NULL) {


temp = current->prev;

current->prev = current->next;

current->next = temp;

current = current->prev;

// Reset the head to the last node

if (temp != NULL) {

head = temp->prev;

cout << "The list has been reversed." << endl;

void displayList(Doctor* head) {

if (head == NULL) {

cout << "The list is empty." << endl;

return;

cout << "Doctor List: " << endl;

while (head != NULL) {

cout << "Name: " << head->name

<< ", Specialization: " << head->specialization

<< ", Phone: " << head->phone << endl;

head = head->next;

int main() {

Doctor* head = NULL;

int choice;
string name, specialization, phone;

do {

cout << "\nMenu:" << endl;

cout << "1. Append Doctor" << endl;

cout << "2. List Doctors by Specialization" << endl;

cout << "3. Reverse the List" << endl;

cout << "4. Display List" << endl;

cout << "5. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter Doctor Name: ";

cin.ignore();

getline(cin, name);

cout << "Enter Specialization: ";

getline(cin, specialization);

cout << "Enter Phone Number: ";

getline(cin, phone);

appendDoctor(head, name, specialization, phone);

cout << "Doctor added successfully!" << endl;

break;

case 2:

cout << "Enter Specialization to search: ";

cin.ignore();

getline(cin, specialization);

listDoctorsBySpecialization(head, specialization);

break;

case 3:
reverseList(head);

break;

case 4:

displayList(head);

break;

case 5:

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

break;

default:

cout << "Invalid choice. Please try again." << endl;

} while (choice != 5);

return 0;

You might also like