0% found this document useful (0 votes)
19 views8 pages

Doubly Linked List

Dont use this

Uploaded by

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

Doubly Linked List

Dont use this

Uploaded by

niranjankota09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

​ Write a program to implement a doubly linked list with the following operations:
Insert at the beginning,
Insert at a given position,
Insert at the end,
Display.

#include <iostream>
#include <cstdlib>
using namespace std;

class linked
{
protected:
struct node
{
int info;
node *prev;
node *next;
};
typedef struct node Node;
Node *start;

public:
linked()
{
start = NULL;
}

void insert_beg();
void insert_end();
void insert_sp();
void display();
};

void linked::insert_beg()
{
Node *ptr = new Node;
int item;
cout << "Enter an Item:\t";
cin >> item;
ptr->info = item;
ptr->prev = NULL;
ptr->next = start;
if (start != NULL)
start->prev = ptr;
start = ptr;
}
void linked::insert_sp()
{
Node *temp, *ptr;
int y, item;
if (start == NULL) {
cout << "List is empty\n";
return;
}

temp = start;
cout << "Enter the element after which you want to insert: ";
cin >> y;

while (temp != NULL && temp->info != y)


{
temp = temp->next;
}

if (temp == NULL)
{
cout << "Element not found\n";
return;
}

ptr = new Node;


cout << "Enter item: ";
cin >> item;
ptr->info = item;

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

if (temp->next != NULL)
temp->next->prev = ptr;

temp->next = ptr;
}

void linked::insert_end()
{
Node *ptr, *temp;
int item;
cout << "Enter an Item:\t";
cin >> item;
ptr = new Node;
ptr->info = item;
ptr->next = NULL;
if (start == NULL)
{
ptr->prev = NULL;
start = ptr;
} else
{
temp = start;
while (temp->next != NULL)
temp = temp->next;
temp->next = ptr;
ptr->prev = temp;
}
}

void linked::display()
{
Node *ptr = start;
if (start == NULL)
{
cout << "List is Empty\n";
return;
}
cout << "List elements: ";
while (ptr != NULL)
{
cout << ptr->info << "\t";
ptr = ptr->next;
}
cout << endl;
}

int main()
{
linked l;
int ch;
while (1)
{
cout << "\n1. Insert at Beginning\n2. Insert at End\n3. Insert at Specific Position\n4. Display\n5.
Exit\n";
cout << "Enter your choice: ";
cin >> ch;

switch (ch)
{
case 1:
l.insert_beg();
break;
case 2:
l.insert_end();
break;
case 3:
l.insert_sp();
break;
case 4:
[Link]();
break;
case 5:
exit(0);
default:
cout << "Invalid choice\n";
}
}

return 0;
}
2. Write a program to implement a doubly linked list with the following operations:
Delete at the beginning,
Delete at a given position,
Delete at the end,
Display.

#include <iostream>
using namespace std;

class doublylinked
{
protected:
struct node
{
int info;
node *next, *prev;
};
typedef struct node Node;
Node *start;

public:
doublylinked()
{
start = NULL;
}

void insert_beg();
void display();
int del_beg();
void delete_sp();
int del_end();
};

void doublylinked::insert_beg()
{
Node *ptr = new Node;
int item;
cout << "Enter an Item:\t";
cin >> item;
ptr->info = item;
ptr->prev = NULL;
ptr->next = start;

if (start != NULL)
start->prev = ptr;

start = ptr;
}

void doublylinked::display()
{
Node *ptr;
if (start == NULL)
{
cout << "List is Empty\n";
} else
{
ptr = start;
cout << "List Elements:\n";
while (ptr != NULL)
{
cout << ptr->info << "\t";
ptr = ptr->next;
}
cout << "\n";
}
}

int doublylinked::del_beg()
{
Node *ptr;
int item;
if (start == NULL)
{
cout << "List is Empty\n";
return 0;
} else
{
ptr = start;
item = ptr->info;
start = start->next;
if (start != NULL)
start->prev = NULL;
delete ptr;
cout << "Deleted from beginning: " << item << "\n";
return item;
}
}

void doublylinked::delete_sp()
{
Node *temp;
int x;
if (start == NULL)
{
cout << "List is empty\n";
return;
}

cout << "Enter the element you want to delete:\t";


cin >> x;
temp = start;
while (temp != NULL && temp->info != x)
{
temp = temp->next;
}

if (temp == NULL) {
cout << "Element not found\n";
return;
}

cout << temp->info << " is deleted\n";

if (temp->prev == NULL && temp->next == NULL)


{
start = NULL;
} else if (temp->prev == NULL)
{
start = temp->next;
start->prev = NULL;
} else if (temp->next == NULL)
{
temp->prev->next = NULL;
} else
{
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
}

delete temp;
}

int doublylinked::del_end()
{
Node *temp;
int item;
if (start == NULL) {
cout << "List is Empty\n";
return 0;
}

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

item = temp->info;
if (temp->prev != NULL)
temp->prev->next = NULL;
else
start = NULL;

delete temp;
cout << "Deleted from end: " << item << "\n";
return item;
}

int main() {
doublylinked dl;
int ch;

while (1) {
cout << "\n1. Insert at Beginning";
cout << "\n2. Display";
cout << "\n3. Delete from Beginning";
cout << "\n4. Delete Specified";
cout << "\n5. Delete from End";
cout << "\n6. Exit";
cout << "\nEnter your choice:\t";
cin >> ch;

switch (ch) {
case 1: dl.insert_beg();
break;
case 2: [Link]();
break;
case 3: dl.del_beg();
break;
case 4: dl.delete_sp();
break;
case 5: dl.del_end();
break;
case 6: return 0;
default: cout << "Invalid choice\n";
}
}

return 0;
}

You might also like