• 栈的定义--Stack

栈是只允许在末端进行插入和删除的线性表。栈具有后进先出的特性(LIFO ,Last In Fast Out)。

学过数据结构的人都知道:栈可以用两种方式来实现,一种方法是用数组实现栈,这种栈成为静态栈;另外一种方法是用链表实现栈,这种栈叫做动态栈。

  • 栈提供如下操作
    s.empty()               如果栈为空返回true,否则返回false
s.size() 返回栈中元素的个数
s.pop() 删除栈顶元素但不返回其值
s.top() 返回栈顶的元素,但不删除该元素
s.push() 在栈顶压入新元素
  • 栈的实现

下面是用C++实现的一个栈结构的源码(顺序表)

 #pragma once
#include<iostream>
#include<assert.h>
using namespace std;
template<typename T>
class Stack
{
public:
Stack()
:array(NULL)
, size()
, capacity()
{}
~Stack()
{
if (array != NULL)
{
delete[] array;
}
capacity = ;
size = ;
}
Stack(const Stack<T>& s)
{
array = new T[s.size];
memcpy(array, s.array, sizeof(T)*s.size);
size =s.size;
capacity = s.capacity;
}
Stack<T>& operator=( const Stack<T>& s)
{
capacity = s.capacity;
size = s.size;
this->array =s.array;
return *this;
}
void Print()
{
for (int index = ; index < size; index++)
{
cout << array[index] << " ";
}
cout << endl;
}
void Push(const T& x)
{
_CheckCapacity(); array[size++] = x;
} void Pop()
{
assert(size > );
--size;
} size_t Size()
{
return size;
} bool Empty()
{
return size == ;
} const T& Top()
{
assert(size > ); return array[size - ];
}
protected:
void _CheckCapacity()
{
if (size >= capacity)
{
T *temp = new T[capacity * + ];
for (int index = ; index < size; index++)
{
temp[index] =array[index];
}
delete[] array;
array = temp;
capacity = capacity * + ;
} } protected:
T * array;
size_t size;
size_t capacity;
};
void fun()
{
Stack<int> s;
s.Push();
s.Push();
s.Push();
s.Push();
s.Print();
cout << s.Top() << " ";
s.Pop();
}
void main()
{
fun();
system("pause");
}
  • 栈的应用

1.算术表达式求值。波兰表达式(后缀表达式)   

实现:

 #include <iostream>
#include <assert.h>
#include"stack"
using namespace std; enum Type
{
OP_NUM,
OP_SYMBOL
}; enum OP_SMB
{
ADD,
SUB,
MUL,
DIV,
}; struct Cell
{
Type _type;
int _value;
}; Cell RNPArray[] =
{
{ OP_NUM, },
{ OP_NUM, },
{ OP_NUM, },
{ OP_SYMBOL, ADD },
{ OP_SYMBOL, MUL },
{ OP_NUM, },
{ OP_SYMBOL, SUB },
{ OP_NUM, },
{ OP_NUM, },
{ OP_SYMBOL, DIV },
{ OP_SYMBOL, ADD },
}; int CountRNP(Cell* a, size_t size)
{
stack<int> s;
for (int i = ; i < size; ++i)
{
if (a[i]._type == OP_NUM)
{
s.push(a[i]._value);
}
else if (a[i]._type == OP_SYMBOL)
{
int right = s.top();
s.pop();
int left = s.top();
s.pop(); switch (a[i]._value)
{
case ADD:
s.push(left + right);
break;
case SUB:
s.push(left - right);
break;
case MUL:
s.push(left * right);
break;
case DIV:
s.push(left / right);
break;
}
}
}
return s.top();
}
void test()
{ int ret=CountRNP(RNPArray, sizeof(RNPArray) / sizeof(RNPArray[]));
cout << ret;
}
int main()
{
test();
return ;
}

2.迷宫问题

 #define _CRT_SECURE_NO_WARNINGS    1
#include<iostream>
#include<stack>
#include<assert.h>
using namespace std;
//从文件获取初始迷宫地图
void GetMazeMap(int *maze, int rows, int cols)
{
assert(maze);
FILE *fOut = fopen("MazeMap.txt", "r");
assert(fOut);
for (int i = ; i < rows; i++)
{
for (int j = ; j < cols;)
{
char ch = fgetc(fOut);
if (ch == '' || ch == '')
{
maze[i*cols + j] = ch - '';
j++;
}
}
}
fclose(fOut);
}
//打印迷宫地图
void PrintMazeMap(int*maze, int rows, int cols)
{
assert(maze);
for (int i = ; i < rows; i++)
{
for (int j = ; j < cols; j++)
{
cout << maze[i*cols + j] << " ";
}
cout << endl;
}
cout << endl;
}
struct Point
{
size_t row;
size_t col;
};
//检查该点是否为可通行
bool CheckIsAccess(int *maze, int rows, int cols,Point cur)
{
assert(maze);
if (cur.row < rows&&cur.col < cols&& (maze[cur.row*cols + cur.col] == ))
{
return true;
}
return false;
}
//获取走出迷宫路径
stack<Point> GetMazePath(int* maze, int rows, int cols, Point entry)
{
assert(maze);
stack<Point> path;
Point cur = entry;
path.push(entry);
maze[cur.row*cols + cur.col] = ; while (!path.empty())
{
Point cur = path.top();
Point next = cur;
if (next.row ==rows-)
{
return path;
}
//试探 上
next = cur;
next.row--;
if (CheckIsAccess(maze, , , next))
{
path.push(next);
maze[next.row*cols + next.col] = ;
continue;
}
//下
next = cur;
next.row++;
if (CheckIsAccess(maze, , , next))
{
path.push(next);
maze[next.row*cols + next.col] = ;
continue;
}
//左
next = cur;
next.col--;
if (CheckIsAccess(maze, , , next))
{
path.push(next);
maze[next.row*cols + next.col] = ;
continue;
}
//右
next = cur;
next.col++;
if (CheckIsAccess(maze, , , next))
{
path.push(next);
maze[next.row*cols + next.col] = ;
continue;
}
}
cout << "Not have exit" << endl;
return path;
}
void TestMaze()
{
int maze[][] = {}; GetMazeMap((int *)maze, , );
PrintMazeMap((int*)maze, , );
Point entry = { , };
GetMazePath((int*)maze, , , entry);
PrintMazeMap((int*)maze, , );
}
int main()
{
TestMaze();
//system("pause");
getchar();
return ;
}

