为了方便在c++中完成
#include<stdio.h> #include<malloc.h> #include<assert.h> typedef int datatype; typedef struct SingleLinkList { datatype _data; SingleLinkList *_nextlist; }SingleLinkList; void PrintList(SingleLinkList* phead) { SingleLinkList *cur=phead; while (cur!=NULL) { printf("%d", cur->_data); cur = cur->_nextlist; } } SingleLinkList * BuyNote(datatype x) { SingleLinkList *tmp; tmp = (SingleLinkList*)malloc(sizeof(SingleLinkList)); tmp->_data = x; tmp->_nextlist = NULL; return tmp; } void PushFrount(SingleLinkList* &phead, datatype x) { if (phead == NULL) { phead = BuyNote(x); } else { SingleLinkList *tmp; tmp = BuyNote(x); tmp->_nextlist = phead; phead = tmp; } } void PushTail(SingleLinkList* &phead ,datatype x) { assert(phead); if (phead == NULL) { phead = BuyNote(x); } else { SingleLinkList *cur = phead; while (cur->_nextlist != NULL) { cur = cur->_nextlist; } SingleLinkList *tmp = BuyNote(x); cur->_nextlist = tmp; } } void PopFront(SingleLinkList* &phead) { assert(phead); if (phead == NULL) { printf("没有要删除的"); } else if (phead->_nextlist == NULL) { SingleLinkList *tmp = phead; phead = NULL; free(tmp); } else { SingleLinkList *cur = phead; phead = phead->_nextlist; free(cur); } } void PopTail(SingleLinkList* &phead) { assert(phead); if (phead == NULL) { printf("没有要删除的"); } else if (phead->_nextlist == NULL) { free(phead); phead = NULL; } else { SingleLinkList *cur = phead; while (cur->_nextlist->_nextlist != NULL) { cur = cur->_nextlist; } SingleLinkList *tmp = cur->_nextlist->_nextlist; free(tmp); cur->_nextlist = NULL; } } SingleLinkList *find(SingleLinkList* &phead, datatype x) { assert(phead); SingleLinkList *tmp = phead; while (tmp) { if (tmp->_data == x) { return tmp; } tmp = tmp->_nextlist; } printf("没有抱歉"); return 0; } void Erase(SingleLinkList* &phead,datatype x) { assert(phead); SingleLinkList *tmp = phead; if (tmp->_data == x) { free(tmp); phead =NULL; } else if ((tmp->_nextlist->_data == NULL) && tmp->_nextlist->_nextlist == NULL) { SingleLinkList *ret = tmp->_nextlist; free(ret); tmp->_nextlist = NULL; } while (tmp->_nextlist) { if (tmp->_nextlist->_data == x) { SingleLinkList *ret = tmp->_nextlist; tmp->_nextlist = ret->_nextlist; free(ret); } tmp = tmp->_nextlist; } } void Insert(datatype x, SingleLinkList * &pos) { assert(pos); SingleLinkList *ret = BuyNote(x); SingleLinkList *tmp = pos; if (tmp->_nextlist == NULL) { tmp->_nextlist = ret; ret->_nextlist = NULL; } ret->_nextlist = tmp->_nextlist; tmp->_nextlist = ret; } SingleLinkList * Resever(SingleLinkList * &phead) { assert(phead); if (phead->_nextlist == NULL) { return phead; } else if (phead->_nextlist->_nextlist == NULL) { SingleLinkList *tmp = phead->_nextlist; tmp->_nextlist = phead; phead->_nextlist = NULL; return tmp; } else { SingleLinkList *cur = phead; SingleLinkList *tmp; SingleLinkList *newphead=NULL; while (cur) { tmp = cur; cur = cur->_nextlist; tmp->_nextlist = newphead; newphead = tmp; } return newphead; } } SingleLinkList *MidList(SingleLinkList* &phead) { assert(phead); SingleLinkList *slowstratlist = phead; SingleLinkList *faststratlist = phead; if (phead->_nextlist->_nextlist == NULL) { return phead; } else { while (faststratlist->_nextlist) { slowstratlist = slowstratlist->_nextlist; faststratlist = faststratlist->_nextlist->_nextlist; } return slowstratlist; } } int main() { Test1(); }