单链表之头插法
来源:互联网 发布:java base64库 编辑:程序博客网 时间:2024/06/08 18:05
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象)
指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
头插法
#include <stdio.h>#include <stdlib.h>//单链表 :// head --> 信息域(节点内容)|指针域 (指向下一节点)--> 信息域(节点内容)|指针域 (指向下一节点)--> ...-->NULL//头插法 struct Book{ char title[128]; char author[40]; struct Book *next;};void getInput(struct Book *book){ scanf("%s", book -> title); scanf("%s", book -> author);}void addBook(struct Book **library){ struct Book *book, *tmp; book = (struct Book *) malloc(sizeof(struct Book)); if(book == NULL) exit(0); getInput(book); if(*library != NULL) { tmp = *library; *library = book; book -> next = tmp; } else { //空节点 *library = book; book -> next = NULL; }}void printLibrary(struct Book *library){ struct Book *book; int count = 1; book = library; while(book != NULL) { printf("\n"); printf("第%d本书:\n", count); printf("书名:%s\n", book -> title); printf("作者:%s\n", book -> author); printf("---------\n"); book = book -> next; count++; }} void releaseLibrary(struct Book **library)//释放内存 { struct Book *tmp; while(*library != NULL){ tmp = *library; *library = (*library) -> next; free(tmp); }}int main(){ struct Book *library = NULL; //head addBook(&library); addBook(&library); //addBook(&library); printLibrary(library); releaseLibrary(&library); return 0;}
**输入:**AAABBBCCCDDD**输出:**第1本书:书名:CCC作者:CCC---------第2本书:书名:AAA作者:AAA---------
图解
———————————
图解:
阅读全文
0 0
- 单链表之头插法
- 数据结构之单链表(头插法)
- 数据结构 -- 单链表创建之头插法和尾插法
- 待字闺中之单链表和之恋
- C语言之单链表操作之查找
- C语言之单链表操作之插入
- 数据结构单链表之创建链表(头插法)
- 数据结构学习之单链表
- java数据结构之单链表
- 数据结构之单链表
- 数据结构学习之单链表
- 数据结构之单链表反转
- 数据结构之单链表
- 数据结构之, 单链表
- 类模板之单链表
- 数据结构之单链表
- 线性表之单链表
- 面试题之单链表
- java怎么调用远程服务器中的Shell脚本 一
- jsp 静态引入<%@ include %> 动态引入<jsp:include> 区别
- eclipse tomcat加载项目超时一直无法加载完成,卡在 preparing launch delegate 100% 的解决方法
- index的注释和理解之HTML-body部分
- Mybatis
- 单链表之头插法
- 查看CentOS版本,查看CUDA版本
- 【基础中心】笔记三--oracle连接远程数据库
- The tip point of programming
- iOS探索--点语法和对象初始化
- 让机器说话(文字转美女语音,擅长中英文哦),大小600K(免费下载)!
- java怎么调用远程服务器上的shell脚本 二参数调用远程
- java系统高并发解决方案
- hdu 1101 Constructing Roads 基础最小生成树★