C++ 基于类的单向链表实现

表的实现有顺序表和链表,用顺序表实现的表要收到长度大小限制,而通过动态分配实现的链表则脱离这个束缚,使得表的使用更方便。尤其在添加、删除、清空、插入等操作效果突出。

源代码代码如下:

#include <iostream>
#include <stdexcept>
using namespace std;
template<class Node_entry>
struct Node{
    Node_entry entry;
    Node<Node_entry>* next;
    Node();
    Node( Node_entry, Node<Node_entry>* link = NULL); 
};

template<class Node_entry>
Node<Node_entry>::Node() {
    next = NULL;
}

template<class Node_entry>
Node<Node_entry>::Node(Node_entry x, Node<Node_entry>* link ) {
    entry = x;
    next = link;
}


template<class List_entry>
class List{
    public:         
        ~List();
        List();
        List( const List<List_entry>& copy);//拷贝构造
        void operator=(const List<List_entry>& copy);//赋值重载
        bool empty() const;//是否为空
        int length() const;//获取长度
        List_entry getItem(int position)const;//获取内容
        void traverse(void(*visit)(List_entry& x));//遍历链表
        bool insert(int position, const List_entry& x);//指定插入 
        void append( List_entry x);//尾部添加
        void clear(); //清空
    protected:
        int count;
        Node<List_entry>* head;
        Node<List_entry>* set_position(int position) const;获取链表的结点

};

template<class List_entry>
List<List_entry>::List() {
    count = 0;
    head = NULL;
}

template<class List_entry>
List<List_entry>::~List() {
    Node<List_entry>*p, *q;
    p = head;
    for( ; p; p = q) {
        q = p->next;
        delete p;
    }
}

template<class List_entry>
bool List<List_entry>::empty() const {
    return count <= 0;
}

template<class List_entry>
int List<List_entry>::length() const {
    return count;
}

template<class List_entry>
List_entry List<List_entry>::getItem(int position)const {
    Node<List_entry>*p;
    p = set_position( position );
    return p->entry;

}
template<class List_entry>
void List<List_entry>::traverse(void(*visit)(List_entry& x)) {
    Node<List_entry>*p;
    p = head;
    while(p != NULL) {
        (*visit)(p->entry);
        p = p->next;
    }
}
template<class List_entry>
void List<List_entry>::clear() {
    Node<List_entry>*p, *q;
    p = head;
    for( ; p; p = q) {
        q = p->next;
        delete p;
    }
    head = NULL;
    count = 0;
}

template<class List_entry>
Node<List_entry>* List<List_entry>::set_position(int position) const {
    Node<List_entry>* q = head;
    for( int i = 0; i < position; ++i) q = q->next;
    return q;
}

template<class List_entry>
bool List<List_entry>::insert(int position, const List_entry& x) {
    if(position < 0 || position > count) return false;
    Node<List_entry>* new_node, *previous, *following;
    if( position > 0) {
        previous = set_position( position - 1);
        following = previous->next;
    }

    else following = head;
    new_node = new Node<List_entry>(x, following);
    if(new_node = NULL) return false;
    if(position == 0) head = new_node;
    else previous->next = new_node;

    count++;
    return true;
}

template<class List_entry>
void List<List_entry>::append(  List_entry x) {
    Node<List_entry>* last, *new_node;
    if( count == 0) {
        head = new Node<List_entry>( x );
    } else {
        last = set_position( count - 1);
        new_node = new Node<List_entry>( x );
        last->next = new_node;
    }
    count++;
    return;     
}

template<class List_entry>
List<List_entry>::List( const List<List_entry>& copy) {
    if( copy.empty() ) return;
    else {
        Node<List_entry> *temp_head, *temp_ptr, *copy_ptr;
        copy_ptr = copy.set_position(0);
        temp_head = new Node<List_entry>(copy_ptr->entry);
        temp_ptr = temp_head;
        copy_ptr = copy_ptr->next;
        for( temp_ptr = temp_ptr->next ; copy_ptr;  copy_ptr = copy_ptr->next ) {
             temp_ptr = new Node<List_entry>(copy_ptr->entry);
             temp_ptr = temp_ptr->next;
        }   

        head = temp_head;   
    }


}
template<class List_entry>
void List<List_entry>::operator=(const List<List_entry>& copy) {
    clear();
    if( copy.empty() ) {    
        return;
    } 
    else {
        Node<List_entry> *temp_head, *temp_ptr, *copy_ptr;
        copy_ptr = copy.set_position(0);
        temp_head = new Node<List_entry>(copy_ptr->entry);
        temp_ptr = temp_head;
        copy_ptr = copy_ptr->next;
        for( temp_ptr = temp_ptr->next ; copy_ptr;  copy_ptr = copy_ptr->next ) {
             temp_ptr = new Node<List_entry>(copy_ptr->entry);
             temp_ptr = temp_ptr->next;
        }   

        head = temp_head;   
    }

}

template<class List_entry>
void print( List_entry& x) {
    cout << x << " ";
}

链表还可以实现双向移动,笔者将在下一次呈上源代码。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值