基础数据结构——单链表

来源:互联网 发布:乍得内战知乎 编辑:程序博客网 时间:2024/06/16 10:33

类模板实现链表

#include <cstdlib>#include <iostream>#include <string>using namespace std;template<class T>struct node{T data;node<T> *next;};template<class T>class MYlinklist{public:MYlinklist();MYlinklist(const MYlinklist<T> &aplist);~MYlinklist();MYlinklist<T>& operator = (const MYlinklist<T> &rlist);void makeEmpty();void insertHead(const T &element);//insert before the head of the list.void insertTail(const T &element);//insert at the tail of the list.bool first(T &element);bool find(const T &element);bool remove(const T &element);void printList();private:node<T> *head;node<T> *current;inline void deepCopy(const MYlinklist<T> &original);};template<class T>MYlinklist<T>::MYlinklist(){head = current = NULL;}template<class T>MYlinklist<T>::MYlinklist(const MYlinklist<T> &aplist){deepCopy(aplist);}template<class T>MYlinklist<T>::~MYlinklist(){makeEmpty();}template<class T>MYlinklist<T>& MYlinklist<T>::operator=(const MYlinklist<T> &list){if (this == &list){return *this;}makeEmpty();deepCopy(list);return *this;}template<class T>void MYlinklist<T>::makeEmpty(){while(head != NULL){current = head;head = head->next;delete current;}current = NULL;}template<class T>void MYlinklist<T>::deepCopy(const MYlinklist<T> &original){head = current = NULL;if(original.head == NULL)return;node<T> *copyptr = head = new node<T>;node<T> *originalptr = original.head;copyptr->data = original->data;if(originalptr = original.current)current = originalptr;while(original.next != NULL){copyptr->next = new node<T>;originalptr = originalptr->next;copyptr = copyptr->next;copyptr->data = originalptr->data;if(originalptr == original.current)copyptr->current = copyptr;}copyptr->next = NULL;}template<class T>void MYlinklist<T>::insertHead(const T &element){current = NULL;node<T> *newnode = new node<T>;newnode->data = element;newnode->next = head;head = newnode;}template<class T>void MYlinklist<T>::insertTail(const T &element){current = NULL;node<T> *newnode = new node<T>;newnode->data = element;newnode->next = NULL;node<T> *tempnode = head;if (head == NULL){head = newnode;return;}while(tempnode->next != NULL){tempnode = tempnode->next;}tempnode->next = newnode;}template <class T>bool MYlinklist<T>::first(T &element){if (head == NULL){return false;}current = head;element = head->data;return true;}template <class T>bool MYlinklist<T>::find(const T &element){node<T> *tempnode = head;if (tempnode == NULL){return false;}while(tempnode != NULL){if (tempnode->data == element){return true;}tempnode = tempnode->next;}return false;}template <class T>bool MYlinklist<T>::remove(const T &element){bool flag = false;node<T> *tempnode = head;while(tempnode->data == element){node<T> *ptr = tempnode;tempnode = head = head->next;delete ptr;flag = true;}while(tempnode->next != NULL){if (tempnode->next->data == element){node<T> *ptr = tempnode->next;tempnode->next = ptr->next;delete ptr;flag = true;continue;}tempnode = tempnode->next;}if (flag){return true;}elsereturn false;}template <class T>void MYlinklist<T>::printList(){node<T>* ptr = head;while(ptr != NULL){cout<<ptr->data<<" ";ptr = ptr->next;}cout<<endl;}int main(int argc, char *argv[]){string str[5] = {"a", "b", "c", "d", "e"};MYlinklist<string> strlist;for (int i = 0; i < 5; i++){strlist.insertTail(str[i]);}strlist.printList();strlist.insertHead(str[0]);strlist.printList();strlist.insertTail(str[4]);strlist.printList();if (strlist.find(str[3])){ cout<<"find it"<<endl;}strlist.remove(str[0]);strlist.printList();strlist.remove(str[4]);strlist.printList();system("PAUSE");return EXIT_SUCCESS;}


0 0