GithubHelp home page GithubHelp logo

mystack-made-by-class's Introduction

用C++里面的class设计了stack(栈)数据结构,分为用数组设计和用链表设计。

下面是设计思路,最后面的是源代码

设计思路

  • 数组:_top既表示头元素,又能表示元素的数据,但没有元素在栈里时,_top为-1,无法通过下标的方式索引数据,而_top+1的值又可以表示元素
  • 链表:创建Stack的第一个节点创建在栈区,表示其的特殊性,第一个节点的_val存放数据元素的个数_next表示指向下一个数据。而之后的结点都用new开辟到堆区,存放Stack元素。

源码

用数组

#include <iostream>
#include<cassert>
using namespace std;
const int MAX_SIZE = 100;
class Stack
{
	int _vals[MAX_SIZE];
	int _top;

public:
	Stack()
	{
		for (int i = 0; i < MAX_SIZE; ++i)
			_vals[i] = 0;
		_top = -1;//空栈默认-1
	}

	~Stack() {}

	Stack(const Stack& s)
	{
		this->_top = s._top;

		for (int i = 0; i <= s._top; ++i)
			this->_vals[i] = s._vals[i];
	}

	void push(int elem)
	{
		if (this->_top == MAX_SIZE - 1)
		{
			cout << "栈满!" << endl;
			return;
		}
		else
		{
			++this->_top;
			this->_vals[this->_top] = elem;
		}
	}

	void pop()
	{
		if (this->empty())
		{
			cout << "空栈!" << endl;
			return;
		}
		else
		{
			--this->_top;
		}
	}

	int size()
	{
		return this->_top + 1;
	}

	int top()
	{
		if (this->empty())
		{
			cout << "空栈!" << endl;
			assert(0);
		}
		return this->_vals[this->_top];
	}

	bool empty()
	{
		return _top == -1;
	}

	void clear()
	{
		this->_top = -1;
	}
};
int main()
{
	Stack s;
	//**********test here**********
    
    
    //**********end**********
	return 0;
}

用链表(栈链)

#include <iostream>
#include<cassert>
using namespace std;
const int MAX_SIZE = 100;
class Stack
{
	int _val;
	Stack* _next;
	//第一个结点的数据存放元素个数,
	// this->_next指向栈顶(第一个元素)
public:
	Stack()
	{
		_val = 0;
		_next = nullptr;
	}

	~Stack()
	{

	}

	void push(int elem)
	{
		if (this->_val == MAX_SIZE)
		{
			cout << "满栈!" << endl;
			return;
		}
		else
		{
			Stack* p = new Stack;
			p->_val = elem;
			if (this->_val == 0)
			{
				this->_next = p;
				p->_next = nullptr;
				++this->_val;
				return;
			}
			Stack* temp = this->_next;
			this->_next = p;
			p->_next = temp;
			++this->_val;
			return;
		}
	}

	void pop()
	{
		if (this->_val == 0)
		{
			cout << "空栈!" << endl;
			return;
		}
		else
		{
			Stack* p = this->_next;
			this->_next = p->_next;
			delete p;
			--this->_val;
			return;

		}
	}

	int size()
	{
		return this->_val;
	}

	int top()
	{
		if (this->_val == 0)
		{
			cout << "空栈!" << endl;
			assert(0);
		}
		return this->_next->_val;
	}

	bool empty()
	{
		return (this->_val == 0);
	}

	void clear()
	{
		Stack* p = this->_next;
		while (p != nullptr)
		{
			Stack* temp = p;
			p = p->_next;
			delete temp;
			cout << "delete" << endl;
		}
		this->_val = 0;
		this->_next = nullptr;

	}
};
int main()
{
	//**********test**********
	Stack s;

	
	s.clear();
	//**********end**********
	return 0;
}

mystack-made-by-class's People

Contributors

tuning-luna avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.