//
// main.c
// LineList
//
// Created by Rubert on 16/9/11.
// Copyright © 2016年 Study. All rights reserved.
// #include <stdio.h>
//定义链表数据结构
struct node
{
int num;
struct node *next;
struct node *prior; };
//函数声明
struct node *create();
void print();
void linkedList();
void headInsertLinkedList();
void tailInsertLinkedList();
void findLinkedList();
void insertLinkedList();
void deleteLinkedList();
void circleInsertLinkedList();
void doubleInsertLinkedList();
int main(int argc, const char * argv[]) {
// insert code here...
//headInsertLinkedList();
//tailInsertLinkedList();
//findLinkedList();
//insertLinkedList();
//deleteLinkedList();
//circleInsertLinkedList();
doubleInsertLinkedList();
return ;
} /*
* 头插入法创建链表
*/
void headInsertLinkedList() {
struct node *head = NULL;
struct node *p1;
for(int i = ; i < ; i ++) {
p1 = (struct node*)malloc(sizeof(struct node));
p1 -> num = i;
p1 -> next = head;
head = p1;
}
print(head);
} /*
* 尾插入法创建链表
*/
void tailInsertLinkedList() {
struct node *head = NULL;
struct node *p1,*p2;
p1 = p2 = (struct node*)malloc(sizeof(struct node));
for(int i = ; i < ; i ++) {
if(head == NULL) {
head = p1;
} else {
p2 -> next = p1;
}
p2 = p1;
p1 = (struct node*)malloc(sizeof(struct node));
p1->num = i;
}
print(head);
} /**
* 创建循环链表
*/
void circleInsertLinkedList() {
struct node *head = NULL;
struct node *p1,*p2;
p1 = p2 = (struct node*)malloc(sizeof(struct node));
for(int i = ; i < ; i ++) {
if(head == NULL) {
head = p1;
} else { if(i == ) {
p1->next = head;
}
p2 -> next = p1;
}
p2 = p1;
p1 = (struct node*)malloc(sizeof(struct node));
p1->num = i; }
print(head);
} /**
* 创建双向链表
*/
void doubleInsertLinkedList() {
struct node *head = NULL;
struct node *p1,*p2;
p1 = p2 = (struct node*)malloc(sizeof(struct node));
for(int i = ; i < ; i ++) {
if(head == NULL) {
head = p1;
} else {
/*if(i == 9) {
p1->next = head;
}*/
p2 -> next = p1;
p1 -> prior = p2;
}
p2 = p1;
p1 = (struct node*)malloc(sizeof(struct node));
p1->num = i; }
print(head);
} /*
* 链表查找
*/
void findLinkedList() { struct node *head = NULL;
struct node *p1;
for(int i = ; i < ; i ++) {
p1 = (struct node*)malloc(sizeof(struct node));
p1 -> num = i;
p1 -> next = head;
head = p1;
} struct node *p;
p = head -> next;
int j = ;
int i = ;
while(p !=NULL && j < i)
{
p = p->next;
++j;
}
printf("%6d %d %d\n",p->num, p, p->next);/*输出链表节点的值*/
} /*
* 链表中插入
*/ void insertLinkedList() { struct node *head = NULL;
struct node *p1;
for(int i = ; i < ; i ++) {
p1 = (struct node*)malloc(sizeof(struct node));
p1 -> num = i;
p1 -> next = head;
head = p1;
} struct node *p,*m;
p = head;
int j = ;
int i = ;
while(p !=NULL && j < i-)
{
p = p->next;
++j;
} if(p == NULL) {
printf("error");
} else {
m = (struct node*)malloc(sizeof(struct node));
m->num = j;
m->next = p -> next;
p->next = m;
} print(head); } /*
* 链表中删除
*/ void deleteLinkedList() { struct node *head = NULL;
struct node *p1;
for(int i = ; i < ; i ++) {
p1 = (struct node*)malloc(sizeof(struct node));
p1 -> num = i;
p1 -> next = head;
head = p1;
} struct node *p,*m;
p = head;
int j = ;
int i = ;
while(p !=NULL && j < i-)
{
p = p->next;
++j;
} if(p == NULL) {
printf("error");
} else {
//m = (struct node*)malloc(sizeof(struct node));
//m->num = j;
//m->next = p -> next;
p->next = p->next->next; } print(head); } /*
* sample 1
*/
void linkedList()
{
struct node *head;
head = NULL;//创建一个空表
head = create(head); /* 创建单链表 */
print(head);/* 打印单链表 */ } struct node *create(struct node *head) //返回的是与节点相同类型的指针
{
struct node *p1,*p2;
int i = ;
//利用malloc() 函数向系统申请分配一个节点
p1 = p2 = (struct node*)malloc(sizeof(struct node));//新节点
printf("请输入值,值小于等于0结束,值存放地址为:p1_ADDR= %d\n",p1);
scanf("%d",&p1->num);/*输入节点的值*/
p1->next=NULL;/*将新节点的指针置为空*/
while(p1->num > )/*输入节点的数值大于0*/
{
//④将新节点的指针成员赋值为空。若是空表,将新节点连接到表头;若是非空表,将新节点接到表尾;
if(head == NULL)
head = p1;/*空表,接入表头*/
else
p2->next = p1;/*非空表,接到表尾*/
p2 = p1; p1 = (struct node*)malloc(sizeof(struct node));/*下一个新节点*/
i = i+;
printf("请输入值,值小于等于0结束,值存放地址为:p%d_ADDR= %d\n",i,p2);
scanf("%d",&p1->num);/*输入节点的值*/
//⑤判断一下是否有后续节点要接入链表,若有转到3 ),否则结束;
}
//==============原来程序更正部分:(多谢@daling_datou提醒)================================
//free(p1); //申请到的没录入,所以释放掉
p1 = NULL; //使指向空
p2->next = NULL; //到表尾了,指向空
printf("链表输入结束(END)\n");
//==============================================
return head;
} void print(struct node *head)/*出以head为头的链表各节点的值*/
{
struct node *temp;
temp = head;/*取得链表的头指针*/ printf("\n\n\n链表存入的值为:\n");
while(temp != NULL)/*只要是非空表*/
{
printf("%6d %d %d %d\n",temp->num, temp, temp->next, temp->prior);/*输出链表节点的值*/
temp = temp->next;/*跟踪链表增长*/
}
printf("链表打印结束!!");
}

