zoukankan      html  css  js  c++  java
  • 栈 c实现

    栈的数组实现

    stack.h

    #ifndef _STACK_
    #define _STACK_
    
    #define SIZE 100
    
    typedef int data_t;
    
    typedef struct head{
        data_t data[SIZE];
        int top;
    }stack_t;
    
    stack_t *stack_create();
    
    int stack_is_empty(stack_t *head);
    int stack_is_full(stack_t *head);
    void stack_clear(stack_t *head);
    
    int stack_push(stack_t *head, data_t data);
    data_t stack_pop(stack_t *head);
    data_t stack_get_top(stack_t *head);
    
    void stack_show(stack_t *head);
    void stack_destory(stack_t **head);
    
    #endif

    stack.c

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <strings.h>
    
    #include "sqstack.h"
    
    stack_t *stack_create()
    {
        stack_t *head = (stack_t *)malloc(sizeof(stack_t));
        if (head == NULL) {
            printf("malloc failed!
    ");
            return NULL;
        }
        bzero(head, sizeof(stack_t));
    
        head->top = -1;
    
        return head;
    }
    
    int stack_is_empty(stack_t *head)
    {
        return head->top == -1;
    }
    
    int stack_is_full(stack_t *head)
    {
        return head->top == SIZE - 1;
    }
    
    void stack_clear(stack_t *head)
    {
        head->top = -1;
    }
    
    int stack_push(stack_t *head, data_t data)
    {
        if (stack_is_full(head)) {
            printf("stack is full!
    ");
            return -1;
        }
    
        head->data[head->top+1] = data;
        head->top++;
    
        return 0;
    }
    
    data_t stack_pop(stack_t *head)
    {
        if (stack_is_empty(head)) {
            printf("stack is empty!
    ");
            return -1;
        }
    
        data_t data = head->data[head->top];
        head->top--;
    
        return data;
    }
    
    data_t stack_get_top(stack_t *head)
    {
        if (stack_is_empty(head)) {
            printf("stack is empty!
    ");
            return -1;
        }
        
        return head->data[head->top];
    }
    
    void stack_show(stack_t *head)
    {
        int i;
        for (i = head->top; i >= 0; i--) {
            printf("%d, ", head->data[i]);
        }
        printf("
    ");
    }
    
    void stack_destory(stack_t **head)
    {
        free(*head);
        *head = NULL;
    }

    main.c

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <strings.h>
    
    #include "sqstack.h"
    
    int main()
    {
        stack_t *head = stack_create();
    
        int n = 10;
        while (n--) {
            stack_push(head, n+1);
        }
        stack_show(head);
    
        int i;
        for (i = 0; i < 10; i++) {
            printf("%d, ", stack_get_top(head));
            printf("%d
    ", stack_pop(head));
        }
        stack_show(head);
    
        stack_destory(&head);
    
        return 0;
    }

     栈的链表实现

    lstack.h

    #ifndef _STACK_
    #define _STACK_
    
    typedef int data_t;
    
    typedef struct node{
        data_t data;
        struct node *next;
    }NODE;
    
    NODE *stack_create();
    
    int stack_is_empty(NODE *head);
    int stack_is_full(NODE *head);
    
    int stack_length(NODE *head);
    void stack_clear(NODE *head);
    
    int stack_push(NODE *head, data_t data);
    data_t stack_pop(NODE *head);
    data_t stack_get_top(NODE *head);
    
    void stack_show(NODE *head);
    void stack_destory(NODE **head);
    
    #endif

    lstack.c

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <strings.h>
    
    #include "lstack.h"
    
    NODE *stack_create()
    {
        NODE *head = (NODE *)malloc(sizeof(NODE));
        if (NULL == head) {
            printf("malloc failed!
    ");
            return NULL;
        }
        bzero(head, sizeof(NODE));
    
        head->data = -1;
        head->next = NULL;
    
        return head;
    }
    
    int stack_is_empty(NODE *head)
    {
        return head->next == NULL;
    }
    
    int stack_is_full(NODE *head)
    {
        return 0;
    }
    
    int stack_length(NODE *head)
    {
        NODE *p = head->next;
        int len = 0;
    
        while (NULL != p) {
            len++;
            p = p->next;
        }
    
        return len;
    }
    
    void stack_clear(NODE *head)
    {
        NODE *p = head->next;
        NODE *q = NULL;
    
        while (NULL != p)  {
            q = p->next;
            free(p);
            p = q;
        }
    
        head->next = NULL;
    }
    
    int stack_push(NODE *head, data_t data)
    {
        NODE *p = head;
        NODE *q = (NODE *)malloc(sizeof(NODE));
        if (NULL == q) {
            printf("malloc failed!
    ");
            return -1;
        }
        q->data = data;
        q->next = NULL;
    
        q->next = p->next;
        p->next = q;
    
        return 0;
    }
    
    data_t stack_pop(NODE *head)
    {
        if (stack_is_empty(head)) {
            printf("list is empty!
    ");
            return -1;
        }
    
        data_t data = head->next->data;
    
        NODE *p = head;
        NODE *q = head->next;
        p->next = q->next;
        free(q);
        q = NULL;
        
        return data;
    }
    
    data_t stack_get_top(NODE *head)
    {
        if (stack_is_empty(head)) {
            printf("list is empty!
    ");
            return -1;
        }
        
        return head->next->data;
    }
    
    void stack_show(NODE *head)
    {
        NODE *p = head->next;
    
        while (NULL != p) {
            printf("%d, ", p->data);
            p = p->next;
        }
        printf("
    ");
    }
    
    void stack_destory(NODE **head)
    {
        stack_clear(*head);
    
        free(*head);
        *head = NULL;
    }

    main.c

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <strings.h>
    
    #include "lstack.h"
    
    int main()
    {
        NODE *head = stack_create();
    
        if (NULL == head) {
            printf("create failed!
    ");
            return -1;
        }
    
        int n = 10;
        while (n--) {
            if (-1 == stack_push(head, n+1)) {
                printf("insert failed!
    ");
                break;
            }
        }
        
        stack_show(head);
    
        int i;
        for (i = 0; i < 10; i++) {
            printf("%d, ", stack_get_top(head));
            printf("%d
    ", stack_pop(head));
        }
    
        stack_destory(&head);
    
        return 0;
    }
  • 相关阅读:
    LODOP中用ADD_PRINT_IMAGE缩放非图片超文本
    LODOP关联,打印项序号注意事项
    LODOP在页面让客户选择打印机
    【JS新手教程】LODOP打印复选框选中的任务或页数
    映美FP-530K+打印发票的各种经验
    【JS新手教程】LODOP打印复选框选中的内容
    LODOP和C-LODOP注册与角色等简短问答【增强版】
    【JS新手教程】弹出两层div,及在LODOP内嵌上层
    LODOP内嵌挡住浏览器的div弹出层
    【JS新手教程】浏览器弹出div层1
  • 原文地址:https://www.cnblogs.com/Malphite/p/7737877.html
Copyright © 2011-2022 走看看