用c++實現 c++單鏈表的實現(採用模板類)

來源:互聯網
上載者:User

標籤:c++   class   template   單鏈表   類   

函數實現資料的插入(頭插&&尾插)、刪除(頭刪&&尾刪)、尋找、按值插入、按值刪除、求長、單鏈表清除、單鏈表摧毀、資料的逆置以及資料排序

main函數

#include"List.h"//單鏈表void main(){List<int> mylist;int select = 1;int Item;while(select){cout<<"**************************************"<<endl;cout<<"* [0] quit_system     [1] push_back  *"<<endl;cout<<"* [2] push_front      [3] show_list  *"<<endl;cout<<"* [4] pop_back        [5] pop_front  *"<<endl;cout<<"* [6] insert_val      [7] delete_val *"<<endl;cout<<"* [8] find            [9]length      *"<<endl;cout<<"* [10] clear          [11]destroy    *"<<endl;cout<<"* [12] reserv         [13]sort       *"<<endl;cout<<"**************************************"<<endl;cout<<"請選擇:>";cin>>select;switch(select){case 1:cout<<"請輸入要插入的值(-1結束):>";while(cin>>Item, Item!=-1){mylist.push_back(Item);}break;case 2:cout<<"請輸入要插入的值(-1結束):>";while(cin>>Item, Item!=-1){mylist.push_front(Item);}break;case 3:mylist.show_list();break;case 4:mylist.pop_back();break;case 5:mylist.pop_front();break;  case 6:            cout<<"請輸入要插入的值:>";            cin>>Item;            mylist.insert_val(Item);break;case 7:cout<<"請輸入要刪除的值:>";cin>>Item;mylist.delete_val(Item);break;case 8:cout<<"請輸入要尋找的值:>";cin>>Item; mylist.find(Item);break;        case 9:            mylist.length();            break;        case 10:            mylist.clear();            break;        case 11:            mylist.destroy();            break;        case 12:            mylist.reserv();            break;        case 13:            mylist.sort();default:break;}}}


List.h函數

#ifndef _LIST_H#define _LIST_H#include<iostream>using namespace std;template<class Type>class List;template<class Type>class ListNode{friend class List<Type>;public:ListNode():data(Type()),next(NULL){}ListNode(Type d, ListNode<Type> *n=NULL):data(d),next(n){}~ListNode(){}public:void SetData(Type d){data = d;}Type GetData()const{return data;}private:Type data;ListNode<Type> *next;};template<class Type>class List{public:List(){first = last = Buynode();}~List(){//destroy();}public:void push_back(const Type &x);void push_front(const Type &x);void show_list();void pop_back();void pop_front();void insert_val(const Type &x);void delete_val(const Type &x);int  length();void clear();void destroy();void reserv();void sort();void find(const Type &x);protected:ListNode<Type>* Buynode(Type x = Type()){ListNode<Type> *p = new ListNode<Type>(x);return p;}private:ListNode<Type> *first;ListNode<Type> *last;};template<class Type>void List<Type>::push_back(const Type &x)//尾插{ListNode<Type> *s = Buynode(x);last->next = s;last = s;first->data++;}template<class Type>void List<Type>::push_front(const Type &x)//頭插{ListNode<Type> *s = Buynode(x);s->next = first->next;first->next = s;first->data++;}template<class Type>void List<Type>::show_list()//顯示{ListNode<Type> *p = first->next;while(p != NULL){cout<<p->data<<" ";p=p->next;}cout<<endl;}template<class Type>void List<Type>::pop_back()//尾刪{while(first->data==0)return;ListNode<Type> *p = first;while(p->next != last){p = p->next;}delete p->next;p->next = NULL;last=p;first->data--;}template<class Type>void List<Type>::pop_front()//頭刪{ListNode<Type> *p = first->next;ListNode<Type> *tmp = NULL;if(first->data>0){tmp = p->next;first->next = tmp;delete p ;p = NULL;first->data--;}}template<class Type>void List<Type>::insert_val(const Type &x)//按值插入{ListNode<Type> *s = Buynode(x);ListNode<Type> *p = first->next;if(p->data > s->data){push_front(s->data);}else if(last->data < s->data){push_back(s->data);}else{while(p!=NULL){if(p->next->data>x){s->next = p->next;p->next = s;first->data++;return;}p=p->next;}}}template<class Type>void List<Type>::delete_val(const Type &x)//按值刪除{ListNode<Type> *p = first->next;ListNode<Type> *tmp = NULL;if(x == p->data){pop_front();return;}else if(last->data==x){pop_back();return;}else{while(p->next!=NULL){if(p->next->data == x){tmp = p->next;p->next = tmp->next;delete tmp;first->data--;return;}p = p->next;}}}template<class Type>int List<Type>::length()//求長{cout<<first->data<<endl;return first->data;}template<class Type>void List<Type>::clear()//清除{while(first->data>0)pop_front();}template<class Type>void List<Type>::destroy()//摧毀鏈表{clear();delete first;first=last = NULL;}template<class Type>void List<Type>::reserv()//逆序{ListNode<Type> *p = first->next;    ListNode<Type> *curr = p->next;    ListNode<Type> *tmp = NULL;    first->next->next = NULL;    while (curr)    {        tmp = curr->next;        curr->next = p;        p = curr;        curr = tmp;    }    first->next = p;}template<class Type>void List<Type>::sort()//排序{ListNode<Type> *q = first->next;    while (q->next)    {        ListNode<Type> *p = q;        while(p->next)        {             if(q->data > p->next->data)                {                    Type tmp =q->data;                    q->data = p->next->data;                    p->next->data = tmp;                }                p = p->next;        }        q = q->next;    }}template<class Type>void List<Type>::find(const Type &x)//尋找{ListNode<Type> *q = first->next;int count = 0;while(q !=NULL){if(q->data == x){count++;}q=q->next;}if(count)cout<<"存在該值"<<endl;elsecout<<"不存在該值"<<endl;}#endif

 

 

用c++實現 c++單鏈表的實現(採用模板類)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.