链栈(C++模板实现)

一、实验要求

  • 本次实验中的链栈结构指带头结点的单链表;
  • 链栈结构和运算定义,算法的实现以库文件方式实现,不得在测试主程序中直接实现;
  • 实验程序有较好可读性,各运算和变量的命名直观易懂,符合软件工程要求;

二、数据结构设计

template<class ElementType>
struct Node{
    ElementType data;             //数据域
    Node* next;                   //指针域
};
template<class ElementType>
class Stack{
public:
    Stack();                      //初始化栈
    ~Stack();                     //销毁链栈 
    bool empty();                 //判断栈是否为空
    void push(ElementType x);     //压栈
    bool pop(ElementType &x);     //出栈
    ElementType top();            //取栈顶元素
    void print();                 //遍历栈
private:
    Node<ElementType> * head;     //栈头结点指针
};

三、代码实现

#ifndef _QUEUE_H_
#define _QUEUE_H_
#include <iostream>

using namespace std;

template<class ElementType>
struct LNode{
    ElementType data;
    struct LNode * next;
};
template<class ElementType>
class Queue{
public:
    Queue();          //初始化一个队列
    ~Queue();         //销毁队列
    bool empty();     //判断是否队空
    void enter( ElementType x);     //入队
    void out();       //出队
    void getFront(ElementType &x);  //取队头元素
    int getLength();  //求当前队列中元素个数
    void print();            //遍历队列
private:
    LNode <ElementType> *front;
    LNode <ElementType> *rear;
};
//初始化一个队列
template<class ElementType>
Queue< ElementType>::Queue()
{
    front=rear=NULL;
}
//销毁队列
template<class ElementType>
Queue< ElementType>::~Queue()
{
    LNode <ElementType> * p,*q;
    p=front;
    while(p!=NULL)
    {
        q=p->next;
        delete p;
        p=q;
    }
}
//判断是否队空
template<class ElementType>
bool Queue< ElementType>:: empty()
{
    if(front==NULL&&rear==NULL)
    {
        return true;
    }
    else{
        return false;
    }
}
//入队
template<class ElementType>
void Queue< ElementType>::enter( ElementType x)
{
    LNode <ElementType>* p;
    static int i=1;
    if(i==1)
    {
        p=new LNode<ElementType>;
        p->next=NULL;    //创建首节点
        p->data=x;
        front=p;
        rear=p;
        i++;
    }
    else{
        p=new LNode<ElementType>;
        p->next=rear->next;   //创建其他节点
        p->data=x;
        rear->next=p;
        rear=p;
    }

}
//出队
template<class ElementType>
void Queue< ElementType>::out()
{
    if(empty())
    {
        cout<<"队列为空!"<<endl;   //判断节点是否为空
    }
    else{
        if(front->next==NULL)
        {
            delete front;
            front=rear=NULL;        //仅剩首节点市
        }
        else{
            LNode <ElementType>* p;
            p=front->next;
            delete front;
            front=p;
        }
    }
}
//取队头元素
template<class ElementType>
void Queue< ElementType>::getFront(ElementType &x)
{
    if(empty())
    {
        cout<<"队列为空,不能取元素!"<<endl;   //判断节点是否为空,不能取元素
    }
    else{
        x=front->data;
    }
}
//求当前队列中元素个数
template<class ElementType>
int Queue< ElementType>:: getLength()
{
    LNode <ElementType>* p;
    int i=0;
    p=front;
    while(p!=NULL)
    {
        i++;
        p=p->next;
    }
    return i;
}
//遍历队列
template<class ElementType>
void Queue<ElementType>::print()
{
    LNode <ElementType>* p;
    p=front;
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
}
#endif // _QUEUE_H_
#ifndef _LISTSTACK_H_
#define _LISTSTACK_H_
#include <iostream>

using namespace std;

template<class ElementType>
struct Node{
    ElementType data;             //数据域
    Node* next;                   //指针域
};
template<class ElementType>
class Stack{
public:
    Stack();                      //初始化栈
    ~Stack();                     //销毁链栈
    bool empty();                 //判断栈是否为空
    void push(ElementType x);     //压栈
    bool pop(ElementType &x);     //出栈
    ElementType top();            //取栈顶元素
    void print();                 //遍历栈
private:
    Node<ElementType> * head;     //栈头结点指针
};
//初始化栈
template<class ElementType>
Stack<ElementType>::Stack()
{
     head=new Node<ElementType>;
     if(head==NULL)
     {
         cout<<"申请内存失败!"<<endl;
         return;
     }
     head->next=NULL;
}
//销毁栈
template<class ElementType>
Stack<ElementType>::~Stack()
{
    Node<ElementType> * p,*q;
    p= head;               //初始化时p指向头结点
    while(p)
    {
        q=p->next;
        delete p;
        p=q;
    }
}
 //判断栈是否为空
