添加双端队列,用双向循环链表实现。

This commit is contained in:
建峰 2024-06-20 22:26:57 +08:00
parent b4c1421346
commit bf0fe6c75a
5 changed files with 188 additions and 0 deletions

View File

@ -143,6 +143,8 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="deque.c" />
<ClCompile Include="deque_test.c" />
<ClCompile Include="list.c" />
<ClCompile Include="list_test.c" />
<ClCompile Include="main.c" />
@ -159,6 +161,7 @@
<ClInclude Include="common.h" />
<ClInclude Include="config.h" />
<ClInclude Include="demo.h" />
<ClInclude Include="deque.h" />
<ClInclude Include="list.h" />
<ClInclude Include="queue.h" />
<ClInclude Include="rbtree.h" />

View File

@ -54,6 +54,12 @@
<ClCompile Include="list_test.c">
<Filter>测试</Filter>
</ClCompile>
<ClCompile Include="deque.c">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="deque_test.c">
<Filter>测试</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="list.h">
@ -83,5 +89,8 @@
<ClInclude Include="config.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="deque.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

113
datastruct/deque.c Normal file
View File

@ -0,0 +1,113 @@
#include "deque.h"
bool deque_push_back(struct _list* self, void* obj)
{
}
bool deque_push_front(struct _list* self, void* obj)
{
}
bool deque_pop_back(struct _list* self, void* obj)
{
}
bool deque_pop_front(struct _list* self, void* obj)
{
}
bool deque_back(struct _list* self, void* obj)
{
}
bool deque_front(struct _list* self, void* obj)
{
}
bool deque_insert(struct _list* self, int index, void* obj)
{
}
bool deque_erase(struct _list* self, int index, void* obj)
{
}
int deque_index(struct _list* self, void* obj)
{
}
bool deque_remove(struct _list* self, void* obj)
{
}
bool deque_clear(struct _list* self)
{
}
bool deque_get(struct _list* self, int index, void* obj)
{
}
bool deque_set(struct _list* self, int index, void* obj)
{
}
uint32_t deque_size(struct _list* self)
{
}
bool deque_empty(struct _list* self)
{
}
void deque_destory(struct _list* self)
{
}
void deque_print(struct _list* self)
{
}
bool deque_init(struct _deque* deque, uint32_t obj_size)
{
// attribute
deque->_obj_size = obj_size;
deque->_size = 0;
// deque->_capacity = 64;
// deque->_ratio = 2;
// function
deque->back = deque_back;
deque->clear = deque_clear;
deque->destory = deque_destory;
deque->empty = deque_empty;
deque->erase = deque_erase;
deque->front = deque_front;
deque->get = deque_get;
deque->index = deque_index;
deque->insert = deque_insert;
deque->pop_back = deque_pop_back;
deque->pop_front = deque_pop_back;
deque->remove = deque_remove;
deque->set = deque_set;
deque->size = deque_size;
deque->_head = (struct _deque_node*)malloc(sizeof(struct _deque_node));
if (deque->_head == NULL)
{
return false;
}
deque->_head->prev = deque->_head;
deque->_head->next = deque->_head;
return true;
}

56
datastruct/deque.h Normal file
View File

@ -0,0 +1,56 @@
#ifndef _DEQUE_H_
#define _DEQUE_H_
#include "common.h"
struct _deque_node
{
void* obj;
struct _deque_node* prev;
struct _deque_node* next;
};
struct _deque
{
struct _deque_node* _head;
uint32_t _obj_size; // 元素大小
uint32_t _size; // 栈大小
uint32_t _capacity; // 总容量
uint32_t _ratio; // 扩展比率
// kernel
bool (*push_back)(struct _list* self, void* obj);
bool (*push_front)(struct _list* self, void* obj);
bool (*pop_back)(struct _list* self, void* obj);
bool (*pop_front)(struct _list* self, void* obj);
bool (*back)(struct _list* self, void* obj);
bool (*front)(struct _list* self, void* obj);
bool (*insert)(struct _list* self, int index, void* obj);
bool (*erase)(struct _list* self, int index, void* obj);
int (*index)(struct _list* self, void* obj);
bool (*remove)(struct _list* self, void* obj);
bool (*clear)(struct _list* self);
bool (*get)(struct _list* self, int index, void* obj);
bool (*set)(struct _list* self, int index, void* obj);
// size
uint32_t(*size)(struct _list* self);
bool (*empty)(struct _list* self);
// free
void (*destory)(struct _list* self);
// print
void (*print)(struct _list* self);
void (*print_obj)(void* obj);
};
bool deque_init(struct _deque* deque, uint32_t obj_size);
#endif

7
datastruct/deque_test.c Normal file
View File

@ -0,0 +1,7 @@
#include "deque.h"
void deque_test(void)
{
}