/*************************************************************************
> 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;
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("name=");
scanf("%s",tmpName);
strncpy(new->name,tmpName,20); printf("math=");
scanf("%d",&tmpMath);
new->math = tmpMath; p->next = new;
p = new;
new ->next = NULL;
}
return head;
} /***
* 函数功能:
* 打印输出单链表中的数据。 * 函数參数:
* head 是链表的头。 * 返回值:
* 没有返回值
*/
void printf_list(stu *head)
{
stu *tmp = NULL;
int i = 0;
if(head== NULL)
{
return;
}
tmp = head; while(tmp!=NULL)
{
i++;
printf("name = %s\n",tmp->name);
printf("math = %d\n",tmp->math);
tmp = tmp->next;
}
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; if(key == NULL)
{
return NULL;
} 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;
}
return NULL;
} /***
* 函数功能:
* 插入节点。 * 函数參数:
* head:链表中的节点。 * new:须要插入的节点。
* 函数的返回值:
* 返回0表示插入成功。
* 返回1表示插入失败。
*/
int insert_tool(stu* head, stu* new)
{
if(head==NULL||new == NULL)
{
return 1;
} if(head->next == NULL)
{
head->next = new;
new->next = NULL;
}
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;
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;
}
}
return 1;
}
/***
* 函数功能:
* 销毁链表。
* 函数的參数:
* 链表的头。 * 函数的返回值
* 0表示返回成功。 1表示返回失败。
*/ int destory_list(stu* head)
{
stu *tmp;
stu *p;
p = head;
while(p!=NULL)
{
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语言单链表实现19个功能完全详解

    谢谢Lee.Kevin分享了这篇文章 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将 ...

  2. C语言—单链表

    单链表操作:读取,插入和删除 #include "stdafx.h" #include <string.h> #include <stdio.h> #inc ...

  3. C语言——单链表初始化、求表长、读表元素、插入元素

    头文件Linear.h // 单链表的类型定义 typedef struct node { int data; // 数据域 struct node *next; // 指针域 }Node, *Lin ...

  4. c语言-单链表(二)

    继续复习链表知识点,本章包含单链表的增加,删除,判断是否为空,和链表长度,以及链表的排序 几个知识点 1.链表的判断是否为空 //1.判断链表是否为空 bool isempty_list(PNODE ...

  5. C语言单链表的实现

    // //  main.c //  gfhjhgdf // //  Created by chenhao on 13-12-23. //  Copyright (c) 2013年 chenhao. A ...

  6. c语言-单链表(一)

    定义节点: typedef struct Node { int data; Node* pNext; }NODE, *PNODE; 细节说明,PNode 就代表struct Node* ,上面的表单是 ...

  7. 零基础玩转C语言单链表

    下图为最一简单链表的示意图: 第 0 个结点称为头结点,它存放有第一个结点的首地址,它没有数据,只是一个指针变量.以下的每个结点都分为两个域,一个是数据域,存放各种实际的数据,如学号 num,姓名 n ...

  8. c语言——单链表分拆——头插法创建链表,尾插法生成链表

    #if 1 #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; ...

  9. C语言单链表简单实现(简单程序复杂化)

    PS: goto还是很好玩的. #include <stdio.h> #include <stdlib.h> typedef struct _node{ int value; ...

随机推荐

  1. 一个iOS程序员眼中的跨域问题

    摘要: 跨域问题是web开发领域一个常见的问题,相信每个web开发者都遇到"跨域"的问题 最近公司的iOS开发任务比较少,所以自己最近开始了Web开发的任务,在用H5做了很多页面, ...

  2. 文本处理grep命令

    this is a words file. words words to be , , , , , , , , , , beginning linux programming 4th edition ...

  3. 转载 hadoop 伪分布安装

    一. 概要        经过几天的调试,终于在Linux Cent OS 5.5下成功搭建Hadoop测试环境.本次测试在一台服务器上进行伪分布式搭建.Hadoop 伪分布式模式是在单机上模拟 Ha ...

  4. FastDFS的安装(复制自己用)

    FastDFS 安装及使用 FastDFS 安装及使用 2012-11-17 13:10:31|  分类: Linux|举报|字号 订阅     Google了一下,流行的开源分布式文件系统有很多,介 ...

  5. iOS开发UI篇—自定义layer

    一.第一种方式 1.简单说明 以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的DrawRect:方法,然后在该方法中画图. 绘制图形的步骤: ...

  6. c#byte数组和string类型转换

    http://blog.csdn.net/rowanhaoa/article/details/42144313/ 用system.text中的encoding这个类

  7. 对计算属性中get和set的理解

    原文参考:https://blog.csdn.net/xiaxiaoxian/article/details/79304004

  8. 关于 react state的改变数据上面的一点问题

    在react当中 比如说 this.state = { loginInfo: { account: "...", password: "..." } } thi ...

  9. jQuery基础 浅析(含基本方法和选择器)

    1.jQuery与DOM互相转换 jQuery入库函数:$(document).ready(function(){}) $(function(){}) $(“#btn”):jQuery存储的是DOM对 ...

  10. 1月24日考试(ftp密码)

    错因分析 ♦对文件的保存不够恰当,例如第一题和第三题的题目,我是真的很愤怒,第一题在我写了一个多小时,终于样例成功.可是当我再一次打开文件时,里面只有我最开始的代码,谁可以告诉我这是为什么(我绝对保存 ...