C语言栈的顺序存储结构

#include 
#include 
#define MAX_SIZE 40

typedef struct Stack{
    void* value[MAX_SIZE];
    int Size;
}SeqStack;

typedef struct node
{
    int id;
    int age;
    char name[20];
}nodes;


typedef void* SStack;

SStack Stack_Init();
void Stack_Push(SStack Seq_head, SStack value_in);
void Stack_Pop(SStack);
void Stack_Top(SStack Seq_Head, void (*My_printf)(void*));
void my_printf1(void* node_in);
void Len_stack(SStack Seq_head);
void Destroy_stack(SStack Seq_head);

int main() {
    
    nodes n1 = {1, 21, "花木兰"};
    nodes n2 = {2, 21, "凯皇"};
    nodes n3 = {3, 21, "百里守约"};
    nodes n4 = {4, 19, "百里玄策"};
    nodes n5 = {5, 22, "苏烈"};
    nodes n6 = {6, 20, "李信"};

    printf("---开始创建栈---\n");
    SStack Stack_head = Stack_Init();
    printf("---栈创建完毕---\n");
    
    printf("---开始入栈--\n");
    Stack_Push(Stack_head, &n1);
    Stack_Top(Stack_head, my_printf1);
    Stack_Push(Stack_head, &n2);
    Stack_Top(Stack_head, my_printf1);
    Stack_Push(Stack_head, &n3);
    Stack_Top(Stack_head, my_printf1);
    Stack_Push(Stack_head, &n4);
    Stack_Top(Stack_head, my_printf1);
    Stack_Push(Stack_head, &n5);
    Stack_Top(Stack_head, my_printf1);
    Stack_Push(Stack_head, &n6);
    Stack_Top(Stack_head, my_printf1);
    printf("---入栈完毕---\n");
    Len_stack(Stack_head);


    printf("---开始出栈-\n");
    Stack_Top(Stack_head, my_printf1);
    Stack_Pop(Stack_head);
    Stack_Top(Stack_head, my_printf1);
    Stack_Pop(Stack_head);
    Stack_Top(Stack_head, my_printf1);
    Stack_Pop(Stack_head);
    Stack_Top(Stack_head, my_printf1);
    Stack_Pop(Stack_head);
    Stack_Top(Stack_head, my_printf1);
    Stack_Pop(Stack_head);
    Stack_Top(Stack_head, my_printf1);
    Stack_Pop(Stack_head);

    printf("---出栈完毕---\n");
    Len_stack(Stack_head);


    printf("---开始销毁栈---\n");
    Destroy_stack(Stack_head);
    Stack_head = NULL;
    printf("---销毁栈完毕---\n");

    system("pause");
    return 0;
}

SStack Stack_Init() {
    SeqStack* My_Stack = (SeqStack*)malloc(sizeof(SeqStack));
    if(My_Stack == NULL)
        return NULL;
    My_Stack -> Size = 0;
    return My_Stack;
}

void Stack_Push(SStack Seq_Head, SStack value_in) {
    if(Seq_Head == NULL)
        return;
    SeqStack* My_Stack = (SeqStack*)Seq_Head;
    if(My_Stack -> Size >= MAX_SIZE)
        return;
    My_Stack -> value[ My_Stack -> Size] = value_in;
    My_Stack -> Size ++;
}

void Stack_Pop(SStack Seq_Head) {
    if(Seq_Head == NULL)
        return;
    SeqStack* My_stack = (SeqStack*)Seq_Head;
    My_stack -> value[My_stack -> Size - 1] = NULL;
    My_stack -> Size --;
}

void Stack_Top(SStack Seq_Head, void (*My_printf)(void*)) {
    if(Seq_Head == NULL)
        return;
    SeqStack* My_stack = (SeqStack*)Seq_Head;
    if(My_stack -> Size == 0)
        return;
    My_printf(My_stack -> value[My_stack -> Size - 1]);
}

void my_printf1(void* node_in) {
    nodes* p_node = (nodes*)node_in;
    printf("ID:%d 年龄:%d 名字:%s\n", p_node -> id, p_node -> age, p_node -> name);
}

void Len_stack(SStack Seq_Head) {
    if(Seq_Head == NULL)
        return;
    SeqStack *My_stack = (SeqStack*) Seq_Head;
    printf("栈的长度大小为:%d\n", My_stack -> Size);
}

void Destroy_stack(SStack Seq_Head) {
    if(Seq_Head == NULL)
        return;
    SeqStack *My_stack = (SeqStack*) Seq_Head;
    free(My_stack);
    My_stack = NULL;
}

你可能感兴趣的:(C)