some code of c
//
// 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的更多相关文章
- Visual Studio Code 代理设置
Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...
- 我们是怎么做Code Review的
前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...
- Code Review 程序员的寄望与哀伤
一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...
- 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM
刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...
- 在Visual Studio Code中配置GO开发环境
一.GO语言安装 详情查看:GO语言下载.安装.配置 二.GoLang插件介绍 对于Visual Studio Code开发工具,有一款优秀的GoLang插件,它的主页为:https://github ...
- 代码的坏味道(14)——重复代码(Duplicate Code)
坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...
- http status code
属于转载 http status code:200:成功,服务器已成功处理了请求,通常这表示服务器提供了请求的网页 404:未找到,服务器未找到 201-206都表示服务器成功处理了请求的状态代码,说 ...
- Visual Studio Code——Angular2 Hello World 之 2.0
最近看到一篇用Visual Studio Code开发Angular2的文章,也是一篇入门教程,地址为:使用Visual Studio Code開發Angular 2專案.这里按部就班的做了一遍,感觉 ...
- WebStorm 2016 最新版激活(activation code方式)
WebStorm 2016 最新版激活(activation code方式) WebStorm activation code WebStorm 最新版本激活方式: 今天下载最新版本的WebStorm ...
- docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用
.net猿遇到了小鲸鱼,觉得越来越兴奋.本来.net猿只是在透过家里那田子窗看外面的世界,但是看着海峡对岸的苹果园越来越茂盛,实在不想再去做一只宅猿了.于是,.net猿决定搭上小鲸鱼的渡轮到苹果园去看 ...
随机推荐
- ThinkPHP M函数疑点
模型类的命名规则是除去表前缀的数据表名称,采用驼峰法命名,并且首字母大写,然后加上模型层的名称(默认定义是Model),例如: 模型名 约定对应数据表(假设数据库的前缀定义是 think_) User ...
- XML代码生成器——XMLFACTORY 简介(三)
XML代码生成器——XMLFACTORY 简介(三) 这一篇我们讲“类名称”页签 的配置功能,您将了解到:如何为Xml元素指定对应的类名称及脱壳功能. 如果,你没看过这个系列的第一篇文章,请先去看这篇 ...
- LESS用法·
CSS 彻底改变了 Web 页面的设计,但 CSS 仍然是静态的,而且在其句法发展方面受到限制.这些限制是有目的且合乎情理的,鼓励广泛加以实现.但开发人员和设计人员常常发现 CSS 使用起来很单调乏味 ...
- 快速上手RaphaelJS--Instant RaphaelJS Starter翻译(三)
(目前发现一些文章被盗用的情况,我们将在每篇文章前面添加原文地址,本文源地址:http://www.cnblogs.com/idealer3d/p/Instant_RaphaelJS_Starter3 ...
- jQuery method and examples
一:介绍: jQuery:是DOM和js的封装.jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).现在大多数的pc端的网站都 ...
- 【海洋女神原创】知识普及:IS版本命名规则和高低关系
经常有朋友对IS的版本命名不甚了解,有时候在交流的时候就会造成误会,在这里做一下普及. IS最早出名的版本是IS6.22,这是个非常古老的版本的,但是在IS历史上有不可磨灭的贡献. 之后很长一段时间内 ...
- parsec-2.1 编译错误
OS: Ubuntu 14.04 LTS (x86_64) 参考:https://forums.freebsd.org/threads/security-openssl-build-failure.4 ...
- 自适应布局,响应式布局以及rem,em区别
一.自适应和响应式 先说共同点: 两者都是因为越来越多的 移动设备( mobile, tablet device )加入到互联网中来而出现的为移动设备提供更好的体验的技术.用技术来使网页适应从小到大( ...
- 简单的 JSON 对象进行深拷贝最简单的方法
var json = { a: 123, b: '456' }; var json2 = JSON.parse(JSON.stringify(json)); 只需要先使用 JSON.stringify ...
- 【P1915】[usaco09 dec gold]电视游戏问题
在百度上搜到了nzx学长的题解orz 原题: 农夫约翰的奶牛们游戏成瘾!本来FJ是想要按照陶叫兽的做法拿她们去电击戒瘾的,可是后来他发现奶牛们玩游戏之后比原先产更多的奶.很明显,这是因为满足的牛会产更 ...