template<class ElementType>
bool Stack<ElementType>::empty()
{
    if( head->next==NULL)
    {
        return 1;
    }
    else{
        return 0;
    }
}
//压栈
template<class ElementType>
void Stack<ElementType>::push(ElementType x)
{
    Node<ElementType> *p=new Node<ElementType>;
    if(p==NULL)
     {
         cout<<"申请内存失败!"<<endl;
         return;
     }
     p->data=x;
     p->next=head->next;
     head->next=p;
}
//出栈
template<class ElementType>
bool Stack<ElementType>::pop(ElementType &x)
{
    Node<ElementType> * t;
    if( head->next==NULL)
    {
        return 0;
    }
    else{
        t= head->next;
        x=t->data;
        head->next=t->next;
        delete t;
    }
}
//取栈顶元素
template<class ElementType>
ElementType Stack<ElementType>::top()
{
    if(empty())
    {
        return NULL;
    }
    else{
        return head->next->data;
    }
}
//遍历栈
template<class ElementType>
void Stack<ElementType>::print()
{
    Node<ElementType>* p;
    p= head->next;
    while(p)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
}
#endif // _LISTSTACK_H_
#include <iostream>
#include <string>
#include <Cstdlib>
#include "listStack.h"
#include "queue.h"

using namespace std;

//1.输出函数
void print(int x)
{
    char a[17]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    switch( x)
    {
        case 0 :
           cout<<a[x];
        break;
        case 1 :
           cout<<a[x];
        break;
        case 2 :
           cout<<a[x];
        break;
        case 3 :
           cout<<a[x];
        break;
        case 4 :
           cout<<a[x];
        break;
        case 5 :
           cout<<a[x];
        break;
        case 6 :
           cout<<a[x];
        break;
        case 7 :
           cout<<a[x];
        break;
        case 8 :
           cout<<a[x];
        break;
        case 9 :
           cout<<a[x];
        break;
        case 10 :
           cout<<a[x];
        break;
        case 11 :
           cout<<a[x];
        break;
        case 12 :
           cout<<a[x];
        break;
        case 13 :
           cout<<a[x];
        break;
        case 14 :
           cout<<a[x];
        break;
        case 15 :
           cout<<a[x];
        break;
        case 16 :
           cout<<a[x];
        break;
    }
}
//2.十进制转化为16进制(顺序栈实验),其中i为十进制数,k为16
void convert(int i,int k)
{
    Stack<int> S;
    int mod,x;              //mod为余数,x保存出栈元素
    while(i!=0)
    {
        mod=i%k;            //mod保存余数
        S.push(mod);  //将mod入栈
        i=i/k;              //商保存在i中
    }
    cout<<k<<"进制为:";
    while(!S.empty())
    {
        S.pop(x);   //取栈顶元素,并出栈
        print(x);          //输出x
    }
}
//3.括号匹配
bool is_match(const string &str)
{
    size_t len = str.length();
    Stack<char> s;
    Queue<char> t;
    int i;
    char x,y;
    int p=0,q=0;
    for (i = 0; i < len; ++i)
    {
        if (str[i] =='('||str[i] =='{' ||str[i] =='[')
        {
            s.push(str[i]);
            p++;
            continue;
        }
        if (str[i] ==')'||str[i] =='}' ||str[i] ==']')
        {
            t.enter(str[i]);
            q++;
            continue;
        }
    }
    if(p==q)
    {
        for(i=0;i<p;i++)
        {
            t.getFront(x);
            if((s.top()=='('&& x==')')||(s.top()=='{'&& x=='}')||(s.top()=='['&& x==']'))
            {
                s.pop(y);
                t.out();
            }
        }
    }
    else
    {
         return false;
    }
    if (s.empty()&&t.empty())
    {
       return true;

    }
    else{
         return false;
    }
    s.~Stack();
    t.~Queue();
}
int main()
{
    int a;
    cout<<"******************************************"<<endl;
    cout<<"按1十进制转十六进制"<<endl;
    cout<<"按2括号匹配"<<endl;
    cout<<"按3退出"<<endl;
    cout<<"******************************************"<<endl;
    cout<<"请输入执行算法的序号:";
    cin>>a;
    while(a!=3)
    {
        switch(a)
        {
           case 1 :
                int i;
                cout<<"请输入要转换的数字:";
                cin>>i;
                convert(i,16);
                cout<<endl;
           break;
           case 2:
                string str="(1+2)";
                cout<<"请输入字符串:";
                cin>>str;
                if(is_match(str))
                {
                    cout<<"匹配"<<endl;
                }
                else{
                    cout<<"不匹配"<<endl;
                }
            break;
        }
        system("PAUSE");
        system("CLS");
        cout<<"******************************************"<<endl;
        cout<<"按1十进制转十六进制"<<endl;
        cout<<"按2括号匹配"<<endl;
        cout<<"按3退出"<<endl;
        cout<<"******************************************"<<endl;
        cout<<"请输入执行算法的序号:";
        cin>>a;
    }
    return 0;
}

四、实验截图

发布了46 篇原创文章 · 获赞 48 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_39559641/article/details/89069372