Double Link List
The following implementation is in object oriented using the DoubleLinkList class and dll object. The implemented ADT functions of Double Link List are insert, print, search, modify and delete.
The following C++ code will explains the implementation of Double Link List.
In DoubleLinkList.h
#include <iostream>
using namespace std;
class DoubleLinkList
{
public:
struct node
{
int data;
node *next;
node *prev;
}*f, *p, *c;
int t;
public:
DoubleLinkList()
{
f = new node;
f->prev = NULL;
p = c = f;
t = 0;
}
void Insert (int num)
{
if (t == 0)
{
c->data = num;
c->next = NULL;
}
else
{
c = new node;
c->prev = p;
p->next = c;
c->data = num;
c->next = NULL;
p = c;
}
++t;
}
void Print ()
{
c = f;
cout << "Node\tPrevious Address\tAddress\t\tData\tNext address";
cout << "\n----\t----------------\t-------\t\t----\t------------";
for (int i = 0; i < t; ++i)
{
cout << "\n " << i+1 << ".\t " << c->prev << "\t\t" << c << "\t " << c->data << "\t " << c->next;
c = c->next;
}
}
void Search (int num)
{
c = f;
bool flag = true;
cout << "\nNode\tPrevious Address\tAddress\t\tData\tNext address";
cout << "\n----\t----------------\t-------\t\t----\t------------";
for (int i = 0; i < t; ++i)
{
if (num == c->data)
{
cout << "\n " << i+1 << ".\t " << c->prev << "\t\t" << c << "\t " << c->data << "\t " << c->next;
flag = false;
}
c = c->next;
}
if (flag == true)
{
cout << "\nYou didn't enter this number before.";
}
}
void Modify (int num)
{
c = f;
bool flag = true;
for (int i = 0; i < t; ++i)
{
if (num == c->data)
{
flag = false;
cout << "\nNumber " << num << " found at location : " << c << endl;
cout << "Enter the number with which you want to modify: ";
cin >> c->data;
cout << "Number modified...\n";
}
c = c->next;
}
if (flag == true)
{
cout << "\nYou didn't enter this number before.\n";
}
Print();
}
void Del (int num)
{
p = c = f;
bool flag = true;
for (int i = 0; i < t; ++i)
{
if (num == c->data)
{
if (i == 0)
{
flag = false;
f = c = c->next;
f->prev = NULL;
cout << "\nNumber " << num << " found at location : " << c << endl;
cout << "Number deleted...\n";
p = c = f;
--t;
}
else if (i == t - 1)
{
flag = false;
c->next = NULL;
cout << "\nNumber " << num << " found at location : " << c << endl;
cout << "Number deleted...\n";
p = c;
--t;
}
else
{
flag = false;
p->next = c = c->next;
c->prev = p;
cout << "\nNumber " << num << " found at location : " << c << endl;
cout << "Number deleted...\n";
p = c;
--t;
}
}
if (flag == true)
p = c;
c = c->next;
}
if (flag == true)
cout << "\n\nYou didn't enter this number before.\n";
else if (t != 0)
delete c;
Print();
}
};
In main.cpp
#include "DoubleLinkList.h"
int main()
{
DoubleLinkList dll;
cout << "\n~~~~~~~~~~ INSERT ~~~~~~~~~~\n";
char ch = 'y';
while ((ch == 'y') || (ch == 'Y'))
{
int k;
cout << "\nEnter your value: ";
cin >> k;
dll.Insert(k);
cout << "Do you want to insert more values: (y/n): ";
cin >> ch;
}
cout << "\n\n\n~~~~~~~~~~ PRINT ~~~~~~~~~~\n";
dll.Print();
cout << "\n\n\n\n~~~~~~~~~~ SEARCH ~~~~~~~~~~\n";
ch = 'y';
while ((ch == 'y') || (ch == 'Y'))
{
int k;
cout << "Enter a number to search in entered numbers: ";
cin >> k;
dll.Search(k);
cout << "\nDo you want to search again: (y/n): ";
cin >> ch;
}
cout << "\n\n~~~~~~~~~~ MODIFY ~~~~~~~~~~\n";
ch = 'y';
while ((ch == 'y') || (ch == 'Y'))
{
int k;
cout << "\nEnter number to modify: ";
cin >> k;
dll.Modify(k);
cout << "\nDo you want to modify any other number again: (y/n): ";
cin >> ch;
}
cout << "\n\n~~~~~~~~~~ DELETE ~~~~~~~~~~\n";
ch = 'y';
while ((ch == 'y') || (ch == 'Y'))
{
int k;
cout << "\nEnter number to delete: ";
cin >> k;
dll.Del(k);
cout << "\nDo you want to delete any number again: (y/n): ";
cin >> ch;
}
return 0;
}
Feel free to comment with your questions and suggestions regarding the post content...!
No comments:
Post a Comment
Your valuable comments are appreciated...!