数据结构的基础-单向链表所构造的栈 c++实现

第一次写博客,也同时在学算法,在这里我将把最近学习的算法写出,尽量生动,以此代表学习的轨迹,也希望能帮助到大家,谢谢!

也希望有专家指出我的错误,以及可优化的地方,感激不尽!


算法和数据结构就是编程的一个重要部分,你若失掉了算法和数据结构,你就把一切都失掉了。


下面,我将刚写的 单向链表所构造的栈 用c++写出来,以供分享并希望得到指正!


共涉及5个文件,其中一个为main测试用例所在的文件:



tem_list.h  模版类Stack的声明

<span style="font-size:14px;">//  tem_stack_list
//
//  Created by Sxy on 15/6/16.
//  Copyright (c) 2015年 Sxy. All rights reserved.
//

#ifndef tem_stack_list_tem_list_h
#define tem_stack_list_tem_list_h

#include "exception.h"
template <typename Item>
class Stack
{
    struct Node
    {
        Item * data;
        Node * next;
        Node(const Item * p);
        ~Node();
        
    private:
        Node(constNode&);//prohibition of use
        Node&operator=(constNode&);//prohibition of use
        friendstruct Iterator;
    } * top;
    int N;
    bool clean_up();
    
public:
    
    struct Iterator
    {
        Node * ptr;
        Item& operator *()const;
        Iterator&operator++()throw(stack_list::stack_exception);
        Iteratoroperator++(int)throw(stack_list::stack_exception);
        booloperator==(constIterator&)const;
        booloperator!=(constIterator&)const;
        constNode*operator->()const;
        Iterator(const Stack & sta);
        Iterator(Node*);
    };
    
    Stack();
    ~Stack();
    void push(const Item&)throw(stack_list::stack_exception);
    void push(const Item*)throw(stack_list::stack_exception);
    Item pop() throw(stack_list::stack_exception);
    Item preview() constthrow(stack_list::stack_exception);
    bool is_empty()const;
    int size()const;
    Iterator begin()constthrow(stack_list::stack_exception);
    Iterator end() constthrow(stack_list::stack_exception);
};

#endif
</span>



tem_list.cpp模版类的定义

<span style="font-size:14px;">//  tem_stack_list
//
//  Created by Sxy on 15/6/16.
//  Copyright (c) 2015年 Sxy. All rights reserved.
//

#include "tem_list.h"

template <typename Item>
Stack<Item>::Stack():N(0),top(nullptr)
{}

template <typename Item>
Stack<Item>::~Stack()
{
    clean_up();
}

template <typename Item>
boolStack<Item>::clean_up()
{
    if(N==0)
        returntrue;
    for(Node* newnext=nullptr;N>0;N--)
    {
        newnext=top->next;
        deletetop;
        top=newnext;
    }
    returntrue;
}

template <typename Item>
voidStack<Item>::push(const Item& it)throw(stack_list::stack_exception)
{
    Node* oldnext=top;
    try{
        top=newNode(&it);
    }
    catch(std::bad_alloc&)
    {
        throwstack_list::stack_exception("The disk is full!");
    }
    top->next=oldnext;
    N++;
}

template <typename Item>
voidStack<Item>::push(const Item* ptr)throw(stack_list::stack_exception)
{
    Node* oldnext=top;
    
    try{
    top=newNode(ptr);
    }
    catch(std::bad_alloc&)
    {
        throwstack_list::stack_exception("The disk is full!");
    }
    top->next=oldnext;
    N++;
}

template <typename Item>
Item Stack<Item>::pop()throw(stack_list::stack_exception)
{
    if(is_empty())
        throwstack_list::stack_exception("The stack is empty, you can't"
                                         "pop!");
    Node * temp=top;
    Item It=*(top->data);
    top=top->next;
    delete temp;
    N--;
    return Item(It);
}

template <typename Item>
boolStack<Item>::is_empty()const
{
    returnN==0;
}

template <typename Item>
intStack<Item>::size()const
{
    std::cout<<N<<std::endl;
    returnN;
}

template <typename Item>
Item Stack<Item>::preview()constthrow(stack_list::stack_exception)
{
    if(is_empty())
        throwstack_list::stack_exception("The stack is empty, you can't"
                                         "preview it!");
    return Item(*(top->data));
}

template <typename Item>
typenameStack<Item>::IteratorStack<Item>::begin()const
        throw(stack_list::stack_exception)
{
    if(is_empty())
        throwstack_list::stack_exception("The stack is empty, you can't"
                                         "get a iterator!");
    returnIterator(top);
}

