单向链表的删除元素,添加元素等操作
来源:互联网 发布:财务电算化软件 编辑:程序博客网 时间:2024/04/28 22:42
// xx.cpp : Defines the entry point for the console application.
//#include <stdAfx.h>
#include <math.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>
struct student
{
int id;//
struct student *next;//pointer to next student
char *name;//name
};
struct student_tbl
{
unsigned counts;//whole counts of hash_tbl
unsigned buckets_size;//
struct student *buckets[100];//hash table
};
struct student *add(struct student_tbl *tbl, int id, char *name)
{
struct student *stu = NULL;
int mod = 0;
if (tbl)
{
mod = id%tbl->buckets_size;
if (stu = (struct student *)malloc(sizeof(struct student)))
{
stu->id = id;
stu->name = name;
if (tbl->buckets[mod])//
{//element of list >= 1
stu->next = tbl->buckets[mod]->next;
tbl->buckets[mod]->next = stu;
}
else
{// element of list = 0
if (tbl->buckets[mod] = (struct student *)calloc(sizeof(struct student), 1))
{
stu->next = NULL;
tbl->buckets[mod] = stu;
}
else
{
return NULL;
}
}
}
else
{
return NULL;
}
}
else
{
return NULL;
}
}
struct student *get(struct student_tbl *tbl, int id)
{
struct student *stu = NULL;
struct student *temp = NULL;
int mod = 0;
if (tbl)
{
mod = id%tbl->buckets_size;
if (tbl->buckets[mod])
{
stu = tbl->buckets[mod];
do
{
if (stu->id == id)
{
return stu;
}
temp = stu;
stu = stu->next;
} while (stu);
if (stu)
{
return NULL;
}
}
else
{
return NULL;
}
}
else
{
return NULL;
}
}
void *del(struct student_tbl *tbl, int id)
{
struct student *stu = NULL;
struct student *temp = NULL;
int mod = 0;
int sign = 0;
if (tbl)
{
mod = id%tbl->buckets_size;
if (tbl->buckets[mod])
{
stu = tbl->buckets[mod];
do
{
if (stu->id == id)
{
sign = 1;
break;
}
temp = stu;
stu = stu->next;
} while (stu);
}
else
{
return NULL;
}
if (sign)
{
if (temp)
{
temp->next = stu->next;
free(stu);
}
else
{
if (stu->next)
{
tbl->buckets[mod]->id = stu->next->id;
tbl->buckets[mod]->name = stu->next->name;
tbl->buckets[mod]->next = stu->next->next;
}
else
{
tbl->buckets[mod] = NULL;
// tbl->buckets[mod]->id = NULL;
// tbl->buckets[mod]->name = NULL;
// tbl->buckets[mod]->next = NULL;
}
}
}
else
{
printf("cann't find the node that you want to delete!");
}
}
return 0;
}
void print_stu_hash(struct student_tbl *tbl)
{
int i = 0, j = 0;
struct student *stu = NULL;
if (tbl)
{
j = tbl->buckets_size;
for (i = 0; i < j; i++)
{
if (tbl->buckets[i])
{
stu = tbl->buckets[i];
do
{
printf("id = %d ", stu->id);
printf("name = %s/n", stu->name);
stu = stu->next;
} while (stu);
printf("/r/n/r/n");
}
}
}
}
void print_single_stu(struct student *stu)
{
if (stu)
{
printf("information of the student which you want to get is as following:/r/n");
printf("id = %d/r/n", stu->id);
printf("name = %s/r/n/r/n", stu->name);
}
else
{
printf("there isn't the student which you want to get!/r/n");
}
}
int main(int argc, char* argv[])
{
struct student_tbl *tbl = NULL;
if (tbl = (struct student_tbl *)calloc(sizeof(struct student_tbl), 1))
{
tbl->buckets_size = 10;
struct student *s1 = add(tbl, 12, "zhaoyuanren");
struct student *s2 = add(tbl, 13, "qianzhongshu");
struct student *s3 = add(tbl, 14, "sunwu");
struct student *s4 = add(tbl, 23, "lipeng");
struct student *s5 = add(tbl, 33, "zhouenlai");
struct student *s6 = add(tbl, 22, "wudaozi");
struct student *s7 = add(tbl, 24, "zhenghe");
struct student *s8 = add(tbl, 34, "wangxizhi");
struct student *s9 = add(tbl, 35, "fengchengcheng");
struct student *s10 = add(tbl, 32, "chengsheng");
struct student *s11 = add(tbl, 54, "zhusuiliang");
struct student *s12 = add(tbl, 64, "weiziqing");
print_stu_hash(tbl);
struct student *sg1 = get(tbl, 12);
print_single_stu(sg1);
struct student *sg2 = get(tbl, 16);
print_single_stu(sg2);
del(tbl, 12);
print_stu_hash(tbl);
del(tbl, 35);
print_stu_hash(tbl);
del(tbl, 22);
print_stu_hash(tbl);
del(tbl, 64);
print_stu_hash(tbl);
}
printf("hello world!");
getchar();
return 0;
}
- 单向链表的删除元素,添加元素等操作
- 删除环状单向链表中的重复元素的操作
- 删除单向链表头元素
- jQuery的DOM操作之添加元素和删除元素
- JS的DOM操作元素示例1--删除添加元素
- 关于删除链表元素的操作
- 数据结构(第二天)单向循环链表的创建,插入元素,(删除、查询元素跟单链表操作基本一致)
- 查找单向链表的中间元素
- 单向链表O(1)删除中间元素
- 生成单向链表 并删除链表中某个元素
- Javascript 添加删除等元素和导航
- javascript添加、删除、修改等元素
- 删除链表中和某一个数相同的元素(单向链表)哈理工oj1546
- 数据结构 - 如何删除单向链表的倒数第m个元素?
- 堆的添加,删除元素
- 最小堆操作(元素的添加和删除)
- js数组操作;array数组元素的添加和删除
- java实现单向链表CRUD,反转,排序,查找倒数第k个元素,递归输出等操作
- 好书推荐:2009年图书:VSTO 3.0之Office 2007编程
- 关于蓝牙设备之间的连接问题
- Multi-Byte Character Set & Use Unicode Character Set 收藏
- 444句日常生活英语
- 使用JSON
- 单向链表的删除元素,添加元素等操作
- json_encode 中文解决方法
- 模电应知应会 200 问
- 英语生活箴言
- 现状与未来
- TortoiseSVN 操作指南
- 前缀和后缀运算符在效率上的小区别(转)
- Windows Via C/C++:用户模式下的线程同步——轻量级读写锁(Slim Reader-Writer Locks)
- Windows Via C/C++:用户模式下的线程同步——Condition Variables 条件变量