![]() |
#2
flyue2008-03-16 20:31
我自己写的链表:
#include <windows.h> struct node { int data; node *next; }; node *getone(node *n, int pos) { node *ret = n; for(int i = 0; i < pos; i++) { if(ret == NULL)return NULL; ret = ret->next; } return ret; } node *getend(node *n) { node *ret = n; while(ret->next != NULL) ret = ret->next; return ret; } ////////////////////////////////////////////////////////////////////////// class CNode { private: node *m_list; void insertnode(node *add, int pos) { node *n1 = m_list, *n2 = NULL, *n3 = NULL; if(pos <= 0) { m_list = add; m_list->next = n1; } else { n2 = getone(m_list, pos - 1); n3 = getone(m_list, pos); n2->next = add; add->next = n3; } } public: CNode() { m_list = NULL; } void add(int val, int pos) { node *addn = new node; addn->data = val; addn->next = NULL; insertnode(addn, pos); } void del(int pos) { node *tmp = getone(m_list, pos); if(pos <= 0)m_list = tmp->next; else getone(m_list, pos - 1)->next = tmp->next; delete tmp; } int get(int pos) { node *tmp; tmp = getone(m_list, pos); return tmp->data; } int num() { if(m_list == NULL)return 0; int cnt = 0; node *tmp = m_list; while(tmp->next != NULL) { tmp = tmp->next; cnt++; } return cnt + 1; } }; |
如果拿链表实现:
插入排序,
冒泡排序,
选择排序,
交换排序,
这几种排序方法,该怎么实现呢,小弟初来乍到,不懂的很多,还请个位大虾们,讲解下。