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;
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语言单链表实现的更多相关文章
- C语言单链表实现19个功能完全详解
谢谢Lee.Kevin分享了这篇文章 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将 ...
- C语言—单链表
单链表操作:读取,插入和删除 #include "stdafx.h" #include <string.h> #include <stdio.h> #inc ...
- C语言——单链表初始化、求表长、读表元素、插入元素
头文件Linear.h // 单链表的类型定义 typedef struct node { int data; // 数据域 struct node *next; // 指针域 }Node, *Lin ...
- c语言-单链表(二)
继续复习链表知识点,本章包含单链表的增加,删除,判断是否为空,和链表长度,以及链表的排序 几个知识点 1.链表的判断是否为空 //1.判断链表是否为空 bool isempty_list(PNODE ...
- C语言单链表的实现
// // main.c // gfhjhgdf // // Created by chenhao on 13-12-23. // Copyright (c) 2013年 chenhao. A ...
- c语言-单链表(一)
定义节点: typedef struct Node { int data; Node* pNext; }NODE, *PNODE; 细节说明,PNode 就代表struct Node* ,上面的表单是 ...
- 零基础玩转C语言单链表
下图为最一简单链表的示意图: 第 0 个结点称为头结点,它存放有第一个结点的首地址,它没有数据,只是一个指针变量.以下的每个结点都分为两个域,一个是数据域,存放各种实际的数据,如学号 num,姓名 n ...
- c语言——单链表分拆——头插法创建链表,尾插法生成链表
#if 1 #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; ...
- C语言单链表简单实现(简单程序复杂化)
PS: goto还是很好玩的. #include <stdio.h> #include <stdlib.h> typedef struct _node{ int value; ...
随机推荐
- 使用 宝塔面板快速部署Java项目
环境描述: 服务器系统:CentOS7 64位操作系统 面板版本:宝塔6.9.4 Nginx版本:Nginx 1.16 Tomcat版本:Tomcat7 JDK版本:1.8.0_121 环境部署就不用 ...
- myeclipse搭建activemq 简单聊天
需要一起交流的请加群qq:200634530
- AngularJs 特性 之 MVC
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- [canvas入坑3] 类似ps中魔术棒或者画图中油漆桶的功能
查看效果请到 http://philippica.github.io/ 点击fill 这功能其实实现很low,最早高一看黑书的时候看到了floodfill算法感觉好神奇,转念一想这不就是bfs么!! ...
- Object.keys(obj)
对象.keys 很明显是获得对象的key的一个数组 数组的key arr = ['a', 'b', 'c']; console.log(Object.keys(arr)); // console: [ ...
- Java EE 学习(9):IDEA + maven + spring 搭建 web(5)- 博客文章管理
转载:Gaussic(一个致力于AI研究却不得不兼顾项目的研究生) . 注:在阅读本文前,请先阅读: Java EE 学习(5):IDEA + maven + spring 搭建 web(1) Jav ...
- canvas游戏开发系列(1):基础知识
canvas基础知识 canvas是什么? canvas是html5的一个元素,可以说他的功能是html元素中最强大的一个. 举个栗子: 第一步:在页面中引入canvas标签,并且设置好宽高背景等样式 ...
- 如何在Windows下开发Python:在cmd下运行Python脚本+如何使用Python Shell(command line模式和GUI模式)+如何使用Python IDE
http://www.crifan.com/how_to_do_python_development_under_windows_environment/ 本文目的 希望对于,如何在Windows下, ...
- 反射的基本使用以及原理(Class获取方式)
1.什么是反射技术? 动态获取指定类以及类中的内容(成员),并运行其内容. 应用程序已经运行,无法在其中进行new对象的建立,就无法使用对象.这时可以根据配置文件的类全名去找对应的字节码文件,并加载进 ...
- Scrapy笔记:持久化,Feed exports的使用
首先要明确的是,其实所有的FeedExporter都是类,里面封装了一般进行io操作的方法.因此,要怎么输出呢?其实从技术实现来说,在生成item的每一步调用其进行储存都是可以的,只不过为了更加符合s ...