Set容器c语言版本

小白写代码之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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中没有内置的向量(vector)容器,但可以自己实现一个类似于向量的动态数组,以下是一个简单的实现示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { int* data; int size; int capacity; } vector; vector* vec_create() { vector* vec = (vector*)malloc(sizeof(vector)); vec->data = (int*)malloc(sizeof(int) * 2); vec->size = 0; vec->capacity = 2; return vec; } void vec_destroy(vector* vec) { free(vec->data); free(vec); } void vec_push_back(vector* vec, int elem) { if (vec->size == vec->capacity) { vec->capacity *= 2; vec->data = (int*)realloc(vec->data, sizeof(int) * vec->capacity); } vec->data[vec->size++] = elem; } int vec_size(vector* vec) { return vec->size; } int vec_get(vector* vec, int index) { if (index < 0 || index >= vec->size) { printf("vector index out of range!"); exit(1); } return vec->data[index]; } void vec_set(vector* vec, int index, int elem) { if (index < 0 || index >= vec->size) { printf("vector index out of range!"); exit(1); } vec->data[index] = elem; } int main() { vector* vec = vec_create(); vec_push_back(vec, 1); vec_push_back(vec, 2); vec_push_back(vec, 3); vec_push_back(vec, 4); vec_push_back(vec, 5); for (int i = 0; i < vec_size(vec); i++) { printf("%d ", vec_get(vec, i)); } vec_set(vec, 2, 6); printf("\n"); for (int i = 0; i < vec_size(vec); i++) { printf("%d ", vec_get(vec, i)); } vec_destroy(vec); return 0; } ``` 以上代码实现了一个简单的向量容器,使用方法类似于STL中的vector。vec_create用于创建向量,vec_push_back用于向向量末尾添加元素,vec_size用于获取向量的大小,vec_get用于获取指定下标的元素,vec_set用于修改指定下标的元素,vec_destroy用于销毁向量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值