typedef struct LNode
{
int nData;
struct LNode* pNext;
}LNode, *LinkList;
// output error
void MyError(void)
{
std::cout << "error" << std::endl;
}
// create List
bool CreateList(LinkList& pList)
{
pList = (LinkList)malloc(sizeof(LNode));
if (NULL == pList)
{
return false;
}
pList->nData = 0;
pList->pNext = NULL;
return true;
}
// insert element
bool InsertElement(LinkList& pList, int nPos, int nElement)
{
LinkList pListTemp = pList;
int nIndex = 0;
while (NULL != pListTemp && nIndex < nPos)
{
++nIndex;
pListTemp = pListTemp->pNext;
}
LinkList pNode = (LinkList)malloc(sizeof(LNode));
if (NULL == pNode)
{
return false;
}
pNode->nData = nElement;
pNode->pNext = pListTemp->pNext;
pListTemp->pNext = pNode;
++pList->nData;
return true;
}
// delete element
bool DeleteElement(LinkList& pList, int nPos)
{
LinkList pListTemp = pList;
int nIndex = 0;
while (NULL != pListTemp->pNext && nIndex < nPos - 1)
{
pListTemp = pListTemp->pNext;
++nIndex;
}
if (NULL == pListTemp->pNext || nIndex > nPos - 1)
{
return false;
}
LinkList pNode = pListTemp->pNext;
pListTemp->pNext = pListTemp->pNext->pNext;
--pList->nData;
free(pNode);
return true;
}
// Edit element
void EditElement(LinkList& pList, int nPos, int nElement)
{
LinkList pListTemp = pList;
int nIndex = 0;
while (NULL != pListTemp->pNext && nIndex < nPos)
{
pListTemp = pListTemp->pNext;
++nIndex;
}
pListTemp->nData = nElement;
}
// Output list
void OutPutElements(const LinkList& pList)
{
LinkList pListTemp = pList->pNext;
while (NULL != pListTemp)
{
std::cout << pListTemp->nData << std::endl;
pListTemp = pListTemp->pNext;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
LinkList pList;
if (!CreateList(pList))
{
MyError();
}
for (int i = 0; i < 10; ++i)
{
if (!InsertElement(pList, pList->nData, i))
{
MyError();
}
}
if (!DeleteElement(pList, 2))
{
MyError();
}
if (!DeleteElement(pList, 1))
{
MyError();
}
EditElement(pList, 5, 0);
OutPutElements(pList);
return 0;
}
{
int nData;
struct LNode* pNext;
}LNode, *LinkList;
// output error
void MyError(void)
{
std::cout << "error" << std::endl;
}
// create List
bool CreateList(LinkList& pList)
{
pList = (LinkList)malloc(sizeof(LNode));
if (NULL == pList)
{
return false;
}
pList->nData = 0;
pList->pNext = NULL;
return true;
}
// insert element
bool InsertElement(LinkList& pList, int nPos, int nElement)
{
LinkList pListTemp = pList;
int nIndex = 0;
while (NULL != pListTemp && nIndex < nPos)
{
++nIndex;
pListTemp = pListTemp->pNext;
}
LinkList pNode = (LinkList)malloc(sizeof(LNode));
if (NULL == pNode)
{
return false;
}
pNode->nData = nElement;
pNode->pNext = pListTemp->pNext;
pListTemp->pNext = pNode;
++pList->nData;
return true;
}
// delete element
bool DeleteElement(LinkList& pList, int nPos)
{
LinkList pListTemp = pList;
int nIndex = 0;
while (NULL != pListTemp->pNext && nIndex < nPos - 1)
{
pListTemp = pListTemp->pNext;
++nIndex;
}
if (NULL == pListTemp->pNext || nIndex > nPos - 1)
{
return false;
}
LinkList pNode = pListTemp->pNext;
pListTemp->pNext = pListTemp->pNext->pNext;
--pList->nData;
free(pNode);
return true;
}
// Edit element
void EditElement(LinkList& pList, int nPos, int nElement)
{
LinkList pListTemp = pList;
int nIndex = 0;
while (NULL != pListTemp->pNext && nIndex < nPos)
{
pListTemp = pListTemp->pNext;
++nIndex;
}
pListTemp->nData = nElement;
}
// Output list
void OutPutElements(const LinkList& pList)
{
LinkList pListTemp = pList->pNext;
while (NULL != pListTemp)
{
std::cout << pListTemp->nData << std::endl;
pListTemp = pListTemp->pNext;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
LinkList pList;
if (!CreateList(pList))
{
MyError();
}
for (int i = 0; i < 10; ++i)
{
if (!InsertElement(pList, pList->nData, i))
{
MyError();
}
}
if (!DeleteElement(pList, 2))
{
MyError();
}
if (!DeleteElement(pList, 1))
{
MyError();
}
EditElement(pList, 5, 0);
OutPutElements(pList);
return 0;
}
1348

被折叠的 条评论
为什么被折叠?



