1.一致性哈希
2.解决冲突问题
#pragma once
typedef int ELEM_TYPE;
#define MAXQSIZE 100
typedef struct Queue
{
ELEM_TYPE* base;//数据域 指向malloc申请来的动态内存
int front;//头指针,当队列不空的时间,保存是队头,指向的第一个元素的下标
int rear;//尾指针,当队列不空的时间,保存的是队尾,指向的下一个元素入队的下标
//int length;//有效元素个数
}Queue, * PQueue;
//初始化
void Init_Queue(PQueue pq);
//入队 push
bool Push(PQueue pq, ELEM_TYPE val);
//出队 pop 需要删除操作
bool Pop(PQueue pq, ELEM_TYPE* rtval);
//top 获取队头元素值, 不需要删除操作
bool Top(PQueue pq, ELEM_TYPE* rtval);
//获取其有效元素个数
int Get_length(PQueue pq);
//判空
bool IsEmpty(PQueue pq);
//判满
bool IsFull(PQueue pq);
//清空
void Clear(PQueue pq);
//销毁
void Destroy(PQueue pq);
void Show(PQueue pq);
#pragma once
#include "queue.h"
//设计我们需要的那个结构体:two_queue_to_stack
typedef struct TQTStack
{
Queue q1;//队列1
Queue q2;//队列2
}TQTStack, * PTQTStack;
//初始化
void my_Init_stack(PTQTStack ptq);
//入栈(或者叫压栈 push)
bool my_Push(PTQTStack ptq, ELEM_TYPE val);
//出栈(或者叫弹栈 pop(获取顶部数据,并且删除))//rtval是一个输出参数(C语言讲到)
bool my_Pop(PTQTStack ptq, ELEM_TYPE* rtval);
//获取顶部元素值 top(获取顶部数据)
bool my_Top(PTQTStack ptq, ELEM_TYPE* rtval);
//获取其有效数据个数
int my_Get_length(PTQTStack ptq);
//判空
bool my_IsEmpty(PTQTStack ptq);
//判满
bool my_IsFull(PTQTStack ptq);
扩容
//static void my_Inc(PTQTStack ptq);
//清空 一间房住了一户人 清空相当于把人赶出去
void my_Clear(PTQTStack ptq);
//销毁 一间房住了一户人 销毁相当于把人赶出去还把房烧了
void my_Destroy(PTQTStack ptq);
//打印
void my_Show(PTQTStack ptq);
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "Two_queue_to_stack.h"
//初始化
void my_Init_stack(PTQTStack ptq)
{
assert(ptq != NULL);
Init_Queue(&ptq->q1);
Init_Queue(&ptq->q2);
}
//入栈(或者叫压栈 push)
bool my_Push(PTQTStack ptq, ELEM_TYPE val)
{
assert(ptq != NULL);
if (!IsEmpty(&ptq->q1))
{
return Push(&ptq->q1, val);
}
else
{
return Push(&ptq->q2, val);
}
}
//出栈(或者叫弹栈 pop(获取顶部数据,并且删除))//rtval是一个输出参数(C语言讲到)
bool my_Pop(PTQTStack ptq, ELEM_TYPE* rtval)
{
assert(ptq != NULL);
if (my_IsEmpty(ptq))//若q1和q2都为空 则不需要出栈
return false;
int tmp;
if (!IsEmpty(&ptq->q1))
{
int size = Get_length(&ptq->q1);
while (size > 1)
{
Pop(&ptq->q1, &tmp);//从q1里取值放到tmp里
Push(&ptq->q2, tmp);//将tmp里的值入到q2中
size--;//不能忘
}
//此处 while执行结束 代表着q1里仅剩下唯一的一个元素
return Pop(&ptq->q1, rtval);
}
else
{
int size = Get_length(&ptq->q2);
while (size > 1)
{
Pop(&ptq->q2, &tmp);//从q2里取值放到tmp里
Push(&ptq->q1, tmp);//将tmp里的值入到q1中
size--;//不能忘
}
//此处 while执行结束 代表着q2里仅剩下唯一的一个元素
return Pop(&ptq->q2, rtval);
}
}
//获取顶部元素值 top(获取顶部数据)
bool my_Top(PTQTStack ptq, ELEM_TYPE* rtval)
{
if (my_IsEmpty(ptq))//若q1和q2都为空 则不需要Top
return false;
int tmp;
if (!IsEmpty(&ptq->q1))//当q1不空,则数据都在q1里
{
int size = Get_length(&ptq->q1);
while (size > 1)
{
Pop(&ptq->q1, &tmp);//从q1里取值放到tmp里
Push(&ptq->q2, tmp);//将tmp里的值入到q2中
size--;//不能忘
}
//此处 while执行结束 代表着q1里仅剩下唯一的一个元素
Top(&ptq->q1, rtval);
Pop(&ptq->q1, &tmp);
Push(&ptq->q2, tmp);
return true;
}
else
{
int size = Get_length(&ptq->q2);
while (size > 1)
{
Pop(&ptq->q2, &tmp);//从q2里取值放到tmp里
Push(&ptq->q1, tmp);//将tmp里的值入到q1中
size--;//不能忘
}
//此处 while执行结束 代表着q2里仅剩下唯一的一个元素
Top(&ptq->q2, rtval);
Pop(&ptq->q2, &tmp);
Push(&ptq->q1, tmp);
return true;
}
}
//获取其有效数据个数
int my_Get_length(PTQTStack ptq)
{
return Get_length(&ptq->q1) + Get_length(&ptq->q2);//0+0 x+0 0+x
}
//判空
bool my_IsEmpty(PTQTStack ptq)
{
//当q1和q2都为空的时候 才为空
return IsEmpty(&ptq->q1) && IsEmpty(&ptq->q2);
}
//判满
bool my_IsFull(PTQTStack ptq)
{
if (!IsEmpty(&ptq->q1))
{
return IsFull(&ptq->q1);
}
return IsFull(&ptq->q2);
}
扩容
//static void my_Inc(PTQTStack ptq);
//清空 一间房住了一户人 清空相当于把人赶出去
void my_Clear(PTQTStack ptq)
{
Clear(&ptq->q1);
Clear(&ptq->q2);
}
//销毁 一间房住了一户人 销毁相当于把人赶出去还把房烧了
void my_Destroy(PTQTStack ptq)
{
Destroy(&ptq->q1);
Destroy(&ptq->q2);
}
//打印
void my_Show(PTQTStack ptq)
{
assert(ptq != NULL);
if (!IsEmpty(&ptq->q1))
{
Show(&ptq->q1);
}
Show(&ptq->q2);
}