c语言双向循环链表尾插法详细,C实现头插法和尾插法来构建双向非循环链表(带头结点尾结点)...

双向链表中若是有了头结点和尾结点,对于头插法和尾插法就显得很是方便。这样在尾部插入一个元素也就不用去遍历链表了。我的建议使用这种链表来处理问题。代码上传至  https://github.com/chenyufeng1991/HeadInsertAndTailInsert_DoubleList_HeadList 。git

核心代码以下:github

//建立带头结点和尾结点的双向非循环链表(头插法)

void HeadInsertCreateList(Node *pHead,Node *pTail){

Node *pInsert;

pInsert = (Node *)malloc(sizeof(Node));

memset(pInsert, 0, sizeof(Node));

pInsert->prior = NULL;

pInsert->next = NULL;

scanf("%d",&(pInsert->element));

while (pInsert->element > 0) {

pHead->next->prior = pInsert;

pInsert->next = pHead->next;

pInsert->prior = pHead;

pHead->next = pInsert;

pInsert = (Node *)malloc(sizeof(Node));

memset(pInsert, 0, sizeof(Node));

pInsert->prior = NULL;

pInsert->next = NULL;

scanf("%d",&(pInsert->element));

}

printf("%s函数执行完成,头插法创建带头节点和尾结点的双向非循环链表建立成功\n",__FUNCTION__);

}

//建立带头结点和尾结点的双向非循环链表(尾插法)

void TailInsertCreateList(Node *pHead,Node *pTail){

Node *pInsert;

pInsert = (Node *)malloc(sizeof(Node));

memset(pInsert, 0, sizeof(Node));

pInsert->prior = NULL;

pInsert->next = NULL;

scanf("%d",&(pInsert->element));

while (pInsert->element > 0) {

pTail->prior->next = pInsert;

pInsert->prior = pTail->prior;

pInsert->next = pTail;

pTail->prior = pInsert;

pInsert = (Node *)malloc(sizeof(Node));

memset(pInsert, 0, sizeof(Node));

pInsert->prior = NULL;

pInsert->next = NULL;

scanf("%d",&(pInsert->element));

}

printf("%s函数执行完成,尾插法创建带头节点和尾结点的双向非循环链表建立成功\n",__FUNCTION__);

}

测试代码以下:

int main(int argc, const char * argv[]) {

Node *pHead;//头结点

Node *pTail;//尾结点

InitialList(&pHead, &pTail);

HeadInsertCreateList(pHead, pTail);

PrintList(pHead, pTail);

TailInsertCreateList(pHead, pTail);

PrintList(pHead, pTail);

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值