// "DoublyLinkedList.cpp"
#include<iostream>
#include<conio.h>
using namespace std;
class node
{
public:
int data;
node *right_link, *left_link;
};
class friend_node
{
private:
node *head, *current_node;
void add_to_list(int);
int move_right();
int move_left();
void display_list();
void delete_record();
void dispose_list();
void print_options();
public:
friend_node();
~friend_node();
void handle_choice();
};
friend_node::friend_node()
{
head = new node;
head->data = 0;
head->left_link = head;
head->right_link = head;
current_node = head;
}
friend_node::~friend_node()
{
delete head;
}
void main()
{
friend_node go;
cout << "Welcome to the Doubly Linked List Simulation\n\n";
cout << "1 - Exit program\n";
cout << "2 - Add\n";
cout << "3 - Move left\n";
cout << "4 - Move right\n";
cout << "5 - Print\n";
cout << "6 - Delete\n";
cout << "7 - Dispose\n";
cout << "8 - Show these options again\n";
go.handle_choice();
}
void friend_node::handle_choice()
{
friend_node free_memory;
int value;
int choice;
while (choice != 1)
{
cout << "\nEnter choice: ";
cin >> choice;
int temp = 0;
switch(choice)
{
case 1:
free_memory.~friend_node();
cout << "\nThank you!\n\n";
getch();
exit(1);
break;
case 2:
cout << "\nEnter a number: ";
cin >> value;
temp = value;
cout << "\n" << temp << " has been added!\n";
getch();
add_to_list(value);
break;
case 3:
cout << "\nCurrent data to the left is: " << move_left() << "\n\n";
getch();
break;
case 4:
cout << "\nCurrent data to the right is: " << move_right() << "\n\n";
getch();
break;
case 5:
display_list();
break;
case 6:
delete_record();
break;
case 7:
dispose_list();
break;
case 8:
print_options();
break;
default:
cout << "\nInvalid choice!\n\n";
getch();
break;
}
}
}
void friend_node::add_to_list(int add_data)
{
node *add = new node;
add->data = add_data;
add->left_link = current_node;
add->right_link = current_node->right_link;
current_node->right_link->left_link = add;
current_node->right_link = add;
current_node = add;
}
int friend_node::move_left()
{
char empty[30] = {"\n\n\a\a\a\aThe list is empty!\n\n"};
if(head->right_link == head)
{
cout << empty << endl;
getch();
handle_choice();
}
else
{
current_node = current_node->left_link;
if(current_node == head)
{
current_node = current_node->left_link;
return (current_node->data);
}
else
return(current_node->data);
}
return (0);
}
int friend_node::move_right()
{
char empty[30] = {"\n\n\a\a\a\aThe list is empty!\n\n"};
if(head->right_link == head)
{
cout << empty << endl;
getch();
handle_choice();
}
else
{
current_node = current_node->right_link;
if(current_node == head)
{
current_node = current_node->right_link;
return (current_node->data);
}
else
return(current_node->data);
}
return (0);
}
void friend_node::display_list(void)
{
char empty[30] = {"\n\n\a\a\a\aNothing to print!\n\n"};
node *print;
if(head->right_link == head)
{
cout << empty << endl;
getch();
handle_choice();
}
else
{
cout << "\nCurrent data in the list: ";
print = head->right_link;
while(print != head)
{
cout << print->data <<" ";
print = print->right_link;
}
}
getch();
cout <<"\n\n";
}
void friend_node::delete_record()
{
char empty[30] = {"\n\n\a\a\a\aNothing to delete!\n\n"};
node *temp_current = new node;
node *temp_current2 = temp_current;
temp_current = current_node;
if (head->right_link == head)
{
cout << empty << endl;
getch();
handle_choice();
}
while(current_node != head)
{
cout << "\n\n" << current_node->data << " has been deleted!\n\n";
current_node->left_link->right_link = current_node->right_link;
current_node->right_link->left_link = current_node->left_link;
current_node = current_node->right_link;
current_node = current_node->left_link;
getch();
handle_choice();
}
}
void friend_node::dispose_list()
{
char empty[30] = {"\n\n\a\a\a\aNothing to dispose!\n\n"};
if(head->right_link == head)
{
cout << empty << endl;
getch();
handle_choice();
}
while(current_node != head)
{
current_node->left_link->right_link = current_node->right_link;
current_node->right_link->left_link = current_node->left_link;
current_node = current_node->right_link;
current_node = current_node->left_link;
if(current_node == head)
{
cout << "\n\nThe list has been disposed!\n\n";
getch();
}
}
}
void friend_node::print_options()
{
cout << "\n\n1 - Exit program\n";
cout << "2 - Add\n";
cout << "3 - Move left\n";
cout << "4 - Move right\n";
cout << "5 - Print\n";
cout << "6 - Delete\n";
cout << "7 - Dispose\n";
cout << "8 - Show these options again\n\n";
}