c语言循环单链表
/*************************************************************************
> File Name: singleLineTable.c
> Author: zshh0604
> Mail: zshh0604@.com
> Created Time: 2014年10月15日 星期三 11时34分08秒
************************************************************************/ #include<stdio.h>
#include<stdlib.h>
#include<string.h> /***
* 循环单链表。
*
* 学生结构体:
* id: 学生编号
* name:学生姓名
* math:分数
* next:指向下一个学生结构体
*/
typedef struct student {
int id;
char name[20];
int math;
struct student * next;
}stu; typedef int cmp_stu(const void * ,const void *); /****
* 函数功能:
* 创建一个头节点。
* 函数參数:
* void.
* 函数的返回值:
* 返回头节点指针。
*/
stu * create(void)
{
stu *head = NULL;
stu *p = NULL;
stu *new = NULL;
int tmpId = 0 ;
char tmpName[20];
int tmpMath; head =(stu*) malloc(sizeof(stu));
if(head == NULL)
{
printf("分配stu地址空间失败! ! ! \n");
return NULL;
} head->id = tmpId; printf("name =");
scanf("%s",tmpName);
strncpy(head->name,tmpName,20); printf("math =");
scanf("%d",&tmpMath);
head->math = tmpMath; // head->next = NULL; //单链表
head->next = head; //循环单链表
p = head; //当头创建出来之后应该将指针指向该头部。 while(1)
{
new = (stu*) malloc(sizeof(stu));
if(new==NULL)
{
printf("malloc new error\n");
return NULL;
}
tmpId++;
if(tmpId == 3)
{
break;
} new->id = tmpId; printf("\nname=");
scanf("%s",tmpName);
strncpy(new->name,tmpName,20); printf("math=");
scanf("%d",&tmpMath);
new->math = tmpMath; p->next = new;
p = new;
//new ->next = NULL; //单链表
new->next = head; //循环单链表
}
return head;
} /***
* 函数功能:
* 打印输出单链表中的数据。 * 函数參数:
* head 是链表的头。 * 返回值:
* 没有返回值
*/
void printf_list(stu *head)
{
stu *tmp = NULL;
int i = 0;
if(head== NULL)
{
return;
}
tmp = head->next;
#if 1 //循环单链表
printf("name = %s\n",head->name);
printf("math = %d\n",head->math);
while(tmp!=head)
{
i++;
printf("name = %s\n",tmp->name);
printf("math = %d\n",tmp->math);
tmp = tmp->next;
}
#else
while(tmp!=NULL) //单链表
{
i++;
printf("name = %s\n",tmp->name);
printf("math = %d\n",tmp->math);
tmp = tmp->next; #endif
printf("len = %d\n",i);
}
/*****
* 函数功能:
* 比較函数。
* 函数參数:
*
* 函数返回值:
* 返回0表示成功。
* 返回1表示失败?
* */
int cmp(const void * data, const void * key)
{
stu * head = NULL;
int * tmpkey =NULL;
head = (stu*) data;
tmpkey=(int*)key;
//printf("head->id = %d, tmpkey = %d",((stu*)data)->id, *tmpkey);
if(head->id == *tmpkey)
{
return 0;
}
return 1;
} /****
*
* 函数功能:
* 查找一个节点中的数据。
* 函数參数:
*
* 函数返回值:
* 返回0查找成功,返回1查找失败。 */
void * find_stu(stu* head,cmp_stu* cmps, const void *key)
{
stu * tmp = NULL;
tmp = head->next; if(key == NULL)
{
return NULL;
} #if 1 //循环单链表
if(cmps((const void *)head, (const void *)key) == 0)
{
printf("name = %s\n",tmp->name);
printf("math = %d\n",tmp->math);
return tmp;
}
while(tmp != head)
{
if (cmps((const void *) tmp,(const void * )key)==0)
{
printf("name = %s\n",tmp->name);
printf("math = %d\n",tmp->math);
return tmp;
}
tmp = tmp->next;
}
#else //单链表
while(tmp != NULL)
{
if (cmps((const void *) tmp,(const void * )key)==0)
{
printf("name = %s\n",tmp->name);
printf("math = %d\n",tmp->math);
return tmp;
}
tmp = tmp->next;
#endif
return NULL;
} /***
* 函数功能:
* 插入节点。
* 函数參数:
* head:链表中的节点。
* new:须要插入的节点。
* 函数的返回值:
* 返回0表示插入成功。
* 返回1表示插入失败。
*/
int insert_tool(stu* head, stu* new)
{
if(head==NULL||new == NULL)
{
return 1;
}
#if 1 //循环单链表
if(head->next == head)
{
head->next = new;
new->next = head;
}
#else //单链表
if(head->next == NULL)
{
head->next = new;
new->next = NULL;
}
#endif
new->next = head->next;
head->next = new;
return 0;
} /***
* 函数功能:
* 依据名称进行比較。
* 函数參数:
* data数据,key为要查数据中查找的值。 * 函数的返回值:
* 返回0成功。 返回1失败。
*/
int cmp_name(const void *data, const void *key)
{ stu *tmp = NULL;
char *tmpName = NULL;
if(data== NULL || key == NULL)
{
return 1;
}
tmp =(stu *) data;
tmpName =(char *) key;
if(strncmp(tmp->name,tmpName, 20)==0)
{
return 0;
}
return 1;
} /***
*
* 函数功能:
* 插入一个节点到链表中。 * 函数參数:
* head:链表的头节点。
* name :要查看的节点的名称。 * 函数返回值:
* 返回0插入成功。
* 返回1插入失败。
*/
int insert_stu(stu* head,char *name)
{
stu * tmp = NULL;
stu * new = NULL;
char tmpName[20];
int tmpMath;
tmp = (stu *)find_stu(head,cmp_name,name); if(tmp == NULL)
{
printf("没有找到该同学\n");
return 1;
}
new = (stu*) malloc(sizeof(stu)); printf("name=");
scanf("%s",tmpName);
strncpy(new->name,tmpName,20); printf("math=");
scanf("%d", &tmpMath);
new->math = tmpMath; new->id = 10;
insert_tool(tmp,new);
return 0;
} /**
*函数功能:
* 删除制定的节点。 *參数:
* head:链表的头
* name:要删除学生的名字。 *返回值。
* 0 返回成功。1返回失败。 */
int delete_stu(stu * head,char *name)
{
stu * back = NULL;
stu * p = NULL;
p = head;
#if 1 //循环单链表
while(p!=head)
{
back = p;
p = p->next;
if(strcmp(p->name,name) == 0)
{
back->next = p->next;
p->next = head;
free(p);
return 0;
}
} #else //单链表
while(p!=NULL)
{
back = p;
p = p->next;
if(strcmp(p->name,name) == 0)
{
back->next = p->next;
p->next = NULL;
free(p);
return 0;
} #endif
return 1;
}
/***
* 函数功能:
* 销毁链表。
* 函数的參数:
* 链表的头。
* 函数的返回值
* 0表示返回成功。 1表示返回失败。 */ int destory_list(stu* head)
{
stu *tmp;
stu *p;
p = head->next;
while(p!=head)
{
tmp = p;
p = p->next;
tmp->next = NULL;
printf("name = %s", tmp->name);
free(tmp);
} } int main(void)
{
int i = 2;
stu * head = NULL;
head = create();
printf_list(head);
find_stu(head,cmp,&i);
insert_stu(head,"bb");
printf_list(head);
printf("----------------------\n");
destory_list(head);
head = NULL;
printf_list(head);
printf("---------------------\n");
}
c语言循环单链表的更多相关文章
- C语言版本:循环单链表的实现
SClist.h #ifndef __SCLIST_H__ #define __SCLIST_H__ #include<cstdio> #include<malloc.h> # ...
- c语言有头循环单链表
/************************************************************************* > File Name: singleLin ...
- 带头结点的循环单链表----------C语言
/***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...
- 循环单链表定义初始化及创建(C语言)
#include <stdio.h> #include <stdlib.h> /** * 含头节点循环单链表定义,初始化 及创建 */ #define OK 1; #defin ...
- C语言实现单链表,并完成链表常用API函数
C语言实现单链表,并完成链表常用API函数: 1.链表增.删.改.查. 2.打印链表.反转打印.打印环形链表. 3.链表排序.链表冒泡排序.链表快速排序. 4.求链表节点个数(普通方法.递归方法). ...
- c语言版单链表
1 //c语言单链表 2 #include <stdio.h> 3 #include <stdlib.h> 4 typedef struct Node 5 { 6 int da ...
- C代码实现非循环单链表
C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...
- C语言实现单链表-03版
在C语言实现单链表-02版中我们只是简单的更新一下链表的组织方式: 它没有更多的更新功能,因此我们这个版本将要完成如下功能: Problem 1,搜索相关节点: 2,前插节点: 3,后追加节点: 4, ...
- C语言实现单链表-02版
我们在C语言实现单链表-01版中实现的链表非常简单: 但是它对于理解单链表是非常有帮助的,至少我就是这样认为的: 简单的不能再简单的东西没那么实用,所以我们接下来要大规模的修改啦: Problem 1 ...
随机推荐
- FORM-加载前指定日期时间格式
PRE-FORM -- Standard date format --BEGIN set_application_property(DATE_FORMAT_COMPATIBILITY_MODE, ' ...
- Laravel技⃣术⃣特⃣点⃣
1.Bundle是Laravel的扩展包组织形式或称呼.Laravel的扩展包仓库已经相当成熟了,可以很容易的帮你把扩展包(bundle)安装到你的应用中.你可以选择下载一个扩展包(bundle)然后 ...
- [UML] UML中类之间的几种关系
类之间可能存在以下几种关系:关联(association).依赖(dependency).聚合(Aggregation,也有的称聚集).组合(Composition).泛化(generalizatio ...
- fiddler在ios10.3系统抓包https失败原因解决
一直是按照以往的设置抓包,设置代理ip,通过Safari下载安装证书,抓包https怎么显示证书无效呢?难道证书被apple设为黑名单了?google后发现,IOS10.3以后,安装了证书不是默认启用 ...
- Hibernate原生SQL查询数据转换为HQL查询数据方法
HQL形式:(构造方法不支持timestamp类型) public List<Device> queryByMatherBoardId(String matherBoardId) { St ...
- IIS 服务器隐藏index.php 的方法
在项目根目录下创建web.config文件 写入以下代码即可 <?xml version="1.0" encoding="UTF-8"?> < ...
- thinkphp3.2中在模板页面使用运算符
首先要明确的是,ThinkPHP 内置模板引擎支持在模板中使用算术运算符(+.-.*./ 和 %),例子: public function index(){ $x = 1; $y = 2; $z = ...
- 往aws中的s3上传数据
在官网下载对应的sdk http://aws.amazon.com/cn/sdk-for-net/ 然后再aws上为s3上的bucket生成密钥对 access_key和secret_key 调用对应 ...
- spring java 方式配置JedisPool Bean
来自一个开源项目https://git.oschina.net/geek_qi/ace-cache package com.ace.cache.config; import com.ace.cache ...
- 阿里巴巴Android开发手册(规约)
阿里巴巴Android开发手册(规约) 学习了:https://www.cnblogs.com/jb2011/p/8487889.html 这个猛 https://blog.csdn.net/ali ...