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猿决定搭上小鲸鱼的渡轮到苹果园去看 ...
随机推荐
- MySQL数据库权限操作指南
-- 创建用户 CREATE USER 'dongrichtest' IDENTIFIED BY 'dongrichtest'; -- 新增后删除需要刷新权限 FLUSH PRIVILEGES; -- ...
- Meta http-equiv的属性详解 来自wanglehui
Meta http-equiv 语法标签格式:<meta http-equiv="参数" content="参数值"> 1."过期时间&q ...
- myeclipse10中文注释乱码问题
将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclipse编码格式不同. 总结网上的建议和自己的体 ...
- SQL Server COM 组件创建实例失败
SQL Server COM 组件创建实例失败 SQL2008数据库总会出现从 IClassFactory 为 CLSID 为 {17BCA6E8-A95D-497E-B2F9-AF6AA4759 ...
- Android Studio Eclipse Code Formatter
在从Eclipse转到Android Studio上开发后,如果还想继续使用在Eclipse上制定的自定义的Code Formatter的话,需要按如下步骤操作:1.进入Settings界面,如果能看 ...
- 错误集:js解析jQuery.post返回的xml之Could not find action or result
js里用jQuery.post去后台查询数据,返回的是xml格式的数据流. js代码: var params = ""; params = encodeURI(params); v ...
- 用JS实现的类似QQ密码的输入特效
<input ID="_PSD"></input> <script> function passwordText(ID,pd,mark){ ma ...
- aes rsa加密
aes在加密时,若加密字符串的长度不是16,则会在后面加0x00补足16位,所以在解密后还应该去除0x00 小于16字节的原文会得到16字节长度的密文,小于32字节的原文会得到32字节长度的密文,大于 ...
- MySQL中函数CONCAT及GROUP_CONCAT
一.CONCAT()函数CONCAT()函数用于将多个字符串连接成一个字符串.使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为+----+ ...
- c :set标签的陷阱(未解决)
三层嵌套的list,第二层解套的时候用Cset标签给设置别名,第一个对象正常使用,第二个对象开始传入内存的地址的值,但是无法获取对象属性