zoukankan      html  css  js  c++  java
  • 栈的顺序存储,所谓的顺序栈

    与线性表类似栈也有两种存储结构,即顺序存储结构和链表存储结构。栈的顺序存储结构亦称为顺序栈,它是运算受限的顺序表。

    #pragma once
    
    class stack
    {
    public:
    	stack(void);
    	stack(int maxsize);
    	~stack(void);
    private:
    	int *m_list;
    	int m_maxsize;
    	int m_top;
    public:
    	bool push(int value);
    	bool pop();
    	bool top(int *value);
    	bool empty();
    	bool full();
    	int size();
    	void output();
    };
    
    #include "StdAfx.h"
    #include "stack.h"
    
    
    stack::stack(void)
    {
    	m_maxsize = 10;
    	m_list = new int[m_maxsize];
    	for(int i=0; i<m_maxsize; i++)
    	{
    		m_list[i] = 0xFFFFFFFF;
    	}
    	m_top = -1;
    }
    
    
    stack::stack(int maxsize)
    {
    	m_maxsize = maxsize;
    	m_list = new int[m_maxsize];
    	for(int i=0; i<m_maxsize; i++)
    	{
    		m_list[i] = 0xFFFFFFFF;
    	}
    	m_top = -1;
    }
    
    stack::~stack(void)
    {
    	delete []m_list;
    	m_top = -1;
    	m_list = NULL;
    }
    
    bool stack::push(int value)
    {
    	if (!full())
    	{
    		m_list[m_top] = value;
    		m_top++;
    		return true;
    	}
    	return false;
    }
    
    bool stack::pop()
    {
    	if(!empty())
    	{
    		m_top--;
    		return true;
    	}
    	return false;
    }
    
    bool stack::top(int *value)
    {
    	if (!empty())
    	{
    		*value = m_list[m_top - 1];
    		return true;
    	}
    	*value = 0xFFFFFFFF;
    	return false;
    }
    
    bool stack::empty()
    {
    	if (m_top == -1)
    	{
    		return true;
    	}
    	return false;
    }
    
    bool stack::full()
    {
    	if (m_top + 1 == m_maxsize)
    	{
    		return true;
    	}
    	return false;
    }
    
    int stack::size()
    {
    	return m_top + 1;
    }
    
    void stack::output()
    {
    	printf("---------------------------------
    ");
    	printf("stack information:
    ");
    	printf("maxsize=%d
    ", m_maxsize);
    	printf("top=%d
    ", m_top);
    	for(int i=0; i<m_top; i++)
    	{
    		printf("index%d=%d
    ", i, m_list[i]);
    	}
    }
    


     

    主函数测试:

    // sequstack.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "stack.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	stack mystack(10);
    
    	//入栈 0 --- 9
    	/*for(int i=0; i<10; i++)
    	{
    		mystack.push(i);
    	}
    	
    	if (mystack.full()) printf("stack is full
    ");
    	printf("stack size=%d
    ", mystack.size());
    
    	//出栈
    	for(int i=0; i<10; i++)
    	{
    		int value = 0;
    		mystack.top(&value);
    		mystack.pop();
    		printf("pop value=%d
    ", value);
    	}
    	if (mystack.empty()) printf("stack is empty
    ");
    	printf("stack size=%d
    ", mystack.size());*/
    
    	mystack.push(8);
    	mystack.push(9);
    	mystack.push(10);
    	mystack.push(11);
    	
    	printf("size=%d
    ", mystack.size());
    	int count = mystack.size();
    
    	for(int i=0; i<count; i++)
    	{
    		int value = 0;
    		mystack.top(&value);
    		printf("value = %d
    ", value);
    		mystack.pop();
    	}
    
    	getchar();
    	return 0;
    }
    
    


  • 相关阅读:
    非诚勿扰骆琦攻略
    IT服务者的困惑与解决之道
    某某银行IT运维管理的三点和四化
    提升CIO地位及IT价值体现,IT治理理念在中国势在必行
    振兴民族软件,险恶的江湖该如何仗剑走天涯
    某连锁饭店IT服务台、自助服务建设
    证券行业ITIL初探助力券商成就IT管理之路
    分享屡见成效的另类方法论保障ITIL软件及ITSM方案落地实施
    城市商业银行IT科技工作管理之痛
    【转】陈天晴:信息化发展要注意规划调整 重视IT治理
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3181635.html
Copyright © 2011-2022 走看看