/*************************************************************************
> 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 = 0;
strncpy(head->name,"\0");
head->math = 0; // 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 //有头循环单链表
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->next;
#if 1 //循环单链表
if(strcmp(p->name,name)==0)
{
head->next = p->next;
p->next = NULL;
free(p);
return 0;
}
while(p != head)
{
back = p;
p = p->next;
if(strcmp(p->name,name) == 0)
{
back->next = p->next;
p->next = NULL;
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语言有头循环单链表的更多相关文章

  1. C语言版本:循环单链表的实现

    SClist.h #ifndef __SCLIST_H__ #define __SCLIST_H__ #include<cstdio> #include<malloc.h> # ...

  2. c语言循环单链表

    /************************************************************************* > File Name: singleLin ...

  3. 带头结点的循环单链表----------C语言

    /***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...

  4. 循环单链表定义初始化及创建(C语言)

    #include <stdio.h> #include <stdlib.h> /** * 含头节点循环单链表定义,初始化 及创建 */ #define OK 1; #defin ...

  5. C代码实现非循环单链表

    C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...

  6. 简单约瑟夫环的循环单链表实现(C++)

    刚刚接触C++以及数据结构,今天做了第一次尝试用C++和数据结构解决问题,问题是基于约瑟夫环问题的简单版. 先来看看约瑟夫环问题的介绍: 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.. ...

  7. PTA 循环单链表区间删除 (15 分)

    本题要求实现带头结点的循环单链表的创建和单链表的区间删除.L是一个带头结点的循环单链表,函数ListCreate_CL用于创建一个循环单链表,函数ListDelete_CL用于删除取值大于min小于m ...

  8. 【c++版数据结构】之循环单链表的实现(带头结点以及尾节点)

    所实现的循环单链表的结构例如以下图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill ...

  9. C++学习(三十五)(C语言部分)之 单链表

    单链表 就好比火车 火车头-->链表头部火车尾-->链表尾部火车厢-->链表的节点火车厢连接的部分-->指针火车中的内容-->链表节点的的数据链表节点包含数据域和指针域数 ...

随机推荐

  1. Winform窗体验证登陆

    用户名,密码尽量不要在BLL,UIL判断,尽可能的在储存过程判断,通过返回的值不同,进行判断,这样提高安全性SQL Server储存过程代码: BEGINif(exists ( select User ...

  2. 用 vue cli 脚手架搭建单页面 Vue 应用(进阶2)

    1.配置 Node 环境. 自行百度吧. 安装好了之后,打开 cmd .运行 node -v .显示版本号,就是安装成功了. 注:不要安装8.0.0以上的版本,和 vue-cli 不兼容. 我使用的 ...

  3. pycharm激活2018

    因为我的是Windows,所以这篇文章只针对Windows系统. 1.将“0.0.0.0 account.jetbrains.com”中的内容添加到hosts文件中,hosts路径为:C:\Windo ...

  4. BZOJ 3144 切糕 最小割

    题意: 一个矩阵,每个格子分配一个数,不同的数字,代价不同,要求相邻格子数字差小等于d 求最小代价. 分析: 我猜肯定有人看题目就想到最小割了,然后一看题面理科否决了自己的这个想法…… 没错,就是最小 ...

  5. 笔试算法题(30):从已排序数组中确定数字出现的次数 & 最大公共子串和最大公共序列(LCS)

    出题:在已经排序的数组中,找出给定数字出现的次数: 分析: 解法1:由于数组已经排序,所以可以考虑使用二分查找确定给定数字A的第一个出现的位置m和最后一个出现的位置n,最后m-n+1就是A出现的次数: ...

  6. ubuntu 虚拟机系统调优

    Ubuntu虚拟机镜像最佳实践 分区/boot     >1G/root      >10G/var        >5G配swap空间,内存的2倍 vi    /etc/secur ...

  7. eclipse 导入svn项目并添加server

    1.打开svn资源库 window-->show view-->other-->svn-->svn资源库 2.控制台选中文件夹右键-->检出为--finish 3.添加服 ...

  8. mysql数据库导出导入

    MYSQL的命令行模式设置: 我的电脑->属性->高级系统设置->环境变量->系统变量-> 选择Path,在后面添加“;path\mysql\bin;”其中path为MY ...

  9. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  10. 关于Filter中ServletRequest和ServletResponse强转HttpServletRequest和HttpServletResponse安全问题(向下转型一定不安全吗?)

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...