zoukankan      html  css  js  c++  java
  • 栈的c语言实现

    C语言实现代码
    #include<iostream>
    using namespace std;
    
    #define STACK_INIT_SIZE 10
    #define STACKINCREMENT 10
    #define ElemType int
    
    typedef struct
    {
    	ElemType *base;
    	int top;
    	size_t capacity;
    }SqStack;
    
    bool IsFull(SqStack *st)
    {
    	return st->top >= st->capacity;
    }
    bool IsEmpty(SqStack *st)
    {
    	return st->top == 0;
    }
    bool InitStack(SqStack *st)
    {
    	st->base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));
    	st->top = 0;
    	st->capacity = STACK_INIT_SIZE;
    	return true;
    }
    ElemType GetTop(SqStack *st)
    {
    	if(!IsEmpty(st))
    	{
    		return st->top;
    	}
    	cout<<"stack empty!"<<endl;
    	return -1;
    }
    void Push(SqStack *st, ElemType item)
    {
    	if(!IsFull(st))
    	{
    		st->base[st->top++] = item;
    	}
    	else
    	{
    		cout<<"stack full!"<<endl;
    	}
    }
    void Show(SqStack *st)
    {
    	for(int i=st->top-1; i>=0; --i)
    	{
    		cout<<st->base[i]<<" ";
    	}
    	cout<<endl;	
    }
    ElemType Pop(SqStack *st)
    {
    	if(!IsEmpty(st))
    	{
    		st->top--;
    		return st->base[st->top];	
    	}
    	return -1;
    }
    
    int main()
    {
    	SqStack st;
    	InitStack(&st);
    	int i = 0;
    	for(; i<5; ++i)
    		Push(&st,i);
    	Pop(&st);
    	Show(&st);
    	return 1;
    }



    c++实现代码:

    #include<iostream>
    using namespace std;
    
    #define ElemType int
    #define STACK_INIT_SIZE 10
    template<class _Ty>
    class stack
    {
    public:
    	stack():top(-1),capacity(STACK_INIT_SIZE)
    	{
    		base = new ElemType[capacity];
    	}
    	~stack()
    	{
    		delete []base;
    	}
    bool IsEmpty()
    {
    	return top == -1;
    }
    bool IsFull()
    {
    	return top == capacity-1;
    }
    bool Push(_Ty item)
    {
    	if(!IsFull())
    	{
    		base[++top] = item;
    		return true;
    	}
    	cout<<"stack full! increment base..."<<endl;
    	return false;
    }
    bool Pop()
    {
    	if(!IsEmpty())
    	{
    		base[top--];
    		return true;
    	}
    	return false;
    }
    _Ty GetTop()
    {
    	if(!IsEmpty())
    	{
    		return base[top];
    	}
    	return -1;
    }
    int Show()
    {
    	int i =top;
    	for(; i>=0; --i)
    		cout<<base[i]<<" ";
    	cout<<endl;
    	return 1;
    }
    private:
    	ElemType *base;
    	int top;
    	size_t capacity;
    };
    
    int main()
    {
    	stack<int> st;
    	int i = 0;
    	for(; i<5; ++i)
    		st.Push(i);
    	st.Pop();
    	st.Show();
    	cout<<st.GetTop()<<endl;
    	return 1;
    }
    


  • 相关阅读:
    IOS技能点之Foundation之NSString
    JavaScript学习笔记 -- ES6学习(二) let 和const
    JavaScript 学习笔记-- ES6学习(一)介绍以及Babel的使用
    JavaScript 学习笔记: 扩充类型的功能
    PHP学习笔记(八)
    PHP学习笔记(六)
    Less 官方文档学习笔记
    PHP学习笔记(五)
    PHP 学习笔记 (四)
    PHP 学习笔记 (三)
  • 原文地址:https://www.cnblogs.com/llguanli/p/7262463.html
Copyright © 2011-2022 走看看