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

来源:互联网 发布:海狼 知乎 杰克伦敦 编辑:程序博客网 时间:2024/05/14 15:10

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

源代码代码如下:

#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 0