数据结构—栈(Stack)的更多相关文章

  1. C# 数据结构 栈 Stack

    栈和队列是非常重要的两种数据结构,栈和队列也是线性结构,线性表.栈和队列这三种数据结构的数据元素和元素的逻辑关系也相同 差别在于:线性表的操作不受限制,栈和队列操作受限制(遵循一定的原则),因此栈和队 ...

  2. java数据结构 栈stack

    栈(Stack) 栈(Stack)实现了一个后进先出(LIFO)的数据结构. 你可以把栈理解为对象的垂直分布的栈,当你添加一个新元素时,就将新元素放在其他元素的顶部. 当你从栈中取元素的时候,就从栈顶 ...

  3. 模板 - 数据结构 - 栈/Stack

    普通的栈大家都会写,STL的栈据说默认实现方式是deque,没关系反正deque跑得飞快. 这里收录的是一些奇怪的栈,当然双栈实现的队列收录在队列里面. 对顶栈 众所周知,栈可以维护一系列前缀和,包括 ...

  4. 数据结构----栈stack

    栈的概念与数据结构 栈(有时称为“后进先出栈”)是一个元素的有序集合,其中添加移除新元素总发生在同一端.这一端通常称为“顶部”.与顶部对应的端称为“底部”.栈的底部很重要,因为在栈中靠近底部的元素是存 ...

  5. [C++][数据结构]栈(stack)的实现

    对于栈的定义,前人之述备矣. 我实现的是一个stack<value>容器类,支持push,pop,top,size,empty,clear和copy construction操作. 主要的 ...

  6. python基本数据结构栈stack和队列queue

    1,栈,后进先出,多用于反转 Python里面实现栈,就是把list包装成一个类,再添加一些方法作为栈的基本操作. 栈的实现: class Stack(object): #初始化栈为空列表 def _ ...

  7. 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现

      本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型   栈是 ...

  8. Python与数据结构[1] -> 栈/Stack[0] -> 链表栈与数组栈的 Python 实现

    栈 / Stack 目录 链表栈 数组栈 栈是一种基本的线性数据结构(先入后出FILO),在 C 语言中有链表和数组两种实现方式,下面用 Python 对这两种栈进行实现. 1 链表栈 链表栈是以单链 ...

  9. java数据结构——栈(Stack)

    学习数据结构与算法是枯燥的,但只有坚持不懈的积累,才会有硕果累累的明天. /** * 继续学习Java数据结构 ————栈 * 栈的实现其实还是使用数组,只不过我们不能直接访问数组下标,而是通过一个指 ...

随机推荐

  1. Spring、Spring Boot、Spring Frame、Spring MVC的区别

    Spring框架就像一个厂商,其下有很多产品,如Spring Boot.Spring Frame.Spring Cloud等等. Spring Boot用于快速.方便.简单的搭建一个Spring项目. ...

  2. JAVAOOP I/O

    程序的主要任务就是操作数据,通过允许程序读取文件的内容或向文件写入数据,可以使程序应用更加广泛. I/O(input/output) 在不同操作系统之下,所占的字节数也不同,一般认为 8.1.1使用F ...

  3. jQuery最重要的知识点

    1.各种常见的选择器.2.对于属性的操作.[重点] 2.1)获取或设置属性的值: prop(); 2.2 ) 添加.删除.切换样式: addClass/removeClass/toggleClass ...

  4. webpack和sass功能简介

    1.webpack webpack 是一个打包工具,为什么需要打包?因为有的人的脚本开发语言可能是 CoffeeScript 或者是 TypeScript,样式开发工具可能是 Less 或者 Sass ...

  5. pyqt5--学习资料

    http://zetcode.com/gui/pyqt5/ http://www.thehackeruniversity.com/2014/01/23/pyqt5-beginner-tutorial/ ...

  6. 微信小程序横向滚动

    <scroll-view scroll-x="true" style=" white-space: nowrap; display: flex" > ...

  7. Python基础教程学记(1)

    引言 Python是什么?——Python是一种面向对象的解释性高级编程语言,具有动态语义.这句话的要点在于,Python是一种知道如何不妨碍你编写程序的编程语言.它让你能够毫无困难地实现所需的功能, ...

  8. pyecharts的简单使用

    由于需要在项目中展示数据,查了查资料发现,pyecharts模块在网页数据展示方面有很大优势,所以就学了点pyechas 参考博客:Python:数据可视化pyecharts的使用 - JYRoy - ...

  9. python基础之初识

    一. 计算机是什么 基本组成: 主板+cpu+内存 cpu: 主频, 核数(16) 内存:大小(8G, 16G, 32G) 型号: DDR3, DDR4, DDR5, 主频(海盗船,玩家国度) 显卡: ...

  10. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...