本帖最后由 古月不傲 于 2020-2-23 01:10 编辑
[C] 纯文本查看 复制代码 #include <iostream>
using namespace std;
typedef struct _List
{
struct _List *pNext;
int nData;
}List, *pList;
pList CreateHeader()
{
pList plist = new List;
memset(plist, 0, sizeof(List));
plist->pNext = NULL;
plist->nData = 0;
return plist;
}
void InsertEnd(pList *plist, int nData)
{
pList pNew = new List;
memset(pNew, 0, sizeof(List));
pList pTemp = *plist;
if ((*plist)->pNext == NULL)
{
(*plist)->pNext = pNew;
pNew->pNext = NULL;
pNew->nData = nData;
return;
}
while (pTemp->pNext != NULL)
{
pTemp = pTemp->pNext;
}
pTemp->pNext = pNew;
pNew->pNext = NULL;
pNew->nData = nData;
}
void InsertAny(pList *plist, int nData, int nPos)
{
pList pTemp = *plist;
int i = 0;
while (pTemp->pNext != NULL && i <= nPos - 1)
{
pTemp = pTemp->pNext;
i++;
}
pList pNew = new List;
memset(pNew, 0, sizeof(List));
pNew->pNext = pTemp->pNext;
pTemp->pNext = pNew;
pNew->nData = nData;
}
void Print(pList *plist)
{
pList pTemp = *plist;
while (pTemp->pNext != NULL)
{
printf("地址:%x 值%d\n", pTemp->pNext, pTemp->pNext->nData);
pTemp = pTemp->pNext;
}
}
int main(void)
{
pList plist;
plist = CreateHeader();
InsertEnd(&plist, 2);
InsertEnd(&plist, 4);
InsertEnd(&plist, 6);
InsertEnd(&plist, 7);
InsertEnd(&plist, 10);
InsertEnd(&plist, 3);
Print(&plist);
system("cls");
InsertAny(&plist, 5, 2);
Print(&plist);
system("pause");
return 0;
}
|