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