此文为上一篇文章改为C++格式实现(多文件形式)
头文件
#pragma once
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct Node
{
int iData;
struct Node* pNext;
struct Node* pPre;
};
class SX
{
private:
struct Node* g_pHead = NULL;
struct Node* g_pEnd = NULL;
int g_iNodeCount = 0;
private:
void FreeList();
public:
SX();
~SX();
public:
void AddToEnd(int iData);
void LookZheng();
void LookFan();
void AddToHead(int iData);
void InsertNodeByIndex(int iIndex, int iCount, int iData);
void InsertNodeByData(int iValue, int iData);
struct Node* GetByIndex(int iIndex);
struct Node* GetByData(int iData);
void ChangeByIndex(int iIndex, int iValue);
void ChangeByData(int iData, int iValue);
void DeleteByIndex(int iIndex);
void DeleteNode(struct Node* pTemp);
void DeleteByData(int iValue);
};
函数实现
#include"sx.h"
SX::SX()
{
}
SX::~SX()
{
FreeList();
}
void SX::DeleteByData(int iValue)
{
struct Node* pTemp = NULL;
while (NULL != (pTemp = GetByData(iValue)))
DeleteNode(pTemp);
}
void SX::DeleteNode(struct Node* pTemp)
{
if (pTemp == g_pHead)
{
if (g_pHead == g_pEnd)
{
delete g_pHead;
g_pHead = NULL;
g_pEnd = NULL;
}
else
{
g_pHead = g_pHead->pNext;
delete (g_pHead->pPre);
g_pHead->pPre = NULL;
}
}
else if (pTemp == g_pEnd)
{
g_pEnd = g_pEnd->pPre;
delete(g_pEnd->pNext);
g_pEnd->pNext = NULL;
}
else
{
pTemp->pPre->pNext = pTemp->pNext;
pTemp->pNext->pPre = pTemp->pPre;
delete (pTemp);
}
g_iNodeCount--;
}
void SX::DeleteByIndex(int iIndex)
{
struct Node* pTemp = GetByIndex(iIndex);
if (pTemp != NULL)
{
DeleteNode(pTemp);
}
}
void SX::ChangeByData(int iData, int iValue)
{
if (NULL == g_pHead)
return;
struct Node* pTemp = g_pHead;
while (pTemp != NULL)
{
if (pTemp->iData == iData)
pTemp->iData = iValue;
pTemp = pTemp->pNext;
}
}
void SX::ChangeByIndex(int iIndex, int iValue)
{
if (NULL == g_pHead || iIndex < 0 || iIndex >= g_iNodeCount)
return;
struct Node* pTemp = GetByIndex(iIndex);
if (pTemp != NULL)
{
pTemp->iData = iValue;
}
}
Node* SX::GetByData(int iData)
{
if (NULL == g_pHead)
return NULL;
struct Node* pTemp = g_pHead;
while (pTemp != NULL)
{
if (pTemp->iData == iData)
break;
pTemp = pTemp->pNext;
}
return pTemp;
}
Node* SX::GetByIndex(int iIndex)
{
if (NULL == g_pHead || iIndex < 0 || iIndex >= g_iNodeCount)
return NULL;
struct Node* pTemp = g_pHead;
for (int i = 0; i < iIndex; i++)
pTemp = pTemp->pNext;
return pTemp;
}
void SX::InsertNodeByData(int iValue, int iData)
{
if (NULL == g_pHead)
return;
struct Node* pTemp = g_pHead;
while (pTemp != NULL)
{
if (pTemp->iData == iValue)
break;
pTemp = pTemp->pNext;
}
if (pTemp != NULL)
{
if (pTemp == g_pHead)
AddToHead(iData);
else
{
struct Node* pNew = new struct Node;
if (NULL == pNew)
return;
pNew->iData = iData;
pNew->pNext = NULL;
pNew->pPre = NULL;
pTemp->pPre->pNext = pNew;
pNew->pPre = pTemp->pPre;
pNew->pNext = pTemp;
pTemp->pPre = pNew;
g_iNodeCount += 1;
}
}
}
void SX::InsertNodeByIndex(int iIndex, int iCount, int iData)
{
if (iIndex < 0 || iIndex>g_iNodeCount || iCount <= 0)
return;
if (0 == iIndex)
{
for (int i = 0; i < iCount; i++)
AddToHead(iData);
}
else if (iIndex == g_iNodeCount)
{
for (int i = 0; i < iCount; i++)
AddToEnd(iData);
}
else
{
struct Node* pTemp = g_pHead;
for (int i = 0; i < iIndex; i++)
pTemp = pTemp->pNext;
for (int i = 0; i < iCount; i++)
{
struct Node* pNew = new struct Node;
if (pNew == NULL)
return;
pNew->iData = iData;
pNew->pNext = NULL;
pNew->pPre = NULL;
pTemp->pPre->pNext = pNew;
pNew->pPre = pTemp->pPre;
pNew->pNext = pTemp;
pTemp->pPre = pNew;
}
g_iNodeCount += iCount;
}
}
void SX::AddToHead(int iData)
{
struct Node* pTemp = new struct Node;
if (pTemp == NULL)
return;
pTemp->iData = iData;
pTemp->pNext = NULL;
pTemp->pPre = NULL;
if (NULL == g_pHead)
{
g_pEnd = pTemp;
}
else
{
pTemp->pNext = g_pHead;
g_pHead->pPre = pTemp;
}
g_pHead = pTemp;
g_iNodeCount++;
}
void SX::LookFan()
{
if (NULL == g_pEnd)
return;
printf("共有 %d 个节点: ", g_iNodeCount);
struct Node* pTemp = g_pEnd;
while (pTemp != NULL)
{
printf("%d ", pTemp->iData);
pTemp = pTemp->pPre;
}
putchar('\n');
}
void SX::LookZheng()
{
if (NULL == g_pHead)
return;
printf("共有 %d 个节点: ", g_iNodeCount);
struct Node* pTemp = g_pHead;
while (pTemp != NULL)
{
printf("%d ", pTemp->iData);
pTemp = pTemp->pNext;
}
putchar('\n');
}
void SX::FreeList()
{
if (NULL == g_pHead)
return;
Node* pTemp = g_pHead;
if (pTemp == NULL)
return;
while (pTemp != NULL)
{
struct Node* pT = pTemp;
pTemp = pTemp->pNext;
delete pT;
}
g_pHead = NULL;
g_pEnd = NULL;
g_iNodeCount = 0;
}
void SX::AddToEnd(int iData)
{
if (g_iNodeCount < 0)
return;
struct Node* pTemp = new struct Node;
if (NULL == pTemp)
return;
pTemp->iData = iData;
pTemp->pNext = NULL;
pTemp->pPre = NULL;
if (NULL == g_pHead)
{
g_pHead = pTemp;
}
else
{
g_pEnd->pNext = pTemp;
pTemp->pPre = g_pEnd;
}
g_pEnd = pTemp;
g_iNodeCount++;
}
主函数
#include"sx.h"
int main()
{
SX s;
s.AddToEnd(1);
s.AddToEnd(2);
s.AddToEnd(3);
s.LookZheng();
return 0;
}
|