zoukankan      html  css  js  c++  java
  • 用c和c++的方式实现栈

    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    struct LinkNode
    {
        int data;
        struct LinkNode* next;
    };
    
    struct Stack
    {
        struct LinkNode* head;
        int size;
    };
    
    void StackInit(struct Stack* stack)
    {
        stack->head = NULL;
        stack->size = 0;
    }
    
    void StackPush(struct Stack* stack, const int num)
    {
        struct LinkNode* node = (struct LinkNode*)malloc(sizeof(struct LinkNode));
        assert(node != NULL);
        node->data = num;
        node->next = stack->head;
        stack->head = node;
        ++stack->size;
        return;
    }
    
    int StackEmpty(struct Stack* stack)
    {
        return (stack->size == 0);
    }
    
    int StackPop(struct Stack* stack, int* num)
    {
        if (StackEmpty(stack) == 1)
            return 0;
    
        *num = stack->head->data;
        struct LinkNode* tmp;
        tmp = stack->head;
        stack->head = stack->head->next;
        --stack->size;
        free(tmp);
        return 1;
    }
    
    void StackCleanup(struct Stack* stack)
    {
        struct LinkNode* tmp;
        while (stack->head != NULL)
        {
            tmp = stack->head;
            stack->head = stack->head->next;
            free(tmp);
        }
    
        stack->size = 0;
        return;
    }
    
    int main(void)
    {
        struct Stack stack;
        StackInit(&stack);
        int i;
        for (i = 0; i < 5; i++)
        {
            StackPush(&stack, i);
        }
    
        while (!StackEmpty(&stack))
        {
            StackPop(&stack, &i);
            printf("%d ", i);
        }
    
        printf("
    ");
        return 0;
    }
    #include <iostream>
    using namespace std;
    
    class Stack
    {
        struct Link
        {
            int data_;
            Link* next_;
            Link(int data, Link* next) :data_(data), next_(next)
            {
    
            }
        };
    
    public:
        Stack() :head_(0), size_(0)
        {
    
        }
    
        ~Stack()
        {
            Link* tmp;
            while (head_ != NULL)
            {
                tmp = head_;
                head_ = head_->next_;
                delete tmp;
            }
    
            return;
        }
    
        void Push(const int num)
        {
            Link* node = new Link(num, head_);
            head_ = node;
            ++size_;
            return;
        }
    
        bool Empty()
        {
            return (size_ == 0);
        }
    
        bool Pop(int& num)
        {
            if (Empty() == 1)
                return false;
    
            num = head_->data_;
            Link* tmp = head_;
            head_ = head_->next_;
            --size_;
            delete tmp;
            return true;
        }
    
    private:
        Link *head_;
        int size_;
    };
    
    int main(void)
    {
        Stack stack;
        int i;
        for (i = 0; i < 5; i++)
        {
            stack.Push(i);
        }
    
        while (!stack.Empty())
        {
            stack.Pop(i);
            cout<<i<<" ";
        }
    
        cout<<endl;
        return 0;
    }
  • 相关阅读:
    Python conda 入门
    EmEditor
    LInux 阿里云系统遇到挖矿程序
    Git 全局配置查看修改
    chrome下input文本框自动填充背景问题解决
    织梦调用当前位置 修改当前位置间隔符号
    dedecms调用文章内容
    dedecms文章页调用上一篇和下一篇文章
    dedecms在任意页面调用任意栏目文章
    dedecms判断当前页面是否为首页 织梦设置首页高亮
  • 原文地址:https://www.cnblogs.com/lulu10922/p/5822271.html
Copyright © 2011-2022 走看看