一个很好地List实现源码
生活随笔
收集整理的這篇文章主要介紹了
一个很好地List实现源码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個很好地List實現源碼:
#pragma once #include "list.h" #include <iostream> using namespace std; template <class Type> class List; template <class Type> class Node {Type contents;Node * next;friend List<Type>; }; template <class Type> class List { public:List();~List();bool IsEmpty() const {return (pHead==NULL) ? true : false;}//判斷鏈表是否為空int Length() const {return length;} //返回鏈表中的元素總數bool Find(int k, Type & intnum) const; //尋找鏈表中的第k個元素,并將其傳送至intnumint Search(const Type & intnum) const; //尋找intnum,如果發現intnum,則返回intnum的位置,如果intnum不在鏈表中,則返回0void Delete(int k, Type & intnum); //把第k個元素取至intnum,然后從鏈表中刪除第k個元素void Insert(int k, const Type &intnum); //在第k個元素之后插入intnumvoid Modify(int k,const Type &intum);//將第k個元素的值修改為intnumvoid ShowListNode(); //顯示輸出鏈表的所有數據 private:Node<Type> *pHead;int length; }; #include "list.h" template <class Type> List<Type>::~List() {length = 0;delete pHead;pHead = NULL; } template <class Type> List<Type>::List() {length = 0;pHead = NULL; } template <class Type> void List<Type>::Insert(int k, const Type & intnum) {if(k==0){if(length == 0){pHead = new Node<Type>;pHead->contents = intnum;length++;}else{Node<Type> *p= new Node<Type>;p->contents = intnum;p->next=pHead;pHead=p;length++;}}else if(k==length){Node<Type> *temp = pHead;for (int i=0;i<k-1;i++){temp = temp->next;}Node<Type> *newNode = new Node<Type>;newNode->contents = intnum;newNode->next = NULL;temp->next = newNode;length++;}else{Node<Type> *temp = pHead;for (int i=0;i<k-1;i++){temp = temp->next;}Node<Type> *newNode = new Node<Type>;newNode->contents = intnum;newNode->next = temp->next;temp->next = newNode;length++;} }template <class Type> void List<Type>::ShowListNode() {if(length == 0){cout<<"空表";}else{ Node<Type> *temp = pHead;for(int i=0;i<length;i++){cout<<"節點"<<i+1<<": "<<temp->contents<<endl;if(i!=length-1){temp = temp->next;}}} }template <class Type> void List<Type>::Delete(int k, Type & intnum) {Node<Type> *temp = pHead;if(k==1 && k==length){delete pHead;pHead = NULL;}else if(k==1){pHead = pHead->next;delete temp;}else if(k==length){for(int i=0; i<length-2; i++){temp = temp->next;}delete temp->next;temp->next = NULL;}else{for (int i=0; i<k-1; i++){temp = temp->next;}intnum = temp->contents;for(int i=k; i<length;i++){temp->contents = temp->next->contents;if(i != length-1){temp = temp->next;}else{delete temp->next;temp->next = NULL;break;}}}length--; } template <class Type> bool List<Type>::Find(int k, Type & intnum) const {if(k > length)return false;else{Node<Type> *temp = pHead ;for (int i=0; i<k-1; i++){temp = temp->next;}intnum = temp->contents;return true;} } template <class Type> int List<Type>::Search(const Type & intnum) const {Node<Type> *temp = pHead;for (int i=0; i<length; i++){if(temp->contents == intnum){return i+1;}if(i != length-1)temp = temp->next;}return 0; } template <class Type> void List<Type>::Modify(int k,const Type &intnum) {Node<Type> *temp = pHead ;for (int i=0; i<k-1; i++){temp = temp->next;}temp->contents = intnum; }總結
以上是生活随笔為你收集整理的一个很好地List实现源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: A星寻路算法介绍
- 下一篇: A星算法(VC版源码)