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; ...
随机推荐
- http的一些知识
TCP/IP协议分层 应用层 TFP DNS DNS域名解析的过程 在浏览器DNS缓存中搜索 读取系统的hosts文件,查找其中是否有对应的ip 向本地配置的首选DNS服务器发起域名解析请求 HTTP ...
- mysql数据库无法插入中文字符
分析原因:这是因为之前修改了数据库的编码,但数据表的编码却没有跟着改变导致的. 安装mysql 时,使用的是latin编码(随后修改为utf8).建的数据表是在latin编码下建立的.而jsp页面使用 ...
- [SCOI2007][bzoj1070] 修车 [费用流]
题面 传送门 思路 我们考虑某个工人修车的从前到后序列如下: ${W_1,W_2,W_3,...,W_n}$ 那么,对于这n辆车的车主而言,他们等候的总时间为: $\sum_{i=1}^{n}W_i\ ...
- [AGC004D] Teleporter [贪心]
题面: 传送门 思路: 分析可知,这道题中的图是一个环套内向树,首都在环上 首先有一个结论:当首都的出边指向首都时,一定最优(不然首都出发可能无法按时到达首都)(可以按时到达的情况也一定有到不了的) ...
- 【05】Vue 之 实例详解与生命周期
Vue的实例是Vue框架的入口,其实也就是前端的ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进行对整个Vue实例生成.编译.挂着. ...
- ios工程中一天只让显示一次的广告,或是弹出窗,如何实现
需求: 产品 代码实现: 在首页.m中 //一天之内只能批量邀请一次 NSUserDefaults *userDefault = [NSUserDefaults standardUserDefault ...
- Http 请求头 Range
HTTP 请求头 Range 请求资源的部分内容(不包括响应头的大小),单位是byte,即字节,从0开始. 如果服务器能够正常响应的话,服务器会返回 206 Partial Content 的状态码及 ...
- Maven多模块项目依赖管理
Maven多模块项目依赖管理及dependencies与dependencyManagement的区别 转自:http://blog.csdn.net/liutengteng130/article/d ...
- BZOJ 2813: 奇妙的Fibonacci
2813: 奇妙的Fibonacci Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 497 Solved: 134[Submit][Status][ ...
- *LOJ#2085. 「NOI2016」循环之美
$n \leq 1e9,m \leq 1e9,k \leq 2000$,求$k$进制下$\frac{x}{y}$有多少种不同的纯循环数取值,$1 \leq x \leq n,1 \leq y \leq ...