Doubly circular linked complete solution
/*
-------------------------------------------------------------------------
------------- This is the code for Doubly Circular linked List ----------
-------------------------------------------------------------------------
*/
#include <iostream>
template <typename type>
class DblCrclrLnkLst
{
public:
//----- Default constructor -------//
DblCrclrLnkLst<type>()
{
this->Start = nullptr;
}
//------- Overloaded Constructor -----//
DblCrclrLnkLst<type>(const DblCrclrLnkLst<type> &item)
{
this->Start = nullptr;
*this = item;
}
//------- Push Function to insert at end -------//
void push(const type &item)
{
Node *ptr = nullptr;
ptr = this->Start;
Node *temp = nullptr;
if (ptr)
{
ptr = ptr->Previous;
temp = new Node;
temp->Data = item;
temp->Next = this->Start;
temp->Previous = ptr;
ptr->Next = temp;
ptr = this->Start;
ptr->Previous = temp;
}
else
{
temp = new Node;
temp->Data = item;
temp->Next = temp;
temp->Previous = temp;
this->Start = temp;
}
}
//------ Pop Function to get data from end -----//
type pop()
{
Node *ptr = nullptr;
Node *temp = nullptr;
ptr = this->Start;
temp = this->Start;
type _Data_ = NULL;
if (ptr)
{
ptr = ptr->Previous;
_Data_ = ptr->Data;
temp->Previous = ptr->Previous;
if (temp->Previous == this->Start)
{
delete[] ptr;
this->Start = nullptr;
}
else
{
delete[] ptr;
}
}
return _Data_;
}
//------- Enqueue Function to store data at start ------//
void Enqueue(const type &item)
{
Node *ptr = nullptr;
Node *temp = nullptr;
temp = new Node;
ptr = this->Start;
if (ptr)
{
temp->Data = item;
temp->Next = this->Start;
ptr = ptr->Previous;
ptr->Next = temp;
temp->Previous = ptr;
ptr = this->Start;
ptr->Previous = temp;
this->Start = temp;
}
else
{
temp = new Node;
temp->Next = temp;
temp->Previous = temp;
temp->Data = item;
this->Start = temp;
}
}
//------- Dequeue Function to get data from start ------//
type Dequeue()
{
Node *ptr = nullptr;
Node *temp = nullptr;
type _Data_ = NULL;
ptr = this->Start;
if (ptr)
{
Node *spec = ptr;
_Data_ = ptr->Data;
temp = ptr->Next;
temp->Previous = ptr->Previous;
ptr = ptr->Previous;
ptr->Next = temp;
if (temp->Previous == this->Start)
{
delete[] this->Start;
this->Start = nullptr;
}
else
{
delete[] spec;
this->Start = temp;
}
}
return _Data_;
}
//------- Printing List function ---------//
void PrintList()
{
Node *ptr = nullptr;
ptr = this->Start;
if (ptr)
{
cout << "Previous\tNext\t\tData" << endl;
while (ptr->Next != this->Start)
{
cout << ptr->Previous;
cout << "\t";
cout << ptr->Next;
cout << "\t";
cout << ptr->Data;
cout << endl;
ptr = ptr->Next;
}
cout << ptr->Previous;
cout << "\t";
cout << ptr->Next;
cout << "\t";
cout << ptr->Data;
cout << endl;
ptr = ptr->Next;
}
}
//------- Find Function --------//
bool FindItem(const type &item)
{
Node *temp = Find(item);
if (temp)
return true;
return false;
}
//------ Assignment operator overloading ------//
DblCrclrLnkLst<type> &operator = (const DblCrclrLnkLst<type> &item)
{
if (this != &item)
{
this->EraseList();
Node *ptr = nullptr;
ptr = item.Start;
while (ptr->Next!=item.Start)
{
this->Enqueue(ptr->Data);
ptr = ptr->Next;
}
if (ptr->Next !=ptr->Previous)
{
this->Enqueue(ptr->Data);
}
}
return *this;
}
/********************************************
*********************************************
*************** PRIVATE *********************
*********************************************
********************************************/
private:
struct Node
{
type Data;
Node *Next=nullptr;
Node *Previous=nullptr;
};
Node *Start;
Node *Find(const type &item)
{
Node *ptr = nullptr;
ptr = this->Start;
if (ptr)
{
while ((ptr->Next!=this->Start) || (ptr->Data!=item))
{
ptr = ptr->Next;
}
if (ptr->Data == item)
return ptr;
}
return nullptr;
}
void EraseList()
{
Node *ptr = nullptr;
ptr = this->Start;
if (ptr)
{
ptr = ptr->Previous;
while (ptr != this->Start)
{
delete[] ptr;
ptr = ptr->Previous;
}
delete[] this->Start;
}
this->Start = nullptr;
}
};
-------------------------------------------------------------------------
------------- This is the code for Doubly Circular linked List ----------
-------------------------------------------------------------------------
*/
#include <iostream>
template <typename type>
class DblCrclrLnkLst
{
public:
//----- Default constructor -------//
DblCrclrLnkLst<type>()
{
this->Start = nullptr;
}
//------- Overloaded Constructor -----//
DblCrclrLnkLst<type>(const DblCrclrLnkLst<type> &item)
{
this->Start = nullptr;
*this = item;
}
//------- Push Function to insert at end -------//
void push(const type &item)
{
Node *ptr = nullptr;
ptr = this->Start;
Node *temp = nullptr;
if (ptr)
{
ptr = ptr->Previous;
temp = new Node;
temp->Data = item;
temp->Next = this->Start;
temp->Previous = ptr;
ptr->Next = temp;
ptr = this->Start;
ptr->Previous = temp;
}
else
{
temp = new Node;
temp->Data = item;
temp->Next = temp;
temp->Previous = temp;
this->Start = temp;
}
}
//------ Pop Function to get data from end -----//
type pop()
{
Node *ptr = nullptr;
Node *temp = nullptr;
ptr = this->Start;
temp = this->Start;
type _Data_ = NULL;
if (ptr)
{
ptr = ptr->Previous;
_Data_ = ptr->Data;
temp->Previous = ptr->Previous;
if (temp->Previous == this->Start)
{
delete[] ptr;
this->Start = nullptr;
}
else
{
delete[] ptr;
}
}
return _Data_;
}
//------- Enqueue Function to store data at start ------//
void Enqueue(const type &item)
{
Node *ptr = nullptr;
Node *temp = nullptr;
temp = new Node;
ptr = this->Start;
if (ptr)
{
temp->Data = item;
temp->Next = this->Start;
ptr = ptr->Previous;
ptr->Next = temp;
temp->Previous = ptr;
ptr = this->Start;
ptr->Previous = temp;
this->Start = temp;
}
else
{
temp = new Node;
temp->Next = temp;
temp->Previous = temp;
temp->Data = item;
this->Start = temp;
}
}
//------- Dequeue Function to get data from start ------//
type Dequeue()
{
Node *ptr = nullptr;
Node *temp = nullptr;
type _Data_ = NULL;
ptr = this->Start;
if (ptr)
{
Node *spec = ptr;
_Data_ = ptr->Data;
temp = ptr->Next;
temp->Previous = ptr->Previous;
ptr = ptr->Previous;
ptr->Next = temp;
if (temp->Previous == this->Start)
{
delete[] this->Start;
this->Start = nullptr;
}
else
{
delete[] spec;
this->Start = temp;
}
}
return _Data_;
}
//------- Printing List function ---------//
void PrintList()
{
Node *ptr = nullptr;
ptr = this->Start;
if (ptr)
{
cout << "Previous\tNext\t\tData" << endl;
while (ptr->Next != this->Start)
{
cout << ptr->Previous;
cout << "\t";
cout << ptr->Next;
cout << "\t";
cout << ptr->Data;
cout << endl;
ptr = ptr->Next;
}
cout << ptr->Previous;
cout << "\t";
cout << ptr->Next;
cout << "\t";
cout << ptr->Data;
cout << endl;
ptr = ptr->Next;
}
}
//------- Find Function --------//
bool FindItem(const type &item)
{
Node *temp = Find(item);
if (temp)
return true;
return false;
}
//------ Assignment operator overloading ------//
DblCrclrLnkLst<type> &operator = (const DblCrclrLnkLst<type> &item)
{
if (this != &item)
{
this->EraseList();
Node *ptr = nullptr;
ptr = item.Start;
while (ptr->Next!=item.Start)
{
this->Enqueue(ptr->Data);
ptr = ptr->Next;
}
if (ptr->Next !=ptr->Previous)
{
this->Enqueue(ptr->Data);
}
}
return *this;
}
/********************************************
*********************************************
*************** PRIVATE *********************
*********************************************
********************************************/
private:
struct Node
{
type Data;
Node *Next=nullptr;
Node *Previous=nullptr;
};
Node *Start;
Node *Find(const type &item)
{
Node *ptr = nullptr;
ptr = this->Start;
if (ptr)
{
while ((ptr->Next!=this->Start) || (ptr->Data!=item))
{
ptr = ptr->Next;
}
if (ptr->Data == item)
return ptr;
}
return nullptr;
}
void EraseList()
{
Node *ptr = nullptr;
ptr = this->Start;
if (ptr)
{
ptr = ptr->Previous;
while (ptr != this->Start)
{
delete[] ptr;
ptr = ptr->Previous;
}
delete[] this->Start;
}
this->Start = nullptr;
}
};
Comments
Post a Comment