小白写代码之set容器实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIGNMAX 10
int Max_size = 1000;
int Num_node = 0;
void gongneng();
typedef struct Node {
int hashcode;
char data[SIGNMAX];
struct Node* dowm;
struct Node* Lode;
struct Node* Rode;
};
struct Node* sethead() {//产生双向链表
struct Node* head;
struct Node* last;
head = (struct Node*)(malloc(sizeof(struct Node)));
head->hashcode = -2;//头节点的标志
head->dowm = NULL;
head->Lode = NULL;
last = (struct Node*)(malloc(sizeof(struct Node)));
last->hashcode = -1;//尾节点的标志
last->dowm = NULL;
last->Rode = NULL;
last->Lode = head;
head->Rode = last;
return head;//返回的是头节点
}
int hash(char a[]) {//测试成功
int hash_data = 0;
printf("size = %d\n", sizeof(a));
for (int i = 0; i < sizeof(a); i++) {
if (a[i] == '\0')
{
int l = (int)a[i - 1];
hash_data += l * l;//hash主体
break;
}
int m = (int)a[i];
hash_data = hash_data+m*m;
}
printf("hashdata:%d", hash_data);
return hash_data;
}
struct Node* begin(struct Node* head)//已完成
{
if (empty(Num_node)) {
printf("空的set,没有元素");
return NULL;
}
printf("当前元素序列:%d\n", head->Rode->hashcode);
return head->Rode;//
}
int empty() {//已完成
if (Num_node == 0) {
printf("空的");
return 1;
}
else {
printf("有东西的");
return 0;
}
}
int clear(struct Node* head, struct Node* last) {//测试成功
struct Node* p;
struct Node* mid;//中间存储变量
struct Node* middowm;//中间存储变量2
struct Node* mid_2;//中间存储变量2
p = head->Rode;
while (p->hashcode!=-1)
{
//对重复hash链表进行删除
middowm = p->dowm;
while (middowm != NULL) {
mid_2 = middowm->dowm;
printf("删除了重复hash值元素:%s\n", middowm->data);
free(middowm);
Num_node--;
middowm = mid_2;
}
p->dowm = NULL;
mid = p->Rode;
printf("删除了%s\n", p->data);
free(p);
p = mid;
Num_node--;
}
//连接头节点尾节点
head->Rode = last;
last->Lode = head;
return empty(Num_node);
}
struct Node* end(struct Node* last) {//已完成
if (empty(Num_node)) {
printf("空的set,没有元素");
return NULL;
}
printf("当前序列号:%d", last->Lode->Lode->Rode->hashcode);
return last->Lode->Lode->Rode;
}
int compArray(char one[10], char two[10]) {
for (int i = 0; i <= sizeof(one); i++) {
if (one[i] != two[i])
return 0;
}
return 1;
}
int erase(struct Node* head, int Erase_hash_code, char Erase_data[]) {//测试成功
struct Node* p;
struct Node* mid_L;
struct Node* mid_R;
struct Node* mid_Up;
p = head->Rode;
while (p->hashcode != -1) {
if (p->hashcode == Erase_hash_code) {
mid_L = p->Lode;
mid_R = p->Rode;
if (compArray(p->data,Erase_data)/*判断主链当前值是否相等*/)
{
if (p->dowm!=NULL/*副链有值*/)
{
p->dowm->Lode = mid_L;//把副链第一个连接其主链左右节点
p->dowm->Rode = mid_R;
mid_R->Lode = p->dowm;
mid_L->Rode = p->dowm;
free(p);
printf("删除成功\n");
Num_node--;
return 1;
//return 1
}
else//副链没有值
{
mid_L->Rode = mid_R;//连接左右节点
mid_R->Lode = mid_L;
free(p);
printf("删除成功\n");
Num_node--;
return 1;
//return 1
}
}
else
{
while (p->dowm != NULL) {
//mid_Up = p;
if (compArray(p->dowm->data, Erase_data)) {
mid_Up = p->dowm->dowm;
free(p->dowm);
printf("删除成功\n");
p->dowm = mid_Up;
Num_node--;
return 1;
}
p = p->dowm;
}
return 0;
}
}
p = p->Rode;
}
printf("没有此元素,删除失败!!\n");
return 0;
}
int find(struct Node* head, int Find_hash_code, char Find_data[]) {//有问题
struct Node* p;
struct Node* mid_dowm;
p = head;
printf("进入find\n");
while (p->hashcode != -1) {
printf("进入循环\n");
if (p->hashcode == Find_hash_code) {
printf("找到hashcode所在\n");
if (compArray(p->data, Find_data))
{
printf("进入分支if\n");
return 1;//p;
}
else
{
printf("进入分支else\n");
mid_dowm = p;
while (mid_dowm
!= NULL) {
if (compArray(mid_dowm->data, Find_data)) {
return 1;//mid_dowm;
}
mid_dowm = mid_dowm->dowm;
}
return 0;//NULL;
}
}
p = p->Rode;
}
return 0;//NULL;
}
struct Node* makeNode(char data[],int make_hash_code) {
struct Node* p;
p = (struct Node*)(malloc(sizeof(struct Node)));
p->hashcode = make_hash_code;/*
strncpy(p->data, data,SIGNMAX);*/
for (int i = 0; i < 10; i++) {
p->data[i] = data[i];
}
p->dowm = NULL;
p->Lode = NULL;
p->Rode = NULL;
return p;
}
int insert(struct Node* head, struct Node* last,int Insert_hash_code,char Insert_data[]) {//未测试没有容量限制
struct Node* p;
struct Node* mid_L;
struct Node* mid_newnode;
int sign = 0;
p = head->Rode;
while (p->hashcode != -1) {
printf("进入插入循环\n");
if (p->hashcode == Insert_hash_code) //找到相同的hash值插入到副链
{
if (compArray(p->data, Insert_data)) {
return 0;
}
while (p->dowm!=NULL)
{
if (compArray(p->data,Insert_data)) {
return 0;
}
p = p->dowm;
}
p->dowm = makeNode(Insert_data,Insert_hash_code);
printf("插入副链\n");
Num_node++;
return 1;
}
else if(p->hashcode>Insert_hash_code&&sign == 0)//找到插入位置
{
mid_newnode = makeNode(Insert_data, Insert_hash_code);
printf("插入主链\n");
mid_L = p->Lode;
mid_L->Rode = mid_newnode;
mid_newnode->Lode = mid_L;
mid_newnode->Rode = p;
p->Lode = mid_newnode;
Num_node++;
return 1;
}
else {
sign = 0;
}
p = p->Rode;
}
//如果都是小于当前hash值
//那就插到主链尾部
mid_L = last->Lode;
mid_newnode = makeNode(Insert_data, Insert_hash_code);
mid_L->Rode = mid_newnode;
mid_newnode->Lode = mid_L;
mid_newnode->Rode = last;
last->Lode = mid_newnode;
printf("插入最后\n");
Num_node++;
return 1;
}
struct Node* lower_bound(struct Node* head, int Low_hash_code) {//未测试
struct Node* p;
p = head;
while (p->hashcode != -1) {
if (p->hashcode >= Low_hash_code) {
printf("当前序列号:%d", p->hashcode);
return p;
}
p = p->Rode;
}
return NULL;
}
struct Node* key_comp(struct Node* head, int First_hash_code,char fist_data[], int Second_hash_code,char secon_data[])/*可能有点小问题*/
{
if (First_hash_code > Second_hash_code) {
return find(head, First_hash_code, fist_data);
}
return find(head, Second_hash_code, secon_data);
}
int max_size() {//已完成
printf("最大值:%d", Max_size);
return Max_size;
}
struct Node* rbegin(struct Node* last) {//已完成
if (empty(Num_node)) {
printf("空的set,没有元素");
return NULL;
}
printf("当前序列号:", last->Lode->hashcode);
return last->Lode;
}
struct Node* rend(struct Node* head) {//已完成
if (empty(Num_node)) {
printf("空的set,没有元素");
return NULL;
}
printf("当前序列号:", head->Rode->Rode->Lode->hashcode);
return head->Rode->Rode->Lode;
}
int size() {//已完成
printf("size:%d", Num_node);
return Num_node;
}
int swap(struct Node* head1, struct Node* last1, struct Node* head2, struct Node* last2) {//已完成
//交换
if (Num_node == 0) {
return 0;
}
struct Node* one;
struct Node* two;
struct Node* three;
struct Node* four;
one = head1->Rode;
two = last1->Lode;
three = head2->Rode;
four = last2->Lode;
head1->Rode = three;
three->Lode = head1;
last1->Lode = four;
four->Rode = last1;
head2->Rode = one;
one->Lode = head2;
last2->Lode = two;
two->Rode = last2;
return 1;
}
struct Node* upper_bound(struct Node* head, int Upper_hash_code) {
struct Node* p;
p = head;
while (p->hashcode != -1) {
if (p->hashcode > Upper_hash_code) {
printf("当前序列号:%d", p->hashcode);
return p;
}
p = p->Rode;
}
return NULL;
}