首页 > 代码库 > 单链表
单链表
#include <iostream> using namespace std;template <typename T>class List{private: template <typename N> struct Node { N data, *next; Node() { data = 0; next = NULL; } Node(N x, N *n = NULL ): data(x), next(n) {} }; Node<T> *Head; int last = 0; public: List() { Head = new Node<T>; } List(const T &x) { Head = new Node<T>; Head->next = new Node<T>(x); last++; } List(List<T> &L) { this->makeEmpty(); for(Node *p = Head->next; p != NULL; p = p->next) this->Insert(i, new Node(p->data)); } ~List() { makeEmpty(); } void makeEmpty(Node<T> *p = Head->next) { if(p->next != NULL) makeEmpty(p->next); delete p; } int Length() const { return last; } Node<T>* getHead() const { return Head; } Node<T>* Search(T x) { for(Node<T> *p = Head->next; p!=NULL; p = p->next) if(p->data =http://www.mamicode.com/= x) return p; return NULL; } Node<T>* Locate(int i) { for(Node<T> *p = Head->next, int r = 1; p != NULL; p = p->next, r++) if(r == i) return p; return NULL: } bool getData(int i, T &x) const { if(Node *p = Locate(i) != NULL) { x = p->data; return true; } return false; } bool setData(int i, T &x) { if(Node *p = Locate(i) != NULL) { p->data =http://www.mamicode.com/ x; return true; } return false; } bool Insert(int i , T &x) { last++; for(Node<T> *p = Head->next, int r = 1; p != NULL; p = p->next, r++) if(r + 1 == i || p->next = NULL) p->next = new Node(x, p->next); } bool Remove(int i, T &x) { if(Node *p = Locate(i - 1) != NULL || p->next != NULL) { Node *d = p->next; p->next = d->next; delete d; return true; } return false; } bool IsEmpty() const { return Head->next == NULL ? true : false; } bool IsFull() const { return false; } void sortInsert(T x) { node *p = Head->next; if(p->next == NULL) { p->next = new node(x, NULL); return ; } for(; p->next != NULL; p = p->next) { if(p->next->num > x) { p->next = new node(x, p->next); return ; } } p->next = new node(x, NULL); } void Sort() { Node *head = new Node(); for(Node *p = Head->next; p != NULL; p = p->next) head->sortInsert(p->data); this.makeEmpty(); this.Head = head; } void input() { int n; cin >> n; for(int i = 0; i < n; i++) { int x; cin >> x; this->Insert(i + 1; x); } } void output() { cout << "{"; for(Node *p = Head->next; p != NULL; p = p->next) cout << p->data << " "; cout << "}" << endl; } List<T> &operator = (List<T> &L) { this->makeEmpty(); for(Node *p = L.Head->next, Node *P = Head; p != NULL; p = p->next, P = P->next) P->next = new(p->data); return this->Head; }}; int main(){ }
今晚思路好乱, 我他喵的也不知到我在写什么了, 代码还没编译通过, 贴上方便用手机看。。。
单链表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。