some code of c的更多相关文章

  1. Visual Studio Code 代理设置

    Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...

  2. 我们是怎么做Code Review的

    前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...

  3. Code Review 程序员的寄望与哀伤

    一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...

  4. 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM

    刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...

  5. 在Visual Studio Code中配置GO开发环境

    一.GO语言安装 详情查看:GO语言下载.安装.配置 二.GoLang插件介绍 对于Visual Studio Code开发工具,有一款优秀的GoLang插件,它的主页为:https://github ...

  6. 代码的坏味道(14)——重复代码(Duplicate Code)

    坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...

  7. http status code

    属于转载 http status code:200:成功,服务器已成功处理了请求,通常这表示服务器提供了请求的网页 404:未找到,服务器未找到 201-206都表示服务器成功处理了请求的状态代码,说 ...

  8. Visual Studio Code——Angular2 Hello World 之 2.0

    最近看到一篇用Visual Studio Code开发Angular2的文章,也是一篇入门教程,地址为:使用Visual Studio Code開發Angular 2專案.这里按部就班的做了一遍,感觉 ...

  9. WebStorm 2016 最新版激活(activation code方式)

    WebStorm 2016 最新版激活(activation code方式) WebStorm activation code WebStorm 最新版本激活方式: 今天下载最新版本的WebStorm ...

  10. docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用

    .net猿遇到了小鲸鱼,觉得越来越兴奋.本来.net猿只是在透过家里那田子窗看外面的世界,但是看着海峡对岸的苹果园越来越茂盛,实在不想再去做一只宅猿了.于是,.net猿决定搭上小鲸鱼的渡轮到苹果园去看 ...

随机推荐

  1. Mac下搭建php开发环境【转】

    Mac OS X 内置了Apache 和 PHP,这样使用起来非常方便.本文以Mac OS X 10.6.3为例.主要内容包括: 启动Apache 运行PHP 安装MySQL 使用phpMyAdmin ...

  2. LayaAir引擎——(九)

    var h = new Array(); var j = new Array(); var xbCursor = 0; function xbinit() { xbinitName(); xbRect ...

  3. css基本设置

    css引入方式: 头部引入:在HTML页面头部编写程序(一般用在访问量大的网页上) 标签内引入:在标签内直接写样式(优先级最高,但难于维护,会沉积大量代码) 外部引入:用link标签引入(常用 ,维护 ...

  4. 第一章Android系统移植与驱动开发概述--读书笔记

    以前,初步学习过嵌入式Linux驱动开发的基础课程,对于驱动开发可以说是有了一点点微末的基础吧.首先我们要对Android嵌入式系统有一个初步的认识,Android系统发展到今天已经具备了完善的架构. ...

  5. JAVA Web day01--- Android小白的第一天学习笔记

    HTML 1.HTML的概述 1.1.HTML简介 l HTML(Hyper Text Markup Language):超文本标记语言. >标记就是标签 >HTML不是一种编程语言,而是 ...

  6. SQL---Chapter01 数据库和SQL

    数据库类型: 层次数据库(Hierarchical Database, HDB) 数据通过层次结构(树形结构)的方式表示出来. 关系型数据库(Relational Database, RDB) 使用专 ...

  7. Chp10 10.7

    <Java语言程序设计>P296 本章是关于对象的思考,主要是在研究面向对象的程序设计时类的设计,作业写得比较杂乱,构造方法时没有严格遵守类的流行设计风格,由于是作业,再加上比较简单,没有 ...

  8. eclipse导入项目前面有感叹号

    1.项目上右击---build path---Config..----Libra----

  9. javascript数组 去重

    数组去重的方法有很多,到底哪种是最理想的,自己不清楚.于是自己测试了下数组去重的效果和性能.测试十万个数据,代码和所耗大概时间如下. 到底采用哪种方法,根据实际情况而定吧. /*方法一: 1,'1' ...

  10. 新建STM32工程

    1) 2)保存 3)选择公司和芯片的型号,STM32F103C8T6,64kB Flash, 20kB SRAM. 4)手动添加启动代码 5)新建如下文件夹 6)回到工程,选中target,右键Add ...