双向链表实现

来源:互联网 发布:网络通信协议有哪些 编辑:程序博客网 时间:2024/04/29 20:03

实现双向链表虽然复杂了点儿,但是有简单链表做前奏,这个还是想当顺心的。

还是要注意 复制的时候要把数据成员全部复制。new结点的时候,给定next = NULL,preceding = new_copy。

插入和删除时分是否位置是0;

贴一发~

List.h:

/*=============================================================================##      Author: liangshu - cbam ##      QQ : 756029571 ##      School : 哈尔滨理工大学 ##      Last modified: 2015-10-28 19:43##     Filename: 双向链表实现##     Description: #        The people who are crazy enough to think they can change the world, are the ones who do ! =============================================================================*/#using namespace std;enum Error_code {success, overflow, underflow, Range_error};template<class Node_entry>struct Node{    Node_entry Entry;    Node<Node_entry> *next;    Node<Node_entry> *back_;    Node();    Node(const Node_entry &item, Node<Node_entry> *link_back = NULL, Node<Node_entry> *link_next = NULL){      Entry = item;      next = link_next;      back_ = link_back;    }};template<class Node_entry>Node<Node_entry>::Node(){  next = NULL;  back_ = NULL;}template<class List_entry>class List{public:    List();    List(const List<List_entry> &Copy);    int Size_list()const{    return num;    }    bool Empty_list()const{       return num == 0;    }    void Clear();    Error_code retrieve(int position, List_entry &item)const;    Error_code Remove(int position, List_entry &item);    Error_code Replace(int position, const List_entry &item);    Error_code Insert(int position, const List_entry &item);    void operator = (const List<List_entry> &Copy);    void Print_list()const;protected:    mutable int current_position;    mutable Node<List_entry> *current;    void set_position(int postion)const;    int num;};template<class List_entry>List<List_entry>::List(){  num = current_position = 0;  current = NULL;}template<class List_entry>void List<List_entry>::Clear(){    set_position(0);    while(!Empty_list()){        Node<List_entry> *old_head = current;        num--;        delete old_head;        current = current -> next;    }    current = NULL;    current_position = 0;}template<class List_entry>List<List_entry>::List(const List<List_entry> &Copy){    if(Copy.Empty_list()){        num = current_position = 0;        current = NULL;        return ;    }    Copy.set_position(0);cout<<"dsg"<<endl;    Node<List_entry> *new_copy, *new_current, *tmp_current = Copy.current;   new_current = new_copy = new Node<List_entry>(tmp_current -> Entry, tmp_current -> back_);   while(tmp_current -> next != NULL){    tmp_current = tmp_current -> next;    new_copy -> next = new Node<List_entry>(tmp_current -> Entry, new_copy);    new_copy = new_copy -> next;   }   num = Copy.num;   current_position = 0;   current = new_current;}template<class List_entry>Error_code List<List_entry>::retrieve(int position, List_entry &item)const{    if(position < 0 || position > num){        return Range_error;    }    set_position(position);   item = current -> Entry;   return success;}template<class List_entry>Error_code List<List_entry>::Remove(int position, List_entry &item){     if(position < 0 || position > num){        return Range_error;     }     if(Empty_list()){        return underflow;     }     Node<List_entry>*previous, *old_node, *following;     if(position > 0){        set_position(position + 1);        following = current;        set_position(position - 1);        old_node = previous = current;        old_node = old_node -> next;        item = old_node -> Entry;        previous -> next = following;        following -> back_ = previous;        delete old_node ;     }     else{        set_position(0);        old_node = current;        current = current -> next;        current -> back_ = NULL;        item = old_node -> Entry;        delete old_node;     }     num--;     return success;}template<class List_entry>Error_code List<List_entry>::Replace(int position, const List_entry &item){    if(position < 0 || position > num){        return Range_error;     }     List_entry x;     Remove(position, x);     Insert(position, item);     return success;}template<class List_entry>void List<List_entry>::operator = (const List<List_entry> &Copy){    if(Copy.Empty_list()){        return ;    }    Node<List_entry>*new_copy, *new_current, *tmp_current = Copy.current;   while(tmp_current -> back_ != NULL){    tmp_current = tmp_current -> back_;   }   new_current = new_copy = new Node<List_entry>(tmp_current -> Entry, tmp_current -> back_);   while(tmp_current -> next != NULL){    tmp_current = tmp_current -> next;    new_copy -> next = new Node<List_entry>(tmp_current -> Entry, new_copy);    new_copy = new_copy -> next;   }   Clear();   current = new_current;   num = Copy.num;}template<class List_entry>void List<List_entry>::set_position(int position)const{   if(current_position < position){    for(; current_position != position; current_position++){        current = current -> next;    }   }   else{    for(; current_position != position; current_position--){        current = current -> back_;    }   }}template<class List_entry>void List<List_entry>:: Print_list()const{    cout<<"| -- ";    if(Empty_list()){            cout<<" |"<<endl;        return ;    }   Node<List_entry> *tmp_current = current;   while(tmp_current -> back_ != NULL){    tmp_current = tmp_current -> back_;   }   while(tmp_current != NULL){    cout<<tmp_current -> Entry<<" -- ";       tmp_current = tmp_current -> next;   }   cout<<" |"<<endl;}template<class List_entry>Error_code List<List_entry>::Insert(int position, const List_entry &item){   if(position < 0 || position > num){    return Range_error;   }   Node<List_entry>*new_node,  *following, *preceding;   if(position == 0){    if(num == 0){        following = NULL;    }    else{        set_position(0);        following = current;    }    preceding = NULL;   }   else{    set_position(position - 1);    preceding = current;    following = preceding -> next;   }   new_node = new Node<List_entry>(item, preceding, following);   if(new_node == NULL){    return overflow;   }   if(preceding != NULL){    preceding -> next = new_node;   }   if(following != NULL){    following -> back_ = new_node;   }   current_position = position;   current = new_node;   num++;   return success;}

