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;
    }
  • 相关阅读:
    2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum
    Codeforces Round #412 C. Success Rate (rated, Div. 2, base on VK Cup 2017 Round 3)
    2017 中国大学生程序设计竞赛 女生专场 Building Shops (hdu6024)
    51nod 1084 矩阵取数问题 V2
    Power收集
    红色的幻想乡
    Koishi Loves Segments
    Wood Processing
    整数对
    Room and Moor
  • 原文地址:https://www.cnblogs.com/Malphite/p/7737877.html
Copyright © 2011-2022 走看看