数据结构-单链表实现
生活随笔
收集整理的這篇文章主要介紹了
数据结构-单链表实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈表模板和實現
主要思想來自于數據結構 實現基本上是靠思想加上自身摸索實現的,有不對的地方請多多指正!
#include <iostream> using namespace std; template <class T> struct linknode{T date;linknode<T>* next;linknode(linknode<T>*io=NULL){next=io;}linknode(T x,linknode<T>* io=NULL){date=x;next=io;} }; template <class T> class list{ private:linknode<T> *first; public:list(linknode<T>*io=NULL){first=new linknode<T>(io);}list(int x,linknode<T> *io=NULL){first=new linknode<T> (x,io);}~list(){while(first->next!=NULL){first->next=first->next->next;delete first->next;}}linknode<T>* locate(int i);bool getdate(int i,T&x);void setdate(int i,T&x);linknode<T>* search(T x);int length();bool Isempty(){return first->next==NULL?1:0;}bool insert(int i ,T&x);bool remove(int i ,T&x);linknode<T>* gethead(){return first;}void input() {linknode<T> *io = gethead();int x;cin>>x;//設置x個T i;while (x >0) {cin >> i;io->next = new linknode<T>(i);io = io->next;x--;}}void f(linknode<T>* io){linknode<T>* ioo=gethead();ioo=ioo->next;cout<<first->date<<endl;return;} }; template<class T> bool list<T>::getdate(int i,T&x){linknode<T>* io=locate(i);if(io!=NULL){x=io->date;return true;}else{return false;} } template<class T> void list<T>::setdate(int i,T&x){linknode<T>* io=locate(i);if(io!=NULL){io->date=x;return;} } template <class T> linknode<T>* list<T>::search(T x){linknode<T>* io=gethead()->next;while(io!=NULL){if(io->date==x){return io;}else{io=io->next;}}return NULL; } template<class T> int list<T>::length(){linknode<T>*io=gethead()->next;//不加頭結點int i=0;while(io!=NULL){i++;io=io->next;}return i; } template <class T> bool list<T>::insert(int i ,T&x){linknode<T>*io=locate(i);if(io==NULL){return false;}linknode<T>* newnode=new linknode<T>(x);newnode->next=io->next;io->next=newnode;return true; } template <class T> bool list<T>::remove(int i,T&x){linknode<T>* io=locate(i-1);if(io==NULL||io->next==NULL){//一定要在io存在的情況下才會有nextreturn false;}io->next=io->next->next;return true; } template<class T> linknode<T>* list<T>::locate(int i){//設附加頭結點為-1的位置,則要向后移動i+1位置,因為從0開始if(i<0){return NULL;//本身first的地址也為NULL}linknode<T>* io=gethead()->next;//從0開始,first為-1int j=0;while(j<i&&io!=NULL){//注意應該是j<i.因為是得到i的位置,所以應該讓循環結束為j==i;io=io->next;j++;}return io; } int main() {list<int>*io=new list<int>(12);io->input();linknode<int>* op=io->gethead();io->f(op);int x;io->getdate(1,x);cout<<"getdate:"<<x<<endl;//2linknode<int>* ioo=io->locate(1);//1 2 3 4 5cout<<"locate:"<<ioo->date<<endl;//2ioo=io->search(x);cout<<"search:"<<ioo->date<<endl;//找到x的值之后返回對應的地址,然后賦值給ioo再由ioo進行訪問cout<<"length:"<<io->length()<<endl;cout<<"isempty:"<<io->Isempty()<<endl;ioo=io->gethead();cout<<"gethead:"<<ioo->next->date<<endl;//使用gethead獲取到頭結點后由他的下一項進行判斷,因為第一項為NULL// 1 2 3 4 5io->setdate(0,x);//就為first->next,此時ioo就為first的位置,此時x的值為2cout<<"setdate:"<<ioo->next->date<<endl;//得到的就是設置0號位后的值,2 2 3 4 5io->insert(0,x);//從i=1處添加ioo=io->locate(1);cout<<"insert:"<<ioo->date<<" "<<ioo->next->date<<endl;//2 2 2 3 4 5io->remove(2,x);//2 2 3 4 5cout<<"remove:"<<x<<endl;//2return 0; }總結
以上是生活随笔為你收集整理的数据结构-单链表实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构-链表篇
- 下一篇: 数据结构-Huffman树