java单链表尾插法_C实现头插法和尾插法来构建单链表(不带头结点)

链表的构建事实上也就是不断插入节点的过程。而节点的插入能够分为头插法和尾插法。

头插法就是在头结点后插入该节点,始终把该节点作为第一个节点。尾插法就是在链表的最后一个节点处插入元素,作为最后一个节点。假设想要了解链表的概念和其它链表操作。请參考《数据结构与算法之链表》《C语言实现链表的基本操作》两篇文章。演示样例代码上传至  https://github.com/chenyufeng1991/HeadInsertAndTailInsert 。

//

// main.c

// HeadInsertAndTailInsert

//

// Created by chenyufeng on 16/2/25.

// Copyright © 2016年 chenyufengweb. All rights reserved.

//

/**

* 分别使用头插法和尾插法建立单链表

*/

#include

#include "stdlib.h"

#include "string.h"

typedef int elemType;

//构造节点

typedef struct ListNode{

int element;

struct ListNode *next;

}Node;

//初始化链表

void initList(Node *pNode){

pNode = NULL;

printf("%s函数运行,头结点初始化完毕\n",__FUNCTION__);

}

//打印链表

void printList(Node *pNode){

if (pNode == NULL) {

printf("%s函数运行,链表为空,打印失败\n",__FUNCTION__);

}else{

while (pNode != NULL) {

printf("%d ",pNode->element);

pNode = pNode->next;

}

printf("\n");

}

}

//头插法

Node *HeadInsert(Node *pNode){

Node *pInsert;

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

if (pInsert == NULL) {

printf("%s函数运行。内存分配失败,建立链表失败\n",__FUNCTION__);

return NULL;

}

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

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

pInsert->next = NULL;

if (pInsert->element <= 0) {

printf("%s函数运行。输入数据有误,建立链表失败\n",__FUNCTION__);

return NULL;

}

while (pInsert->element > 0) {

if (pNode == NULL) {

pNode = pInsert;

}else{

//注意以下语句的顺序,否则可能造成链断裂

pInsert->next = pNode;

pNode = pInsert;

}

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

if (pInsert == NULL) {

printf("%s函数运行,内存分配失败,建立链表失败\n",__FUNCTION__);

return NULL;

}

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

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

pInsert->next = NULL;

}

printf("%s函数运行。头插法建立链表成功\n",__FUNCTION__);

return pNode;

}

//尾插法

Node *TailInsert(Node *pNode){

Node *pInsert; //要插入的节点

Node *pMove; //遍历链表的节点

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

if (pInsert == NULL) {

printf("%s函数运行,内存分配失败,建立链表失败\n",__FUNCTION__);

return NULL;

}

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

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

pInsert->next = NULL;

if (pInsert->element <= 0) {

printf("%s函数运行。输入数据有误,建立链表失败\n",__FUNCTION__);

return NULL;

}

pMove = pNode;

while (pInsert->element > 0) {

if (pNode == NULL) {

//注意不要忘了改动pMove指针的指向,初始pMove一定要指向头节点

pNode = pInsert;

pMove = pNode;

}else{

//遍历找到最后一个节点

while (pMove->next != NULL) {

pMove = pMove->next;

}

pMove->next = pInsert;

}

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

if (pInsert == NULL) {

printf("%s函数运行。内存分配失败,建立链表失败\n",__FUNCTION__);

return NULL;

}

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

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

pInsert->next = NULL;

}

printf("%s函数运行,尾插法建立链表成功\n",__FUNCTION__);

return pNode;

}

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

Node *pList;

initList(pList);

printList(pList);

//头插法建立链表

pList = HeadInsert(pList);

printList(pList);

//尾插法建立链表

pList = TailInsert(pList);

printList(pList);

return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值