/* 用 new 和 delete 实现单链表的 增,删,改,查. 释放和显示
* 包括->头插,尾插,有序插.
*/
#include <iostream>
using namespace std;
struct Data {
int id;
string name;
int age;
};
struct Node {
Data data;
Node* next;
};
using Functor = bool(*)(const Data&,const Data&);
bool InsertNodeFront(Node*& h, Data a)
{
h = new Node{a, h};
return true;
}
bool InsertNodeBack(Node*& h, Data a)
{
auto t = h;
auto p = new Node;
if (p == NULL) {
return false;
}
p->data = a;
p->next = NULL;
if (h == NULL) {
h = p;
} else {
while (t->next != NULL) {
t = t->next;
}
t->next = p;
}
return true;
}
bool RemoveNodeById(Node*& h, int id)
{
Node* prev = NULL;
Node* pfid = h;
while (pfid!=NULL && pfid->data.id!=id) {
prev = pfid;
pfid = pfid->next;
}
if (pfid == NULL) {
return false;
} else if (pfid == h) {
h = pfid->next;
} else if (pfid->next == NULL) {
prev->next = NULL;
} else {
prev->next = pfid->next;
}
delete pfid;
return true;
}
//选择排序:
void SortList1(Node*& h)
{
Node* newH = NULL;
while (h != NULL) {
Node* prev = NULL;
Node* pmax = h;
for(Node* p=h; p->next!=NULL; p=p->next) {
if (p->next->data.id > pmax->data.id) {
prev = p;
pmax = p->next;
}
}
if (prev != NULL) {
prev->next = pmax->next;
} else {
h = pmax->next;
}
pmax->next = newH;
newH = pmax;
}
h = newH;
}
//插入排序:
void SortList2(Node*& h)
{
Node* newH = h;
h = h->next;
newH->next = NULL;
while (h != NULL) {
Node* t = newH;
Node* prev = NULL;
Node* p = h;
h = h->next;
while (t!=NULL && t->data.id<p->data.id) {
prev = t;
t = t->next;
}
if (prev != NULL) {
prev->next = p;
p->next = t;
} else {
p->next = newH;
newH = p;
}
}
h = newH;
}
bool CmpId(const Data& a, const Data& b)
{
return a.id < b.id;
}
bool CmpAge(const Data& a, const Data& b)
{
return a.age < b.age;
}
bool CmpName(const Data& a, const Data& b)
{
return a.name < b.name;
}
void SortList3(Node*& h, Functor fun)
{
Node* newH = h;
h = h->next;
newH->next = NULL;
while (h != NULL) {
Node* t = newH;
Node* prev = NULL;
Node* p = h;
h = h->next;
while (t!=NULL && fun(t->data, p->data)) {
prev = t;
t = t->next;
}
if (prev != NULL) {
prev->next = p;
p->next = t;
} else {
p->next = newH;
newH = p;
}
}
h = newH;
}
//链表反序:
void reverse(Node*& h)
{
Node* prev = NULL;
Node* curr = h;
Node* next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
h = prev;
}
void ShowList(Node* h)
{
while (h != NULL) {
cout << "--------------------" << endl;
cout << "id : " << h->data.id << endl;
cout << "name: " << h->data.name << endl;
cout << "age : " << h->data.age << endl;
h = h->next;
}
}
void FreeList(Node*& h)
{
while (h != NULL) {
auto t = h;
h = h->next;
delete t;
}
}
int main()
{
Data a = {1001, "zhangsan", 35};
Data b = {1002, "lisi", 65};
Data c = {1004, "wangwu", 32};
Data d = {1003, "liliu", 34};
Data e = {1013, "chenqi", 31};
Node* header = NULL;
InsertNodeBack(header, c);
InsertNodeBack(header, b);
InsertNodeBack(header, d);
InsertNodeBack(header, a);
//RemoveNodeById(header, 2);
ShowList(header);
cout << "------排序后-----" << endl;
SortList3(header, CmpAge);
ShowList(header);
FreeList(header);
}