mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 23:56:54 +08:00
deque尾插调试通过了
This commit is contained in:
parent
bf0fe6c75a
commit
cfcf212f96
@ -1,81 +1,155 @@
|
|||||||
|
|
||||||
#include "deque.h"
|
#include "deque.h"
|
||||||
|
|
||||||
bool deque_push_back(struct _list* self, void* obj)
|
bool deque_push_back(struct _deque* self, void* obj)
|
||||||
{
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
assert(self->_head != NULL);
|
||||||
|
struct _deque_node* node = NULL;
|
||||||
|
|
||||||
}
|
// create a new object
|
||||||
bool deque_push_front(struct _list* self, void* obj)
|
void* new_obj = (void*)malloc(self->_obj_size);
|
||||||
{
|
if (new_obj == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memmove(new_obj, obj, self->_obj_size);
|
||||||
|
|
||||||
}
|
// create a new node
|
||||||
bool deque_pop_back(struct _list* self, void* obj)
|
struct _deque_node* new_node = (struct _deque_node*)malloc(sizeof(struct _deque_node));
|
||||||
{
|
if (new_node == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
new_node->obj = new_obj;
|
||||||
|
|
||||||
}
|
// link node
|
||||||
bool deque_pop_front(struct _list* self, void* obj)
|
if(self->empty(self))
|
||||||
{
|
{
|
||||||
|
// if this is first node
|
||||||
|
self->_head->next = new_node;
|
||||||
|
self->_head->prev = new_node;
|
||||||
|
}
|
||||||
|
new_node->prev = self->_head->next;
|
||||||
|
new_node->next = self->_head->prev;
|
||||||
|
|
||||||
}
|
node = self->_head->next;
|
||||||
bool deque_back(struct _list* self, void* obj)
|
node->next = new_node;
|
||||||
{
|
|
||||||
|
|
||||||
|
node = self->_head->prev;
|
||||||
|
node->prev = new_node;
|
||||||
|
|
||||||
|
self->_head->next = new_node;
|
||||||
|
|
||||||
|
self->_size += 1;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
bool deque_front(struct _list* self, void* obj)
|
|
||||||
|
bool deque_push_front(struct _deque* self, void* obj)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool deque_insert(struct _list* self, int index, void* obj)
|
bool deque_pop_back(struct _deque* self, void* obj)
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
bool deque_erase(struct _list* self, int index, void* obj)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int deque_index(struct _list* self, void* obj)
|
bool deque_pop_front(struct _deque* self, void* obj)
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
bool deque_remove(struct _list* self, void* obj)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool deque_clear(struct _list* self)
|
bool deque_back(struct _deque* self, void* obj)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
assert(self->_head != NULL);
|
||||||
|
assert(obj != NULL);
|
||||||
|
|
||||||
|
if (self->empty(self))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memmove(obj, self->_head->next->obj, self->_obj_size);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool deque_front(struct _deque* self, void* obj)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool deque_get(struct _list* self, int index, void* obj)
|
bool deque_insert(struct _deque* self, int index, void* obj)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
bool deque_set(struct _list* self, int index, void* obj)
|
bool deque_erase(struct _deque* self, int index, void* obj)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t deque_size(struct _list* self)
|
int deque_index(struct _deque* self, void* obj)
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
bool deque_empty(struct _list* self)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
void deque_destory(struct _list* self)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void deque_print(struct _list* self)
|
bool deque_remove(struct _deque* self, void* obj)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool deque_clear(struct _deque* self)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool deque_get(struct _deque* self, int index, void* obj)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool deque_set(struct _deque* self, int index, void* obj)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t deque_size(struct _deque* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
return self->_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool deque_empty(struct _deque* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
return !self->size(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deque_destory(struct _deque* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
self->clear(self);
|
||||||
|
if (self->_head != NULL)
|
||||||
|
{
|
||||||
|
free(self->_head);
|
||||||
|
self->_head = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deque_print(struct _deque* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
assert(self->_head != NULL);
|
||||||
|
|
||||||
|
uint32_t i = 0;
|
||||||
|
struct _deque_node * node = self->_head->prev;
|
||||||
|
while (node != self->_head->next)
|
||||||
|
{
|
||||||
|
self->print_obj(node->obj);
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool deque_init(struct _deque* deque, uint32_t obj_size)
|
bool deque_init(struct _deque* deque, uint32_t obj_size)
|
||||||
{
|
{
|
||||||
// attribute
|
// attribute
|
||||||
@ -96,6 +170,9 @@ bool deque_init(struct _deque* deque, uint32_t obj_size)
|
|||||||
deque->insert = deque_insert;
|
deque->insert = deque_insert;
|
||||||
deque->pop_back = deque_pop_back;
|
deque->pop_back = deque_pop_back;
|
||||||
deque->pop_front = deque_pop_back;
|
deque->pop_front = deque_pop_back;
|
||||||
|
deque->push_back = deque_push_back;
|
||||||
|
deque->push_front = deque_push_front;
|
||||||
|
deque->print = deque_print;
|
||||||
deque->remove = deque_remove;
|
deque->remove = deque_remove;
|
||||||
deque->set = deque_set;
|
deque->set = deque_set;
|
||||||
deque->size = deque_size;
|
deque->size = deque_size;
|
||||||
@ -106,8 +183,9 @@ bool deque_init(struct _deque* deque, uint32_t obj_size)
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
deque->_head->prev = deque->_head;
|
deque->_head->obj = NULL;
|
||||||
deque->_head->next = deque->_head;
|
deque->_head->prev = NULL;
|
||||||
|
deque->_head->next = NULL;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ struct _deque_node
|
|||||||
|
|
||||||
struct _deque
|
struct _deque
|
||||||
{
|
{
|
||||||
struct _deque_node* _head;
|
struct _deque_node* _head; // _head->prev指向头节点、_head->next指向尾节点
|
||||||
|
|
||||||
uint32_t _obj_size; // 元素大小
|
uint32_t _obj_size; // 元素大小
|
||||||
uint32_t _size; // 栈大小
|
uint32_t _size; // 栈大小
|
||||||
@ -21,33 +21,33 @@ struct _deque
|
|||||||
uint32_t _ratio; // 扩展比率
|
uint32_t _ratio; // 扩展比率
|
||||||
|
|
||||||
// kernel
|
// kernel
|
||||||
bool (*push_back)(struct _list* self, void* obj);
|
bool (*push_back)(struct _deque* self, void* obj);
|
||||||
bool (*push_front)(struct _list* self, void* obj);
|
bool (*push_front)(struct _deque* self, void* obj);
|
||||||
bool (*pop_back)(struct _list* self, void* obj);
|
bool (*pop_back)(struct _deque* self, void* obj);
|
||||||
bool (*pop_front)(struct _list* self, void* obj);
|
bool (*pop_front)(struct _deque* self, void* obj);
|
||||||
bool (*back)(struct _list* self, void* obj);
|
bool (*back)(struct _deque* self, void* obj);
|
||||||
bool (*front)(struct _list* self, void* obj);
|
bool (*front)(struct _deque* self, void* obj);
|
||||||
|
|
||||||
bool (*insert)(struct _list* self, int index, void* obj);
|
bool (*insert)(struct _deque* self, int index, void* obj);
|
||||||
bool (*erase)(struct _list* self, int index, void* obj);
|
bool (*erase)(struct _deque* self, int index, void* obj);
|
||||||
|
|
||||||
int (*index)(struct _list* self, void* obj);
|
int (*index)(struct _deque* self, void* obj);
|
||||||
bool (*remove)(struct _list* self, void* obj);
|
bool (*remove)(struct _deque* self, void* obj);
|
||||||
|
|
||||||
bool (*clear)(struct _list* self);
|
bool (*clear)(struct _deque* self);
|
||||||
|
|
||||||
bool (*get)(struct _list* self, int index, void* obj);
|
bool (*get)(struct _deque* self, int index, void* obj);
|
||||||
bool (*set)(struct _list* self, int index, void* obj);
|
bool (*set)(struct _deque* self, int index, void* obj);
|
||||||
|
|
||||||
// size
|
// size
|
||||||
uint32_t(*size)(struct _list* self);
|
uint32_t(*size)(struct _deque* self);
|
||||||
bool (*empty)(struct _list* self);
|
bool (*empty)(struct _deque* self);
|
||||||
|
|
||||||
// free
|
// free
|
||||||
void (*destory)(struct _list* self);
|
void (*destory)(struct _deque* self);
|
||||||
|
|
||||||
// print
|
// print
|
||||||
void (*print)(struct _list* self);
|
void (*print)(struct _deque* self);
|
||||||
void (*print_obj)(void* obj);
|
void (*print_obj)(void* obj);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,7 +1,64 @@
|
|||||||
|
|
||||||
#include "deque.h"
|
#include "deque.h"
|
||||||
|
|
||||||
|
static void print_num(void* obj)
|
||||||
|
{
|
||||||
|
printf("(%2d )", *(int*)obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void deque_test_num(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
int data[] = { 0, 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
int temp = 0;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
|
||||||
|
struct _deque dq;
|
||||||
|
deque_init(&dq, sizeof(int));
|
||||||
|
dq.print_obj = print_num;
|
||||||
|
|
||||||
|
printf("\n\n----- deque_test_num -----\n");
|
||||||
|
|
||||||
|
printf("----- push_back -----\n");
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
dq.push_back(&dq, &data[i]);
|
||||||
|
|
||||||
|
dq.back(&dq, &temp);
|
||||||
|
|
||||||
|
printf("push_back = ");
|
||||||
|
dq.print_obj(&temp);
|
||||||
|
printf("\tsize after push_back = %2d\n", dq.size(&dq));
|
||||||
|
}
|
||||||
|
printf("----- print -----\n");
|
||||||
|
dq.print(&dq);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
printf("----- pop -----\n");
|
||||||
|
for (i = 0; i < len + 1; i++)
|
||||||
|
{
|
||||||
|
if (true == s.pop(&s, &temp))
|
||||||
|
{
|
||||||
|
printf("top = ");
|
||||||
|
s.print_obj(&temp);
|
||||||
|
printf("\tsize after push = %2d\n", s.size(&s));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("pop failed! because stack is empty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s.empty(&s))
|
||||||
|
{
|
||||||
|
printf("----- empty -----\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
dq.destory(&dq);
|
||||||
|
}
|
||||||
|
|
||||||
void deque_test(void)
|
void deque_test(void)
|
||||||
{
|
{
|
||||||
|
deque_test_num();
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ int main()
|
|||||||
{
|
{
|
||||||
// while (1)
|
// while (1)
|
||||||
{
|
{
|
||||||
list_test();
|
// list_test();
|
||||||
// stack_test();
|
// stack_test();
|
||||||
|
deque_test();
|
||||||
|
|
||||||
// queue_test();
|
// queue_test();
|
||||||
// tree_test();
|
// tree_test();
|
||||||
// rbtree_test();
|
// rbtree_test();
|
||||||
|
Loading…
Reference in New Issue
Block a user