C语言,用C语言实现一个异质链表的基础功能(创建插入删除等),求思路与开端。 5
structNode{void*pData;structNode*pNext;};这是要求定义异质链表的格式,但还是感觉无从下手。求一个基本的思路。...
struct Node{
void * pData;
struct Node * pNext;
};
这是要求定义异质链表的格式,但还是感觉无从下手。求一个基本的思路。 展开
void * pData;
struct Node * pNext;
};
这是要求定义异质链表的格式,但还是感觉无从下手。求一个基本的思路。 展开
1个回答
展开全部
struct Node
{
void * pData;
struct Node * pNext;
};
struct LinkedList
{
struct Node * pHead;
int count;
};
struct Node *constructNode(void *pData)
{
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
if (NULL == p) {
return NULL;
}
p->pData = pData;
p->pNext = NULL;
return p;
}
void destructNode(struct Node *pNode)
{
free(pNode->pData);
free(pNode);
}
struct Node *getLastNode(struct LinkedList * pList)
{
struct Node *p = pList->pHead;
if (NULL != p) {
while (p->pNext != NULL) {
p = p->pNext;
}
return p;
}
return NULL;
}
void initLinkedList(struct LinkedList * pList)
{
pList->pHead = NULL;
pList->count = 0;
}
int appendNode(struct LinkedList * pList, void *pData)
{
struct Node *pNewNode = constructNode(pData);
struct Node *pLastNode = getLastNode(pList);
if (NULL != pNewNode) {
if (NULL != pLastNode) {
pLastNode->pNext = pNewNode;
} else {
pList->pHead = pNewNode;
}
++pList->count;
return pList->count;
}
return -1;
}
{
void * pData;
struct Node * pNext;
};
struct LinkedList
{
struct Node * pHead;
int count;
};
struct Node *constructNode(void *pData)
{
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
if (NULL == p) {
return NULL;
}
p->pData = pData;
p->pNext = NULL;
return p;
}
void destructNode(struct Node *pNode)
{
free(pNode->pData);
free(pNode);
}
struct Node *getLastNode(struct LinkedList * pList)
{
struct Node *p = pList->pHead;
if (NULL != p) {
while (p->pNext != NULL) {
p = p->pNext;
}
return p;
}
return NULL;
}
void initLinkedList(struct LinkedList * pList)
{
pList->pHead = NULL;
pList->count = 0;
}
int appendNode(struct LinkedList * pList, void *pData)
{
struct Node *pNewNode = constructNode(pData);
struct Node *pLastNode = getLastNode(pList);
if (NULL != pNewNode) {
if (NULL != pLastNode) {
pLastNode->pNext = pNewNode;
} else {
pList->pHead = pNewNode;
}
++pList->count;
return pList->count;
}
return -1;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |