linklist.h
#pragma once #include<stdio.h> #include<stdlib.h> typedef int datatype; typedef struct LinkNode { datatype data; struct LinkNode *pNext; }node; node *addback(node *phead, datatype data);//尾部添加节点 void addhead(node **pphead, datatype data);//头插 void showall(node *phead);//显示 void revshowall(node *phead); node *searchfirst(node *phead, datatype data); node *changefirst(node *phead, datatype data, datatype newdata); node *deletefirst(node *phead, datatype data);//删除 node *insertfirst(node *phead, datatype data, datatype newdata); node *insertback(node *phead, datatype data, datatype newdata); void selectsort(node *phead); void myquicksort(node *phead, node *pback); int getnum(node *phead); node *revlist(node *phead); node *getmin(node *phead);//获取中间值 node *revlistdigui(node *phead); node *addlinklist(node *list1, node *list2);
linklist.c
#include"linklist.h" void addhead(node **pphead, datatype data)//头插 { node *pnew = malloc(sizeof(node)); pnew->data = data; pnew->pNext = NULL; if (*pphead==NULL) { *pphead = pnew; } else { pnew->pNext = *pphead; *pphead = pnew; } } //尾插,改变一个指针,需要指针的地址,或者用返回值给指针赋值 node *addback(node *phead, datatype data)//尾部添加节点 { node *pnew = malloc(sizeof(node)); pnew->data = data; pnew->pNext = NULL; if (phead==NULL) { phead = pnew; } else { node *ptemp = phead; //备份头结点 while (ptemp->pNext!=NULL) { ptemp = ptemp->pNext; } ptemp->pNext = pnew; } return phead; } void showall(node *phead)//显示 { if (phead==NULL) { return; } else { printf("%d %p %p\n", phead->data, phead, phead->pNext); showall(phead->pNext); } } void revshowall(node *phead)//反转显示 { if (phead == NULL) { return; } else { revshowall(phead->pNext); printf("%d %p %p\n", phead->data, phead, phead->pNext); } } node *searchfirst(node *phead, datatype data) { for (node *p=phead;p!=NULL ;p=p->pNext) { if (p->data==data) { return p; } } return NULL; } node *changefirst(node *phead, datatype data,datatype newdata) { for (node *p = phead; p != NULL; p = p->pNext) { if (p->data == data) { p->data = newdata; return phead; } } return NULL; } node *deletefirst(node *phead, datatype data) { node *p1, *p2=NULL; p1 = phead; while (p1!=NULL) { if (p1->data!=data) { p2 = p1; p1 = p1->pNext; } else { break; } } if (p1==phead) { phead = phead->pNext; free(p1); } else { p2->pNext = p1->pNext; free(p1); } return phead; } node *insertfirst(node *phead, datatype data, datatype newdata) { node *p1, *p2 = NULL; p1 = phead; while (p1!=NULL) { if (p1->data==data) { break; } else { p2 = p1; p1 = p1->pNext; } } node *pnew = malloc(sizeof(node)); pnew->data = newdata; pnew->pNext = NULL; if (p1==phead) { pnew->pNext = phead; phead = pnew; } else { p2->pNext = pnew; pnew->pNext = p1; } } node *insertback(node *phead, datatype data, datatype newdata) { node *p1; p1 = phead; while (p1!=NULL) { if (p1->data==data) { break; } else { p1 = p1->pNext; } } node *pnew = malloc(sizeof(node)); pnew->data = newdata; pnew->pNext = p1->pNext; p1->pNext = pnew; return phead; } void selectsort(node *phead) { for (node *p1=phead; p1!=NULL; p1=p1->pNext) { for (node *p2 = phead; p2!=NULL ; p2=p2->pNext) { if (p1->data<p2->data) { datatype temp; temp = p1->data; p1->data = p2->data; p2->data = temp; } } } return phead; } void myquicksort(node *phead, node *pback) //快速排序法 { if (phead==pback) { return; } else { int key = phead->data;//获取第一个值,分界线 node *p1 = phead; for (node *p2 = phead->pNext; p2!=pback; p2=p2->pNext) { if (p2->data<key) { p1 = p1->pNext; datatype temp = p1->data; p1->data = p2->data; p2->data = temp; } } datatype temp2 = p1->data; p1->data = phead->data; phead->data = temp2; myquicksort(phead, p1); myquicksort(p1->pNext, pback); } } int getnum(node *phead) { int i = 0; for (; phead != NULL; i++, phead = phead->pNext); return i; } //翻转 node *revlist(node *phead) { if (phead==NULL||phead->pNext==NULL) { return NULL; } else { node *pre = NULL; node *pcur = NULL; node *pnext = NULL; pre = phead; //第一个节点 pcur = phead->pNext; while (pcur!=NULL) { pnext = pcur->pNext; //下一个节点,备份下一个节点 pcur->pNext = pre; //指针翻转 pre = pcur;//前进 pcur = pnext; } phead->pNext = NULL; phead = pre; return phead; } } node *revlistdigui(node *phead) { if (phead==NULL||phead->pNext==NULL) { return phead; } else { node *pnext = phead->pNext; node *head=revlistdigui(pnext); //递归调用 pnext->pNext = phead; phead->pNext = NULL; return head; } } node *getmin(node *phead) { if (phead==NULL||phead->pNext==NULL) { return phead; } else { node *p1=phead; node *p2=phead; while (p2->pNext!=NULL) { p1 = p1->pNext; p2 = p2->pNext; if (p2->pNext!=NULL) { p2 = p2->pNext; } } return p1; } } node *addlinklist(node *list1, node *list2) { node *temp = list1; while (temp->pNext!=NULL) { temp = temp->pNext; } temp->pNext = list2; return list1; }
main.c
#include"linklist.h" void main() { node *phead=NULL; phead = addback(phead, 11); phead = addback(phead, 12); phead = addback(phead, 13); phead = addback(phead, 14); phead = addback(phead, 15); phead = addback(phead, 16); addhead(&phead, 20); addhead(&phead, 21); addhead(&phead, 22); addhead(&phead, 23); addhead(&phead, 24); //node *pfind = searchfirst(phead, 21); //printf("%d %p\n", pfind->data, pfind); insertback(phead, 16, 459); addback(phead, 555); //selectsort(phead); //myquicksort(phead, NULL); //showall(phead); //phead=revlistdigui(phead); node *phead2 = NULL; phead2 = addback(phead2, 999); phead2 = addback(phead2, 888); phead2 = addback(phead2, 777); phead=addlinklist(phead, phead2); phead=revlist(phead); selectsort(phead); showall(phead); //node *ptemp = getmin(phead); //printf("\n%d %p", ptemp->data,ptemp); system("pause"); }