C实现头插法和尾插法来构建单链表(不带头结点)
2024-10-19 21:36:19
链表的构建事实上也就是不断插入节点的过程。而节点的插入能够分为头插法和尾插法。
头插法就是在头结点后插入该节点,始终把该节点作为第一个节点。尾插法就是在链表的最后一个节点处插入元素,作为最后一个节点。假设想要了解链表的概念和其它链表操作。请參考《数据结构与算法之链表》《C语言实现链表的基本操作》两篇文章。演示样例代码上传至 https://github.com/chenyufeng1991/HeadInsertAndTailInsert 。
//
// main.c
// HeadInsertAndTailInsert
//
// Created by chenyufeng on 16/2/25.
// Copyright © 2016年 chenyufengweb. All rights reserved.
// /**
* 分别使用头插法和尾插法建立单链表
*/ #include <stdio.h>
#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;
}
最新文章
- iOS chart 图表完美解决方案 基于swift
- Access数据库多表连接查询
- java解析xml文档(dom)
- 2016年12月24日 星期六 --出埃及记 Exodus 21:19
- SpringMVC核心——映射问题
- jquery实现轮播
- Thread Join()的用法
- office2010安装出错,windows installer服务不能更新一个或多个受保护的windows文件
- Retrofit网络请求库应用01
- (Release Candidate)Candidate
- PyCharm链接服务器同步代码
- How far away ? HDU - 2586 【LCA】【RMQ】【java】
- ffmpeg奇数分辨率转码失败
- Docker 镜像上传到docker hub仓库
- Java环境编写
- 基于序列化技术(Protobuf)的socket文件传输
- Unable to instantiate application com.txrj.sms.activity.TxrjApplication
- dart --- 更符合程序员编程习惯的javascript替代者
- php 开源项目汇总
- Linux系统下搭建FTP/SFTP服务器
热门文章
- tomcat创建用户
- 201621123079《java程序设计》第六周作业总结
- git 项目相关
- mac下出现xcrun: error导致git无法使用的解决办法
- JustinMind
- sprintboot + mybaits + mysql + html5 + thymeleaf 个人笔记
- python007 Python3 数字(Number)
- tarjan求割边割点
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找
- irules事件和命令