0721学习笔记

数据结构-代码(待完善)

  1. 栈的定义

    1. stack.h
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #define STATIC_STACK
    #define DYNAMIC_STACK
    #define DATATYPE void *
    
    #ifdef STATIC_STACK
    
    #define STACK_H 100
    typedef struct StaticStack
    {
        DATATYPE stack[STACK_H];
        int max_depth;
        int top;
    } StaticStack;
    
    bool initialize_static_stack(StaticStack * s);
    bool static_stack_push(StaticStack *s, DATATYPE value);
    bool static_stack_pop(StaticStack *s, DATATYPE *value);
    bool static_stack_top(StaticStack *s, DATATYPE *value);
    #endif
    
    #ifdef DYNAMIC_STACK
    
    typedef struct DynamicStack
    {
        DATATYPE data;
        DynamicStack *next;
    } DynamicStack;
    
    //returns a top-pointer to a new dynamic stack
    DynamicStack *initialize_dynamic_stack();
    bool dynamic_stack_push(DynamicStack *top, DATATYPE value);
    bool dynamic_stack_pop(DynamicStack *top, DATATYPE *value);
    bool dynamic_stack_top(DynamicStack *top, DATATYPE *value);
    #endif
    
    1. stack.c
    
    #include "stack.h"
    #ifdef STATIC_STACK
    
    bool initialize_static_stack(StaticStack * s)
    {
        s->top = -1;
        s->max_depth = STACK_H; // Initialize depth to 0
        return true;
    }
    bool static_stack_push(StaticStack *s, DATATYPE value)
    {
        if (s->top >= s->max_depth - 1) {
            printf("Static stack overflow\n");
            return false; // Stack is full
        }
        s->stack[++(s->top)] = value;
        return true;
    }
    bool static_stack_pop(StaticStack *s, DATATYPE *value)
    {
        if (s->top < 0) {
            printf("Static stack underflow\n");
            return false; // Stack is empty
        }
        *value = s->stack[(s->top)--];
        return true;
    }
    bool static_stack_top(StaticStack *s, DATATYPE *value)
    {
        if (s->top < 0) {
            printf("Static stack is empty\n");
            return false; // Stack is empty
        }
        *value = s->stack[s->top]; // Get the top value
        return true; // Successfully retrieved the top value
    }
    #endif
    
    #ifdef DYNAMIC_STACK
    DynamicStack *initialize_dynamic_stack()
    {
       DynamicStack *new_stack = (DynamicStack *)malloc(sizeof(DynamicStack));
       if(new_stack == NULL) {
           return NULL; // Memory allocation failed
       }else {
           new_stack->next = NULL;
           return new_stack;
       }
    }
    bool dynamic_stack_push(DynamicStack *top, DATATYPE value)
    {
        DynamicStack *new_item = (DynamicStack *)malloc(sizeof(DynamicStack));
        if(!new_item) {
            printf("Failed to push %d onto dynamic stack\n", value);
            return false; // Memory allocation failed
        }
        new_item->data = value;
        new_item->next = top->next;
        top->next = new_item;
        return true;
    }
    
    bool dynamic_stack_pop(DynamicStack *top, DATATYPE *value)
    {
        if(top->next == NULL) {
            printf("Dynamic stack is empty\n");
            return false; // Stack is empty
        }
        DynamicStack *temp = top->next;
        *value = temp->data;
        top->next = temp->next;
        free(temp);
        return true;
    
    }
    bool dynamic_stack_top(DynamicStack *top, DATATYPE *value)
    {
        if(top->next == NULL) {
            printf("Dynamic stack is empty\n");
            return false; // Stack is empty
        }
    
        *value = top->next->data; // Get the data from the top item
        return true; // Successfully retrieved the top value
    }                                               
    #endif