struct MyList
{
int nA;
MyList* pTnext;
MyList* pTpre;
};
MyList* CreateList()
{
MyList* pHead = NULL;
MyList* pPre = NULL;
MyList* pNext = NULL;
MyList* pCur = NULL;
int nA = 0;
printf("请输入您的数字");
for (int i = 0;i < 5;i++)
{
scanf_s("%d", &nA);
pCur = new MyList;
pCur->pTpre = pPre;
pCur->pTnext = NULL;
if (pCur != nullptr)
{
pCur->nA = nA;
}
if (pPre == NULL)
{
pHead = pCur;
}
else
{
pPre->pTnext = pCur;
}
pPre = pCur;
}
return pHead;
}
bool FindList(int nValue, MyList* pFindHead)
{
MyList* pCur = pFindHead;
if (pCur != NULL)
{
if (pCur->nA == nValue)
{
return true;
printf("恭喜您找到该结点");
}
else
{
return false;
printf("很遗憾,此链表没有您要找的结点");
}
}
}
MyList* DeleteList(int nDelete, const MyList* pDelete)
{
MyList* pCur = (MyList*)pDelete;
MyList* pSHend =(MyList*)pDelete;
MyList* pPre = NULL;
while (pCur != NULL)
{
if (pCur->nA == nDelete)
{
if (pPre == NULL)//头结点
{
pPre = pCur->pTnext;//让当前的下一个结点当头结点
pSHend = pCur->pTnext;
}
else
{
pPre->pTnext = pCur->pTnext;//当前结点的前一个的下一个为当前结点的下一个
pCur->pTnext->pTpre = pPre;
}
delete pCur;//删除结点
pCur = pPre;//前一个结点赋值给当前
}
pPre = pCur;
pCur = pCur->pTnext;
}
return pSHend;
}
MyList* AddList(int nValue, MyList* AddList, int nAdd)
{
MyList* pCur = AddList;
MyList* pAdd = NULL;
MyList* pPre = NULL;
while (pCur != NULL)
{
if (pCur->nA == nValue)
{
pAdd = new MyList;
pAdd->pTnext = NULL;
pAdd->nA = nAdd;
//结点连接
pCur->pTnext->pTpre = pAdd;
pAdd->pTnext = pCur->pTnext;//padd的下一个
pCur->pTnext = pAdd;
pAdd->pTpre = pCur;
}
pCur = pCur->pTnext;
}
return AddList;
}
MyList* UpsetList(int nValue,MyList* pUpsetHead,int nUpset)
{
MyList* pCur = pUpsetHead;
MyList* pPre = NULL;
MyList* pUpset = NULL;
while (pCur != nullptr)
{
if (pCur->nA == nValue)
{
pCur->nA = nUpset;
}
pCur = pCur->pTnext;
}
return pUpsetHead;
}
int main()
{
int nA = 123;
int nB = 888;
int nC = 234;
int nD = 555;
MyList* pList = CreateList();//测试过了pList值是有的
MyList* pDlete = DeleteList(nA, pList);
MyList* pAdd = AddList(nA, pList, nC);
MyList* pAddList = UpsetList(nA, pList,nC);
while (AddList != NULL)
{
printf("%d\n", pAddList->nA);
pAddList = pAddList->pTnext;
}
std::cout << "Hello World!\n";
}