DS006-Principle of the stack-Linear table with restricted operation-C++ STL stack template stack

This article first introduces the principle of sequential stack and chain stack, and then introduces the simple usage of C++ STL stack template.

1. Why the term stack is introduced

In some applications, the processed data has the following characteristics:

1. It has a linear structure and is stored in a linear table

2. However, only simple operations are needed: insert and delete only at one end of the linear table.

Of course, you can use the sequence list and linked list previously implemented, but only some of the operations are used.

The predecessors who have good deeds chose a name for it, called "stack", with one more term and one more burden.

There are two ways to implement linear tables, sequential tables and linked tables

There are also two corresponding stacks.

Second, the sequential stack

This is about to analyze, use the sequence table as the stack, at which end should insert and delete?

Obviously, inserting and deleting at the end of the subscript does not need to move other data, which is efficient

Therefore, the insertion operation (push into the stack) of the sequential stack is to insert at the end of the table, and the delete operation (pull) of the stack is to delete at the end of the table.



#include "stdafx.h"

#include<iostream> 
using namespace std;
//模板类,T是泛化类型
template<typename T>class stack
{//顺序栈
	T *base;//连续空间的首地址,栈底
	int listsize;//连续空间的大小
	int length;//实际元素的个数
	T *top;//栈顶
public:
	stack()
	{//构造函数
		init();
	}
	void init()
	{//对栈进行初始化
		base = new T[16];//初始时,占用16个空间,这个可以自己设置,一般是2的n次方
		listsize = 16;
		length = 0;
		top = base;
	}
	int enlarge()
	{//当L的空间不足时,本函数功能:将L的空间翻倍
	 //如果成功返回1,失败返回0

		T *newbase = new T[listsize * 2];//开辟新空间
		if (0 == newbase)
			return 0;
		for (int i = 0; i<length; i++)//把老空间已有数据拷贝到新空间
			newbase[i] = base[i];
		delete[]base;//释放掉老空间,还给操作系统
		base = newbase;//elem指向新空间首地址
		listsize = listsize * 2;//空间大小翻倍
		top = base + length;
		return 1;

	}
	void push(T x)
	{//入栈,即在顺序表尾部插入数据
		if (length == listsize)
		{//空间不足 
			enlarge();
		}

		*top = x;
		top++;
		length++;
	}

	void pop()
	{//出栈,删除顺序表表尾元素
		top--;
		length--;

	}
	int size()
	{//得到栈中元素个数
		return length;
	}
	int isEmpty()
	{//如果栈为空返回1,否则返回0
		return length == 0;
	}

	T getTop()
	{//得到栈顶元素
		return *(top - 1);

	}
	void print()
	{//测试用
		T *p = base;
		while (p < top)
		{
			cout << *p << "  ";
			p++;
		}
		cout << endl;

	}
	
	~stack()
	{
		delete[]base;
	}
};

int main()
{
	stack<int> s;

	s.push(1);
	s.push(3);
	s.push(5);
	s.print();

	int x;
	x = s.getTop();
	cout << "the top of stack is:" << x << endl;

	s.pop();
	s.print();
	s.pop();
	s.print();


	return 0;
}


Three, chain stack

Using the linked list as the stack, at which end should we insert and delete?

1. When using a singly linked list, it is convenient to insert and delete at the head.

2. When using a one-way circular linked list, it is convenient to insert and delete at the head;

3. It is more convenient when using a two-way circular linked list.

Therefore, in terms of complexity, it is simpler to implement a stack with a singly linked list.



#include "stdafx.h"

#include<iostream> 
using namespace std;
//模板类,T是泛化类型
template<typename T>class stack
{//基于单链表的栈
	struct node
	{
		T data;
		node *next;
	};
	node *head;//头结点指针	
	int length;//实际元素的个数
	
public:
	stack()
	{//构造函数
		init();
	}
	void init()
	{//对栈进行初始化
		head = new node;
		head->next = 0;
		length = 0;
	}
	
	void push(T x)
	{//入栈,即在单链表头部插入数据
		node *t;
		t = new node;
		t->data = x;
		t->next = head->next;
		head->next = t;
		
		length++;
	}

	void pop()
	{//出栈,删除单链表表头元素
		node *t;
		t = head->next;
		head->next = t->next;
		delete t;
		length--;

	}
	int size()
	{//得到栈中元素个数
		return length;
	}
	int isEmpty()
	{//如果栈为空返回1,否则返回0
		return length == 0;
	}

	T getTop()
	{//得到栈顶元素
		node *t = head->next;
		return t->data;

	}
	void print()
	{//测试用
		node *p = head->next;
		while (p)
		{
			cout << p->data << "  ";
			p=p->next;
		}
		cout << endl;

	}
	
	~stack()
	{
		delete[]head;
	}
};

int main()
{
	stack<int> s;

	s.push(1);
	s.push(3);
	s.push(5);
	s.print();

	int x;
	x = s.getTop();
	cout << "the top of stack is:" << x << endl;

	s.pop();
	s.print();
	s.pop();
	s.print();


	return 0;
}


Four, C++ STL stack

The stack template class has been defined in C++ STL.

We can use it directly in the future.



#include "stdafx.h"

#include<stack>//C++ STL 栈模板头文件
#include<iostream> 
using namespace std;

template<typename T>

int main()
{
	stack<int> s;//定义一个整数栈
	int x;
	
	s.push(1);//入栈
	x = s.top();//得到栈顶元素
	cout << "the top of stack is:" << x << endl;

	s.push(3);
	x = s.top();
	cout << "the top of stack is:" << x << endl;

	s.push(5);
	x = s.top();
	cout << "the top of stack is:" << x << endl;
	
	

	s.pop();//出栈
	x = s.top();
	cout << "the top of stack is:" << x << endl;
	s.pop();
	x = s.top();
	cout << "the top of stack is:" << x << endl;


	return 0;
}


Also: s.size() gets the number of elements in the stack;

s.empty() determines whether the stack is empty;

Since it does not provide subscript operations, it is very complicated to view all the elements in it, that is, to traverse.

In this way, the elements are taken out of the stack and printed, stored in another stack, and finally restored.

template<typename T>
void print(stack<T> &s)
{
	stack<T> s2;
	T x;
	while (s.empty() == false)
	{
		x = s.top();
		cout << x << " ";
		s2.push(x);
		s.pop();
	}
	cout << endl;

	while (s2.empty() == false)
	{
		x = s2.top();
		s2.pop();
		s.push(x);
	}

}

 

Guess you like

Origin blog.csdn.net/weixin_43917370/article/details/108571420