• 链栈和链队列的类实现


    #include<iostream>
    #include<cassert>

    using namespace std;

    template <class T>//链栈
    struct LinkNode{
    T data;
    LinkNode<T> *Link;
    LinkNode(LinkNode<T> *pr=NULL){Link=pr;}
    LinkNode(const T& item,LinkNode<T> *pr=NULL){
    data=item;
    Link=pr;
    }
    };
    template<class T>
    class LinkedStack{
    public:
    LinkedStack()
    {
    top = NULL;
    }
    ~LinkedStack() {makeEmpty();}
    void Push(const T&x);
    bool Pop(T &x);
    bool getTop(T &x);
    bool IsEmpty()const{return (top == NULL)?true:false;}
    int getSize()const;
    void makeEmpty();
    void output();
    protected:
    LinkNode<T> *top;
    };

    template<class T>
    void LinkedStack<T>::makeEmpty(){
    LinkNode<T> *p;
    while(top!=NULL)
    {
    p = top;
    top = top->Link;
    delete p;
    }
    }

    template<class T>
    void LinkedStack<T>::Push(const T& x){
    top = new LinkNode<T>(x,top);
    assert(top!=NULL);
    }

    template<class T>
    bool LinkedStack<T>::Pop(T& x){
    if(IsEmpty()==true)return false;
    LinkNode<T> *p = top;
    top = top->Link;
    x = p->data;
    delete p;
    return true;
    }

    template<class T>
    bool LinkedStack<T>::getTop(T& x){
    if(IsEmpty()==true)return false;
    x = top->data;
    return true;
    }

    template<class T>
    int LinkedStack<T>::getSize()const{
    LinkNode<T> *p = top;
    int k = 0;
    while(p!=NULL)
    {
    p = p->Link;
    k++;
    }
    return k;
    }

    template<class T>
    void LinkedStack<T>::output(){
    LinkNode<T> *p = top;
    while(p!=NULL)
    {
    cout << p->data << " ";
    p = p->Link;

    }
    }

    template <class T>//链队列
    class LinkedQueue
    { public:
    LinkedQueue() { rear=front=new LinkNode<T>; }
    ~LinkedQueue() { makeEmpty(); }
    bool EnQueue(T x);
    bool DeQueue(T& x);
    bool getFront(T& x);
    void makeEmpty();
    bool IsEmpty() const { return front == rear; }
    int getSize();
    void output();
    private:
    LinkNode<T> *front, *rear; //队头、队尾指针
    };

    template <class T>
    bool LinkedQueue<T>::EnQueue(T x)
    { //入队列,将新元素x插到队尾
    if (front == NULL) return false;
    rear->Link = new LinkNode<T> (x);
    if (rear->Link == NULL) return false;
    rear = rear->Link;
    return true;
    }
    template <class T>
    bool LinkedQueue<T>::DeQueue(T& x)
    { //出队列,从队头取元素x,并删除首个数据结点
    if (IsEmpty() == true) return false; //判队空
    LinkNode<T> *p = front->Link;
    x = p->data;
    front->Link = p->Link;
    if(p==rear) rear=front; //修改尾指针
    delete p;
    return true;
    }
    template <class T>
    bool LinkedQueue<T>::getFront(T& x)
    { //若队列不空,则函数取队头元素的值
    if (IsEmpty() == true) return false;
    x = front->Link->data;
    return true;
    }
    template <class T>
    void LinkedQueue<T>::makeEmpty()
    { //将链表置为空表
    LinkNode<T> *q;
    while ( front->Link != NULL)
    {
    q = front->Link; //q指向被删结点
    front->Link = q->Link; //从链上删除该结点
    delete q; //释放结点空间
    }
    rear=front; //修改尾指针
    }
    template <class T>
    int LinkedQueue<T>::getSize() //计算链队列的长度
    { LinkNode<T> *p=front->Link;
    int count=0;
    while(p!=NULL)
    { p=p->Link; count++; }
    return count;
    }
    template <class T>
    void LinkedQueue<T>::output() //输出链队列
    { LinkNode<T> *p=front->Link;
    if(p==NULL) { cout<<"空队列!"; return; }
    while(p!=NULL)
    {
    if(p->data==0)
    break;
    cout<<p->data<<" "; p=p->Link;
    }
    cout<<endl;
    }

    void input(LinkedQueue<int>& Q,LinkedQueue<int>& s)
    {
    int x=1;
    while(x!=0)
    {
    cin>>x;
    Q.EnQueue(x);
    }
    while(x!=0)
    {
    cin>>x;
    s.EnQueue(x);
    }
    while




    }
    int main(){
    LinkedQueue<int>s;
    LinkedQueue<int> Q;
    input(Q,s);

    return 0;
    }
    /*void InitQueue(LinkedQueue<int>& Q ) //初始化一个有头结点的空的链队列
    {
    }
    void InitStack(LinkedStack<int>& s) //初始化一个有头结点的空的链队列
    {
    }
    void DestroyQueue(LinkedQueue<int>& Q ){}
    void DestroyStack(LinkedStack<int>& s){}
    int main( )
    {
    LinkedStack<int> s;
    LinkedQueue<int> Q;
    int e;
    InitQueue(Q);
    InitStack(s);
    cout<<("输入若干整数, 以空格间隔, 以0为结束: ");
    while(1)
    {
    scanf("%d", &e);
    if(e==0)
    break;
    Q.EnQueue(e);
    }
    while(Q.IsEmpty()!=true)
    {
    Q.DeQueue(e);
    s.Push(e);
    }
    s.output();
    DestroyQueue(Q);
    DestroyStack(s);
    return 0;
    }
    */








  • 相关阅读:
    【JavaWeb】MVC案例之新闻列表
    PayPal高级工程总监:读完这100篇论文 就能成大数据高手(附论文下载)
    自己动手搭建搜索工具
    某学院软件工程复试回忆总结
    【NLP】Tika 文本预处理:抽取各种格式文件内容
    OpenNLP:驾驭文本,分词那些事
    【类库】私房干货.Net数据层方法的封装
    Oracle手边常用70则脚本知识汇总
    Oracle手边常用命令及操作语句
    快速了解什么是自然语言处理
  • 原文地址:https://www.cnblogs.com/Peit/p/5978943.html
Copyright © 2020-2023  润新知