单向链表的删除元素,添加元素等操作

来源:互联网 发布:财务电算化软件 编辑:程序博客网 时间: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;
}
原创粉丝点击