zoukankan      html  css  js  c++  java
  • Demo

    顺序栈

    #include <iostream>
    using namespace std;
    
    #define MAXSIZE 100
    typedef int SElemType;
    
    typedef struct
    {
    	SElemType* base;
    	SElemType* top;
    	int stacksize;
    }Sqstack;
    
    int InitStack(Sqstack& S)
    {
    	//初始化base作为数组控件存放数据,top指向第一个元素上面(当前初始化为空栈)
    	S.base = new SElemType[MAXSIZE];
    	if (!S.base) return 0;
    	S.top = S.base;
    	S.stacksize = MAXSIZE;
    	return 1;
    }
    
    int Push(Sqstack& S, SElemType e)
    {
    	if (S.top - S.base == S.stacksize)return 0;
    	*(S.top++) = e;
    	return 1;
    }
    
    int Pop(Sqstack& S, SElemType& e)
    {
    	if (S.top == S.base)return 0;
    	e = *(--S.top);
    	return 1;
    }
    
    SElemType GetTop(Sqstack S)
    {
    	if (S.top != S.base)
    		return *(S.top - 1);
    	return 0;
    }
    
    int main()
    {
    	Sqstack S;
    	SElemType e;
    	InitStack(S);
    	int n;
    	cout << "请输入入栈个数: ";
    	cin >> n;
    	cout << "请依次输入入栈的数据: " << endl;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> e;
    		Push(S, e);
    	}
    	cout << "GetTop: ";
    	cout << GetTop(S) << endl;
    	cout << "Pop: " << endl;
    	for (int i = 0; i < n; i++)
    	{
    		Pop(S, e);
    		cout << e << endl;
    	}
    	return 0;
    }
    

    链栈

    #include <iostream>
    using namespace std;
    
    #define MAXSIZE 100
    typedef int SElemType;
    
    typedef struct StackNode
    {
    	SElemType data;
    	struct StackNode* next;
    }StackNode, * LinkStack;
    
    int InitStack(LinkStack& S)
    {
    	S = NULL;
    	return 1;
    }
    
    int Push(LinkStack& S, SElemType e)
    {
    	//每次入栈的结点指向前一个结点
    	LinkStack p = new StackNode;
    	p->data = e;
    	p->next = S;
    	S = p;
    	return 1;
    }
    //删除S的栈顶元素,用e返回其值
    int Pop(LinkStack& S, SElemType& e)
    {
    	if (S == NULL)return 0;
    	e = S->data;
    	LinkStack p = S;	//用p临时保存栈顶元素空间,以备释放
    	S = S->next;
    	delete p;
    	return 1;
    }
    
    SElemType GetTop(LinkStack S)
    {
    	if (S != NULL)
    		return S->data;
    	return 0;
    }
    
    int main()
    {
    	LinkStack S;
    	SElemType e;
    	InitStack(S);
    	int n;
    	cout << "请输入入栈个数: ";
    	cin >> n;
    	cout << "请依次输入入栈的数据: " << endl;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> e;
    		Push(S, e);
    	}
    	cout << "GetTop: ";
    	cout << GetTop(S) << endl;
    	cout << "Pop: " << endl;
    	for (int i = 0; i < n; i++)
    	{
    		Pop(S, e);
    		cout << e << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    动态代理练习2全站压缩流输出[response动态代理]
    动态代理练习3自定义数据库连接池[connection动态代理]
    类加载器
    Mysql中的数据类型对应于Java中什么数据类型
    使用网上流传的一个数据库连接池在Proxy.newProxyInstance处引起 java.lang.ClassCastException 问题的解决方法
    动态代理练习1全站字符编码过滤[request动态代理]
    用cocos2dx将helloworld实现出来了
    多线程的自动管理(线程池)
    互斥对象
    多线程的自动管理(定时器)
  • 原文地址:https://www.cnblogs.com/kongw/p/13968714.html
Copyright © 2011-2022 走看看