Test.cpp


    #include<iostream>    #include"a.h"    using namespace std;    int main()    {        List<int>list_1, list_2, list_3;        for(int i = 1; i <= 5; i++){           list_1.Insert(i - 1, i);        }        int x;        list_1.Print_list();        cout<<list_1.Size_list()<<endl;        list_1.retrieve(1, x);        cout<<x<<endl;        list_1.Remove(2, x);        cout<<"Remove1 ="<<x<<endl;        list_1.Print_list();        cout<<list_1.Size_list()<<endl;        list_1.Insert(0,235345);        list_1.Print_list();        list_1.Replace(2, 100);        list_1.Print_list();        list_1.Remove(0, x);        cout<<"Remove2 = "<<x<<endl;        list_1.Print_list();        cout<<list_1.Size_list()<<endl;        list_1.Insert(3,123456);        list_1.Print_list();        cout<<list_1.Size_list()<<endl;       list_1.Clear();        list_1.Print_list();        cout<<list_1.Size_list()<<endl;        for(int i = 1; i <= 9; i++){           list_1.Insert(i - 1, i);        }        list_1.Print_list();cout<<"ci = "<<endl;        list_2 = list_1;        list_2.Print_list();        list_3 = List<int>(list_2);        list_3.Print_list();        list_3.Remove(3,x);        list_3.Print_list();        list_3.Insert(0,19999);        list_3.Insert(7, 1000009);        list_3.retrieve(9, x);        cout<<"x ="<<x<<endl;        list_3.Print_list();        list_3.Insert(0,344);        cout<<list_3.Size_list()<<endl;        list_3.Insert(11,234535);        list_3.Print_list();        list_3.Replace(3,10000000);        list_3.Print_list();        return 0;    }


0 0
原创粉丝点击