链式物理结构3
2016-07-28 14:19
357 查看
/*
链式存储结构练习
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
struct node *p_next;
} node;
static node head, tail;
int main() {
node *p_node = NULL, *p_tmp = NULL;
head.p_next = &tail;
//把一个新节点加入到链的末尾
for (p_node = &head;p_node != &tail;p_node = p_node->p_next) {
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid == &tail) {
p_tmp = (node *)malloc(sizeof(node));
if (p_tmp) {
p_tmp->num = 10;
p_tmp->p_next = NULL;
p_first->p_next = p_tmp;
p_tmp->p_next = p_mid;
}
break;
}
}
//把一个新节点加入到链的开头
p_tmp = (node *)malloc(sizeof(node));
if (p_tmp) {
p_tmp->num = 5;
p_tmp->p_next = NULL;
p_tmp->p_next = head.p_next;
head.p_next = p_tmp;
}
//打印链中所有数据
for (p_node = &head;p_node != &tail;p_node = p_node->p_next) {
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &tail) {
printf("%d ", p_mid->num);
}
}
printf("\n");
//释放所有动态分配节点
while (head.p_next != &tail) {
node *p_first = &head;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
}
return 0;
}
链式存储结构练习
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
struct node *p_next;
} node;
static node head, tail;
int main() {
node *p_node = NULL, *p_tmp = NULL;
head.p_next = &tail;
//把一个新节点加入到链的末尾
for (p_node = &head;p_node != &tail;p_node = p_node->p_next) {
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid == &tail) {
p_tmp = (node *)malloc(sizeof(node));
if (p_tmp) {
p_tmp->num = 10;
p_tmp->p_next = NULL;
p_first->p_next = p_tmp;
p_tmp->p_next = p_mid;
}
break;
}
}
//把一个新节点加入到链的开头
p_tmp = (node *)malloc(sizeof(node));
if (p_tmp) {
p_tmp->num = 5;
p_tmp->p_next = NULL;
p_tmp->p_next = head.p_next;
head.p_next = p_tmp;
}
//打印链中所有数据
for (p_node = &head;p_node != &tail;p_node = p_node->p_next) {
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &tail) {
printf("%d ", p_mid->num);
}
}
printf("\n");
//释放所有动态分配节点
while (head.p_next != &tail) {
node *p_first = &head;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
}
return 0;
}
相关文章推荐
- google编程规范
- Android开发——Activity启动模式详解
- 常用的Meta标签写法和作用
- Oracle数据库函数使用
- 【Web容器】Tomcat源码分析(1)-类加载体系
- apache 集群tomcat配置参数说明
- Python 正则表达式 (附调试工具)
- Gradle打包的war文件部署到tomcat后运行解压出错的问题
- MYSQL语句大全
- 对孩子 七 不责
- Sphinx和Coreseek安装配置
- linux基础——linux进程与线程的区别与联系
- 专访李智慧:架构是最高层次的规划和难以改变的决定
- python学习笔记系列----(一)python简介
- 百度SiteApp
- jsp技术:day1--jsp简单用法、静态导入、动态导入、设置errorPage、<jsp:forward>
- 友盟崩溃日志分享,dsym工具
- Android studio快捷键大全+Android studio使用小技巧
- Linux中Apache+Tomcat+JK实现负载均衡和群集的完整过程 .
- git系列------git revert