|
发表于 2022-4-3 22:35:34
|
显示全部楼层
本楼为最佳答案
你不应该写一个栈的初始化函数来初始化两个不同用途的结构体
你应该写一个通用的栈,让这个栈可以保存任意的数据
像下面这样的
stack.h#ifndef _STACK_H_
#define _STACK_H_
#include <stddef.h>
#include <stdbool.h>
typedef struct stack_node_tag {
struct stack_node_tag *next;
void *data;
size_t size;
} stack_node_t;
typedef struct {
stack_node_t *head;
size_t size;
} stack_t;
stack_t *stack_init(void);
void stack_deinit(stack_t *stack);
void stack_clean(stack_t *stack);
bool stack_push(stack_t *stack, const void *data, size_t size);
bool stack_pop(stack_t *stack);
bool stack_top(const stack_t *stack, void *data, size_t size);
bool stack_empty(const stack_t *stack);
size_t stack_size(const stack_t *stack);
#endif
stack.c#include "stack.h"
#include <stdlib.h>
#include <string.h>
stack_t *stack_init(void) {
stack_t *stack = malloc(sizeof(*stack));
if(!stack) return NULL;
stack->head = NULL;
stack->size = 0;
return stack;
}
void stack_deinit(stack_t *stack) {
if(!stack) return;
stack_clean(stack);
free(stack);
}
void stack_clean(stack_t *stack) {
if(!stack) return;
while(!stack_empty(stack)) stack_pop(stack);
}
bool stack_push(stack_t *stack, const void *data, size_t size) {
if(!stack) return false;
if(!data) return false;
stack_node_t *node = malloc(sizeof(*node));
if(!node) return false;
node->data = malloc(size);
if(!node->data) {free(node); return false;}
memcpy(node->data, data, size);
node->size = size;
node->next = stack->head;
stack->head = node;
++stack->size;
return true;
}
bool stack_pop(stack_t *stack) {
if(!stack) return false;
if(stack_empty(stack)) return false;
stack_node_t *temp = stack->head;
stack->head = temp->next;
free(temp->data);
free(temp);
--stack->size;
return true;
}
bool stack_top(const stack_t *stack, void *data, size_t size) {
if(!stack) return false;
if(!data) return false;
if(size < stack->head->size) return false;
memcpy(data, stack->head->data, stack->head->size);
return true;
}
bool stack_empty(const stack_t *stack) {
if(!stack) return true;
return stack->size == 0;
}
size_t stack_size(const stack_t *stack) {
if(!stack) return 0;
return stack->size;
}
main.c#include "stack.h"
#include <stdio.h>
#include <stdint.h>
typedef struct {
long x, y;
} point_t;
typedef struct {
const char *name;
size_t age;
char sex;
const char *addr;
} student_t;
int main(void) {
stack_t *stack = stack_init();
{
int num = 1234;
stack_push(stack, &num, sizeof(num));
double pi = 3.14;
stack_push(stack, &pi, sizeof(pi));
point_t point = {1920, 1080};
stack_push(stack, &point, sizeof(point));
student_t student = {
"胡图图",
3,
'M',
"翻斗大街翻斗花园二号楼1001室(10楼)"
};
stack_push(stack, &student, sizeof(student));
char hello[] = "hello world!";
stack_push(stack, &hello, sizeof(hello));
}
{
char hello[13];
stack_top(stack, &hello, sizeof(hello));
stack_pop(stack);
puts(hello);
student_t student;
stack_top(stack, &student, sizeof(student));
stack_pop(stack);
printf("%s, %lu, %c, %s\n", student.name, student.age, student.sex, student.addr);
point_t point;
stack_top(stack, &point, sizeof(point));
stack_pop(stack);
printf("%ld, %ld\n", point.x, point.y);
double pi;
stack_top(stack, &pi, sizeof(pi));
stack_pop(stack);
printf("%lf\n", pi);
int num;
stack_top(stack, &num, sizeof(num));
stack_pop(stack);
printf("%d\n", num);
}
stack_deinit(stack);
{
stack_t *sa = stack_init();
stack_t *sb = stack_init();
for(char i = '0'; i <= '9'; ++i) {
stack_push(sa, &i, sizeof(i));
}
for(double i = 3.14; i <= 100; i += 10) {
stack_push(sb, &i, sizeof(i));
}
while(!stack_empty(sa)) {
char ch;
stack_top(sa, &ch, sizeof(ch));
stack_pop(sa);
printf("%c ", ch);
}
puts("");
while(!stack_empty(sb)) {
double d;
stack_top(sb, &d, sizeof(d));
stack_pop(sb);
printf("%lf ", d);
}
puts("");
stack_deinit(sb);
stack_deinit(sa);
}
return 0;
}
|
|