数据结构C++版-栈

 

一、概念

 

 

二、应用实例

1.进制转换

#include <stdlib.h>
#include <iostream>
#include <string>

#include "MyStack.h"
#include "Coordinate.h"
using namespace std;

#define  BINARY 2
#define  OCTONARY 8
#define  HEXADECIMAL 16

void main ( )
{ 
    //MyStack<int> s(30);
    MyStack<char> s(30);

    char num[]="0123456789ABCDEF";

    int N=1348;
    int mod=0;
    while (N!=0)
    {
        mod=N%16;
        s.push(num[mod]);
        N=N/16;
    }

    s.stackTraverse(false);

    


    /*
        int elem=0;

        for (int i=s.stackLength()-1;i>=0;i--)
    {
        s.pop(elem);
        cout<<num[elem];
    }*/


    /*while(!s.stackEmpty())
    {
        s.pop(elem);
        cout<<num[elem];
    }*/


    system("pause");
    
} 

 

2.括号匹配

MyStack<char> s(30);   //存放未匹配的括号
    MyStack<char> s1(30);  //存放栈顶括号的另一半

    char str[] = "([])[";    //存放待匹配文本目标,要求无空格 e.g.   [()()]  ([]([]))
    char current=0;  //当前括号需要匹配的另一半

    for (int i=0;i<strlen(str);i++)
    {
        if (current!=str[i])
        {
            s.push(str[i]);
            switch(str[i])
            {
            case  '[':    //case 后面数据类型是int,单个字符会转换成其ASC码

                if (current!=0)
                {
                    s1.push(current);
                }
                current=']';
                break;

            case '(':

                if (current!=0)
                {
                    s1.push(current);
                }
                current=')';
                break;

            default:
                cout<<"括号不匹配."<<endl;  //default后面语句可以注释掉,因为current不等于str[i]时str[i]就会入栈,第一个栈不为零匹配就会失败

            }
            
        } 
        else
        {
            char elem;
            s.pop(elem);

            if(!s1.pop(current))
            {
                current=0;
            }
        }
    }



if (s.stackEmpty())
    {
        cout<<"括号匹配"<<endl;
    } 
    else
    {
        cout<<"括号不匹配"<<endl;
    }

    system("pause");

 

 

 

转载于:https://www.cnblogs.com/Tang-tangt/p/9557559.html

内含资源如下: 1.基本数据结构 1.1.Array ........... 动态数组 1.2.LinkedList ... 链表 1.3.BST .............. 二分搜索树 1.4.MapBST ..... 二分搜索树(用于实现映射) 1.5.AVLTree ...... AVL树 2.接口 2.1.Queue ........... 队列接口 2.2.Stack .............. 接口 2.3.Set .................. 集合接口 2.4.Map ............... 映射接口 2.5.Merger .......... 自定义函数接口 2.6.UnionFind ..... 并查集接口 3.高级数据结构 3.1.ArrayQueue .......................... 队列_基于动态数组实现 3.2.LinkedListQueue .................. 队列__基于链表实现 3.3.LoopQueue ........................... 循环队列_基于动态数组实现 3.4.PriorityQueue ....................... 优先队列_基于最大二叉堆实现 3.5.ArrayPriorityQueue ............. 优先队列_基于动态数组实现 3.6.LinkedListPriorityQueue ..... 优先队列_基于链表实现 3.7.ArrayStack ............................. _基于动态数组实现 3.8.LinkedListStack ..................... _基于链表实现 3.9.BSTSet ..................................... 集合_基于二分搜索树实现 3.10.LinkedListSet ....................... 集合_基于链表实现 3.11.BSTMap ................................ 映射_基于二分搜索树实现 3.12.AVLTreeMap ....................... 映射_ 基于AVL树实现 3.13.LinkedListMap .................... 映射_基于链表实现 3.14.MaxHeap ............................. 最大二叉堆 3.15.SegmentTree ...................... 线段树 3.16.Trie ......................................... 字典树 3.17.QuickFind ............................ 并查集_基于数组实现 3.18.QuickUnion ......................... 并查集_基于树思想实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值