添加stack实现方式2——动态数组

This commit is contained in:
建峰 2024-06-20 11:45:23 +08:00
parent 2580864c4e
commit 0abf76f8f2
2 changed files with 183 additions and 138 deletions

View File

@ -1,149 +1,43 @@
#include "stack.h" #include "stack.h"
static uint32_t stack_capacity(struct _stack* s)
#if STACK == 1
#if 0
bool stack_init(pstack_t *head)
{
*head = (pstack_t)malloc(sizeof(stack_t));
if(*head == NULL)
{
return false;
}
(*head)->top = NULL;
(*head)->size = 0;
return true;
}
void stack_destroy(pstack_t *head)
{
if(*head != NULL)
{
stack_clear(*head);
free(*head);
*head = NULL;
}
}
bool stack_empty(pstack_t head)
{
return (head == NULL || head->top == NULL) ? true : false;
}
void stack_clear(pstack_t head)
{
pstack_node_t top_item;
if(head != NULL)
{
top_item = head->top;
while(top_item != NULL)
{
head->top = top_item->next;
free(top_item);
top_item = head->top;
}
}
head->size = 0;
}
bool stack_push(pstack_t head, stack_data_t data)
{
pstack_node_t new_item;
if(head == NULL)
{
return false;
}
new_item = (pstack_node_t)malloc(sizeof(stack_node_t));
if(new_item == NULL)
{
return false;
}
new_item->data = data;
// insert from head
new_item->next = head->top;
head->top = new_item;
// increase
if(head->size < _UI32_MAX)
{
head->size ++;
}
return true;
}
void stack_pop(pstack_t head, stack_data_t *data)
{
pstack_node_t top_item;
if(!stack_empty(head))
{
top_item = head->top;
*data = top_item->data;
// free the top item
head->top = top_item->next;
free(top_item);
top_item = NULL;
// decrease
if(head->size > 0)
{
head->size--;
}
}
}
bool stack_get_top(pstack_t head, stack_data_t *data)
{
if(!stack_empty(head))
{
*data = head->top->data;
return true;
}
else
{
return false;
}
}
uint32_t stack_get_size(pstack_t head)
{
return head->size;
}
#endif
uint32_t stack_capacity(struct _stack* s)
{ {
assert(s != NULL); assert(s != NULL);
return s->_capacity; return s->_capacity;
} }
uint32_t stack_size(struct _stack* s) static uint32_t stack_size(struct _stack* s)
{ {
assert(s != NULL); assert(s != NULL);
return s->_size; return s->_size;
} }
bool stack_empty(struct _stack* s) static bool stack_empty(struct _stack* s)
{ {
assert(s != NULL); assert(s != NULL);
// assert(s->_head != NULL);
// return s->_head->next == NULL ? true : false;
return !stack_size(s); return !stack_size(s);
} }
bool stack_peek(struct _stack* s, void* obj) static bool stack_peek(struct _stack* s, void* obj)
{ {
assert(s != NULL); assert(s != NULL);
assert(s->_head != NULL); assert(s->_head != NULL);
assert(obj != NULL); assert(obj != NULL);
if (s->empty(s))
{
return false;
}
struct _stack_node * node = s->_head->next; struct _stack_node * node = s->_head->next;
memmove(obj, node->obj, s->_obj_size); memmove(obj, node->obj, s->_obj_size);
return true; return true;
} }
bool stack_push(struct _stack* s, void* obj) static bool stack_push(struct _stack* s, void* obj)
{ {
assert(s != NULL); assert(s != NULL);
assert(s->_head != NULL); assert(s->_head != NULL);
@ -169,20 +63,24 @@ bool stack_push(struct _stack* s, void* obj)
return true; return true;
} }
bool stack_pop(struct _stack* s, void* obj) static bool stack_pop(struct _stack* s, void* obj)
{ {
assert(s != NULL); assert(s != NULL);
assert(s->_head != NULL); assert(s->_head != NULL);
assert(obj != NULL); // assert(obj != NULL);
if (s->_head->next == NULL) if (s->empty(s))
{ {
return false; return false;
} }
struct _stack_node* node = s->_head->next; struct _stack_node* node = s->_head->next;
// ½«µ¯³öµÄÊý¾ÝÊä³ö
memmove(obj, node->obj, s->_obj_size); if (obj != NULL)
{
// 将弹出的数据输出
memmove(obj, node->obj, s->_obj_size);
}
// ¸üÐÂÖ¸Õë // ¸üÐÂÖ¸Õë
s->_head->next = node->next; s->_head->next = node->next;
@ -195,12 +93,11 @@ bool stack_pop(struct _stack* s, void* obj)
return true; return true;
} }
bool stack_clear(struct _stack* s) static bool stack_clear(struct _stack* s)
{ {
assert(s != NULL); assert(s != NULL);
assert(s->_head != NULL);
if (s->_head->next == NULL) if (s->empty(s))
{ {
return false; return false;
} }
@ -221,7 +118,7 @@ bool stack_clear(struct _stack* s)
return true; return true;
} }
void stack_destory(struct _stack* s) static void stack_destory(struct _stack* s)
{ {
assert(s != NULL); assert(s != NULL);
assert(s->_head != NULL); assert(s->_head != NULL);
@ -231,7 +128,7 @@ void stack_destory(struct _stack* s)
s->_head = NULL; s->_head = NULL;
} }
void print(struct _stack* s) static void stack_print(struct _stack* s)
{ {
uint32_t i = 0; uint32_t i = 0;
struct _stack_node* node = s->_head->next; struct _stack_node* node = s->_head->next;
@ -244,6 +141,8 @@ void print(struct _stack* s)
bool stack_init(struct _stack* s, uint32_t obj_size) bool stack_init(struct _stack* s, uint32_t obj_size)
{ {
assert(s != NULL);
// 1. set attr // 1. set attr
s->_obj_size = obj_size; s->_obj_size = obj_size;
s->_size = 0; s->_size = 0;
@ -257,7 +156,7 @@ bool stack_init(struct _stack* s, uint32_t obj_size)
s->push = stack_push; s->push = stack_push;
s->size = stack_size; s->size = stack_size;
s->destory = stack_destory; s->destory = stack_destory;
s->print = print; s->print = stack_print;
// 3. set node // 3. set node
s->_head = (struct _stack_node *)malloc(sizeof(struct _stack_node)); s->_head = (struct _stack_node *)malloc(sizeof(struct _stack_node));
@ -271,9 +170,132 @@ bool stack_init(struct _stack* s, uint32_t obj_size)
return true; return true;
} }
static bool stack2_peek(struct _stack* s, void* obj)
{
assert(s != NULL);
assert(s->_head != NULL);
assert(obj != NULL);
if (s->empty(s))
{
return false;
}
uint32_t top = s->size(s) - 1;
uint32_t offset = top * s->_obj_size;
memmove(obj, (char *)s->_head->obj + offset, s->_obj_size);
return true;
}
static bool stack2_push(struct _stack* s, void* obj)
{
assert(s != NULL);
assert(s->_head != NULL);
assert(obj != NULL);
void* new_obj = (void*)calloc(1, s->_obj_size);
if (new_obj == NULL)
{
return false;
}
memmove(new_obj, obj, s->_obj_size);
struct _stack_node* new_node = (struct _stack_node*)malloc(sizeof(struct _stack_node));
if (new_node == NULL)
{
return false;
}
new_node->obj = new_obj;
new_node->next = s->_head->next;
s->_head->next = new_node;
s->_size += 1;
return true;
}
static bool stack2_pop(struct _stack* s, void* obj)
{
assert(s != NULL);
assert(s->_head != NULL);
// assert(obj != NULL);
if (s->_head->next == NULL)
{
return false;
}
struct _stack_node* node = s->_head->next;
if (obj != NULL)
{
// 将弹出的数据输出
memmove(obj, node->obj, s->_obj_size);
}
// 更新指针
s->_head->next = node->next;
// 释放数据和节点
free(node->obj);
free(node);
s->_size -= 1;
return true;
}
static void stack2_print(struct _stack* s)
{
uint32_t i = 0;
struct _stack_node* node = s->_head->next;
while (node != NULL)
{
s->print_obj(node->obj);
node = node->next;
}
}
bool stack2_init(struct _stack* s, uint32_t obj_size)
{
assert(s != NULL);
// 1. set attr
s->_obj_size = obj_size;
s->_size = 0;
s->_capacity = 64;
// 2. set function
s->clear = stack_clear;
s->empty = stack_empty;
s->peek = stack2_peek;
s->pop = stack2_pop;
s->push = stack2_push;
s->size = stack_size;
s->destory = stack_destory;
s->print = stack2_print;
// 3. set node
s->_head = (struct _stack_node*)malloc(sizeof(struct _stack_node));
if (s->_head == NULL)
{
return false;
}
// s->_head->obj = NULL;
s->_head->next = NULL; // 无效参数
// 4. set array
s->_head->obj = (void *)calloc(s->_capacity, s->_obj_size);
if (s->_head->obj == NULL)
{
return false;
}
return true;
}
void print_num(void *obj) void print_num(void *obj)
{ {
printf("%2d ", *(int*)obj); printf("(%2d )", *(int*)obj);
} }
void stack_test_num(void) void stack_test_num(void)
@ -288,7 +310,10 @@ void stack_test_num(void)
s.print_obj = print_num; s.print_obj = print_num;
printf("\n\n----- stack_test_num -----\n"); printf("\n\n----- stack_test_num -----\n");
printf("\n\n----- push -----\n"); // get top if stack is empty
s.peek(&s, &temp);
printf("----- push -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
s.push(&s, &data[i]); s.push(&s, &data[i]);
@ -328,7 +353,7 @@ void stack_test_num(void)
void print_char(void* obj) void print_char(void* obj)
{ {
printf("%2c ", *(char*)obj); printf("(%2c )", *(char*)obj);
} }
void stack_test_char(void) void stack_test_char(void)
@ -343,7 +368,10 @@ void stack_test_char(void)
s.print_obj = print_char; s.print_obj = print_char;
printf("\n\n----- stack_test_char -----\n"); printf("\n\n----- stack_test_char -----\n");
printf("\n\n----- push -----\n"); // get top if stack is empty
s.peek(&s, &temp);
printf("----- push -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
s.push(&s, &data[i]); s.push(&s, &data[i]);
@ -390,13 +418,16 @@ struct _student
void print_struct(void* obj) void print_struct(void* obj)
{ {
struct _student * student = (struct _student*)obj; struct _student * student = (struct _student*)obj;
printf("(%-5s : %2d) ", student->name, student->id); printf("(%2d:%-5s ) ", student->id, student->name);
} }
void stack_test_struct(void) void stack_test_struct(void)
{ {
uint32_t i = 0; uint32_t i = 0;
struct _student data[] = { {"zhao", 1},{"qian", 2}, {"sun", 3}, {"li",4}}; struct _student data[] = {
{"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
};
struct _student temp = {0}; struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
@ -405,6 +436,9 @@ void stack_test_struct(void)
s.print_obj = print_struct; s.print_obj = print_struct;
printf("\n\n----- stack_test_struct -----\n"); printf("\n\n----- stack_test_struct -----\n");
// get top if stack is empty
s.peek(&s, &temp);
printf("----- push -----\n"); printf("----- push -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
@ -419,6 +453,18 @@ void stack_test_struct(void)
s.print(&s); s.print(&s);
printf("\n"); printf("\n");
printf("----- clear -----\n");
s.clear(&s);
printf("----- print -----\n");
s.print(&s);
printf("\n");
printf("----- push -----\n");
for (i = 0; i < len; i++)
{
s.push(&s, &data[i]);
}
printf("----- pop -----\n"); printf("----- pop -----\n");
for (i = 0; i < len + 1; i++) for (i = 0; i < len + 1; i++)
{ {
@ -448,5 +494,3 @@ void stack_test(void)
stack_test_char(); stack_test_char();
stack_test_struct(); stack_test_struct();
} }
#endif

View File

@ -34,7 +34,8 @@ struct _stack
void (*print)(struct _stack* s); void (*print)(struct _stack* s);
}; };
bool stack_init(struct _stack* s, uint32_t type_size); bool stack_init(struct _stack* s, uint32_t obj_size); // 链表
bool stack2_init(struct _stack* s, uint32_t obj_size); // 数组
#endif #endif