template <typename Item>
typenameStack<Item>::IteratorStack<Item>::end()const
        throw(stack_list::stack_exception)
{
    if(is_empty())
        throwstack_list::stack_exception("The stack is empty, you can't"
                                         "get a iterator!");
    Node* newptr=top;
    for(int i=N;i>1;i--)
    {
        newptr=newptr->next;
    }
    returnIterator(newptr);
}

template <typename Item>
Stack<Item>::Node::Node(const Item * p):next(nullptr)
{
    data=new Item(*p);
}

template <typename Item>
Stack<Item>::Node::~Node()
{
    deletedata;
}

template <typename Item>
Item& Stack<Item>::Iterator::operator*()const
{
    return *(ptr->data);
}

template <typename Item>
typenameStack<Item>::Iterator&Stack<Item>::Iterator::operator++()
        throw(stack_list::stack_exception)
{
    if(ptr==nullptr)
        throwstack_list::stack_exception("The stack is empty, you can't "
                                         "add your iterator!");
    if(ptr->next==nullptr)
        throwstack_list::stack_exception("It's the last Node, you can't "
                                         "add your iterator!");
    ptr=ptr->next;
    return *this;
}

template <typename Item>
typenameStack<Item>::IteratorStack<Item>::Iterator::operator++(int)
        throw(stack_list::stack_exception)
{
    if(ptr==nullptr)
        throwstack_list::stack_exception("The stack is empty, you can't "
                                         "add your iterator!");
    if(ptr->next==nullptr)
        throwstack_list::stack_exception("It's the last Node, you can't "
                                         "add your iterator!");
    Iterator olditer(*this);
    ptr=ptr->next;
    return olditer;

}

template <typename Item>
consttypenameStack<Item>::Node*Stack<Item>::Iterator::operator->()const
{
    returnptr;
}

template <typename Item>
Stack<Item>::Iterator::Iterator(constStack & sta):ptr(sta.top)
{}

template <typename Item>
Stack<Item>::Iterator::Iterator(Node * p):ptr(p)
{}

template <typename Item>
boolStack<Item>::Iterator::operator==(constIterator& it) const
{
    returnptr==it.ptr;
}

template <typename Item>
boolStack<Item>::Iterator::operator!=(constIterator& it) const
{
    returnptr!=it.ptr;
}

</span>



exception.h自定义的异常

<span style="font-size:14px;">//  tem_stack_list
//
//  Created by Sxy on 15/6/16.
//  Copyright (c) 2015年 Sxy. All rights reserved.
//

#ifndef tem_stack_list_exception_h
#define tem_stack_list_exception_h

#include <exception>
#include <iostream>

namespace stack_list
{
    class stack_exception:publicstd::exception
    {
        std::string erro_info;
    public:
        stack_exception(conststd::string& str):erro_info(str)
        {}
        constchar* what()
        {
            std::cout<<erro_info<<std::endl;
            returnerro_info.c_str();
        }
    };
}

#endif</span><pre name="code" class="cpp"><span style="font-size:14px;">
</span>

 

tem_list_mix.h用来使得模版声明和定义可以分开编译

<span style="font-size:14px;">//  tem_stack_list
//
//  Created by Sxy on 15/6/17.
//  Copyright (c) 2015年 Sxy. All rights reserved.
//

#ifndef tem_stack_list_tem_list_mix_h
#define tem_stack_list_tem_list_mix_h

#include "tem_list.h"
#include "tem_list.hpp"

#endif</span><pre name="code" class="cpp"><span style="font-size:14px;">
</span>

 

main.cpp测试用例

//  tem_stack_list
//
//  Created by Sxy on 15/6/16.
//  Copyright (c) 2015年 Sxy. All rights reserved.
//

#include <iostream>
#include "tem_list_mix.h"

int main(int argc,constchar * argv[])try{
        Stack<int> sta;
        int a[10];
        for(int i=0;i<10;i++)
        {
            a[i]=i;
            sta.push(a[i]);
        }
        sta.size();
    
        Stack<int>::Iterator it=sta.begin();
        for(int j=1;it!=sta.end();it++,j++)
        {
            std::cout<<*it<<" ";
        }
        
        std::cout<<*it<<std::endl;
        std::cout<<sta.preview()<<std::endl;
        
        int b[10];
        for(int i=0;i<10;i++)
        {
            b[i]=sta.pop();
        }
    
        for(int i=0;i<10;i++)
        {
            std::cout<<b[i];
            if(i!=9)
                std::cout<<" ";
            if(i==9)
                std::cout<<std::endl;
        }
}
catch(stack_list::stack_exception& exc)
{
    exc.what();
}




稍后我将贴出讲解,大家有什么问题可以留言,希望大家一同学习,而不是只是复制粘贴,谢谢!

转载请注明出处,盗用必究


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值