پياده سازي ليست پيوندي دو طرفه به زبان ++C
// dLinkList.cpp: implementation of the dLinkList class.
//
//////////////////////////////////////////////////////////////////////
#include "dLinkList.h"
#include
#include
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
dLinkList::dLinkList()
{
Front = NULL;
Rear = Front;
Count = 0;
}
dLinkList::~dLinkList()
{
NodePtr current, temp;
current = Front;
while (current != NULL)
{
temp = current;
current = current->Right;
delete temp;
}
Front = NULL;
Rear = Front;
Count = 0;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* InsertFirst */
/* Task: To insert a new node containing Item at the front of the list*/
/* Given: Item: A data item */
/* Return: Nothing */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void dLinkList::InsertFirst(char item)
{
NodePtr current;
current = new Node;
if (current == NULL)
{
cerr << "Memory allocation error!" << endl;
exit(1);
}
current->Right = Front;
current->Left = NULL;
current->Info = item;
Front = current;
if (Count == 0)
Rear = current;
else
Front->Left = current;
Count++;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* InsertLast */
/* Task: To insert Item into a new node added to the rear of the list.*/
/* Given: Item A data item */
/* Return: Nothing */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void dLinkList::InsertLast(char item)
{
NodePtr current;
current = new Node;
if (current == NULL)
{
cerr << "Memory allocation error!" << endl;
exit(1);
}
current->Right = NULL;
current->Left = Rear;
current->Info = item;
if (Count == 0)
Front = current;
else
Rear->Right = current;
Rear = current;
Count++;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* RemoveFirst */
/* Task: To remove first node from the list. */
/* Given: Nothing */
/* Return: Data item that removed from the list */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void dLinkList::RemoveFirst(char &item)
{
NodePtr current;
if (Count == 0)
{
cerr << "ERROR: there is no item to remove in the list!" << endl;
exit(1);
}
current = Front;
item = current->Info;
Front = current->Right;
Front->Left = NULL;
Count--;
if (Count == 0)
Rear = Front;
delete current;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* ShowList */
/* Task: Show all data item of the list. */
/* Given: Nothing */
/* Return: Nothing */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void dLinkList::ShowFirstToLast(void)
{
NodePtr current;
current = Front;
while (current != NULL)
{
cout<< current->Info< current = current->Right;
}