数据结构3_160805无头单向不循环
来源:互联网 发布:贾森威廉姆斯生涯数据 编辑:程序博客网 时间:2024/05/16 15:42
单链表优点:动态结构,整个空间可以多个链表共用
不需要预先分配空间
插入删除方便
缺点:指针占用额外的空间,不能随机查找
==============list.h========================
#ifndef LIST_H__
#define LIST_H__
#define SIZE 32
typedef struct _datatype
{
int id;
char name[SIZE];
int math;
int chinese;
}datatype;
typedef struct node_st{
datatype data;
struct node_st *next;
}list;
int list_destroy(list *);
void list_display(list *);
int list_insert(list **, datatype *);
int list_delete(list **);
datatype *list_find(list *,int );
#endif
===========list.c=======================
#include"list.h"
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
void list_display(list *me)
{
list *cur;
for (cur = me; cur!=NULL; cur = cur->next)
{
printf("%d %s %d %d\n",cur->data.id, cur->data.name, cur->data.math, cur->data.chinese);
}
return ;
}
int list_destroy(list *me)
{
list * save = NULL;
while(me)
{
save = me->next;
free(me);
me = save;
}
return 0;
}
int list_insert(list **me, datatype *data)
{
list *new;
new = malloc(sizeof(*new));
if (NULL == new)
return -1;
new->data = *data;
new->next = *me;
*me = new;
return 0;
}
int list_delete(list **me)
{
if(NULL == *me)
return -1;
list *save;
save = *me;
*me = (*me)->next;
free(save);
return 0;
}
datatype *list_find (list *me, int id)
{
list *save;
for (save = me;save!=NULL;save = save->next)
{
if( save->data.id ==id)
{
return &save->data;
}
}
return NULL;
}
===============main.c==========================
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"list.h"
int main()
{
list *list = NULL;
datatype tmp;
int i;
for (i = 0; i <7 ; i++)
{
tmp.id = i;
snprintf(tmp.name, SIZE, "stu%d",i);
tmp.math = rand()%100;
tmp.chinese = rand()%100;
list_insert(&list, &tmp);
}
list_display(list);
list_delete(&list);
printf("=============\n");
list_display(list);
datatype *prt;
prt = list_find(list,3);
if(prt==NULL)
printf("can't find\n");
else
printf("find it\n");
list_destroy(list);
return 0;
}
- 数据结构3_160805无头单向不循环
- 数据结构2_160802有头单向不循环
- 数据结构-双向循环链表(无头结点)相关操作
- 数据结构-4-160808约瑟夫算法-无头循环链表
- C语言实现,无头结点不带环的单向链表的基本操作
- 单向不循环链表
- 数据结构二(顺序表 & 链表;循环及无头循环链表 & 链栈 )
- 单向无头节点链表操作
- 有头节点单向循环链表
- 数据结构——单向循环链表
- 数据结构之单向循环链表
- 数据结构之单向循环链表
- 【数据结构】单向循环链表实现
- 数据结构--单向循环链表、双向循环链表
- (学习java)带有头结点的单向循环链表
- 【数据结构】单链表(无头节点)
- 数据结构--单向链表3
- 数据结构头结点循环链表
- 一个printf引发的问题
- 机器学习中的算法(1)-决策树模型组合之随机森林与GBDT
- 用 Eclipse 插件提高JAVA 代码质量
- iOS -- textfield输入时键盘升起,改变view高度
- 常用类-NSMutableDictionary
- 数据结构3_160805无头单向不循环
- 九度OJ 1138 进制转换
- 2个链表的第一个公共结点
- 电话号码验证
- Mysql 5.6.30 insert null (ERROR 1048 (23000): Column 'ctime' cannot be null)
- HDU-2647 Reward 【拓扑模板】
- 每天一个Linux命令----(压缩、解压缩之tar与gzip)
- TensorFlow的分布式学习框架简介
- CSS边框与背景[下]