#include <iostream>
using namespace std;
#define NameLenth 20
#define OK 0
#define Error 1
typedef struct FlagNode
{
int number;
char name[NameLenth];
struct FlagNode *pNext;
}Node;
/***************************
*生成结点
***************************/
inline Node *NewNode()
{
Node *nodeUnit = new Node(0);
return nodeUnit;
}
/***************************
*销毁化链表
***************************/
void DestroyLink(Node *pHead)
{
if(NULL != pHead)
{
while(!pHead->pNext)
{
pHead->pNext = pHead->pNext->pNext;
delete pHead->pNext;
}
delete pHead;
}
}
/***************************
*初始化链表
***************************/
Node *InitLink(Node *pHead)
{
if(NULL != pHead)
{
DestroyLink(pHead);
}
return(NewNode());
}
/***************************
*向链表添加新结点
前插
***************************/
void AddNodeToLinkHead(Node *pHead,int CardNumber,char *Name)
{
if(!pHead || !Name)
{
return ;
}
Node *NewNodeAdd = NewNode();
NewNodeAdd->number = CardNumber;
memcpy(NewNodeAdd->name,Name,NameLenth*sizeof(char));
NewNodeAdd->pNext = pHead->pNext;
pHead->pNext = NewNodeAdd;
return;
}
/***************************
*计算链表长度
***************************/
int GetLenthLink(Node *pHead)
{
int Lenth = 0;
if(NULL != pHead)
{
while(NULL != pHead->pNext)
{
Lenth++;
pHead = pHead->pNext;
}
}
return Lenth;
}
/***************************
*插入结点
输入的不是下标,而是第几个,所以至少是1
***************************/
int InsertNode(Node *pHead,unsigned int location,int Number,char *pName)
{
int i = 0;
unsigned int lenth = 0;
if(NULL == pHead || NULL == pName)
{
return Error;
}
lenth = GetLenthLink(pHead);
if(lenth<location || location<1)
{
return Error;
}
Node *TmpNode = NewNode();
TmpNode->number = Number;
memcpy(TmpNode->name,pName,NameLenth*sizeof(char));
for(i=1;i<location;i++)
{
pHead = pHead->pNext;
}
TmpNode->pNext = pHead->pNext;
pHead->pNext = TmpNode;
return OK;
}
int main(void)
{
int CardNumber = 289384;
int Lenth = 0;
unsigned int LocationArr = 0;
char Name[NameLenth] = {"thank_you@qq.com"};
Node *HeadLink = NULL;
HeadLink = InitLink(HeadLink);
AddNodeToLinkHead(HeadLink,CardNumber,Name);
Lenth = GetLenthLink(HeadLink);
if(InsertNode(HeadLink,LocationArr,CardNumber,Name))
{
cout<<"插入失败!\n"<<endl;
}
else
{
cout<<"插入成功!\n"<<endl;
}
}
08-16
660
08-15
08-15
08-15