下面给出了完整的实现
#include#include using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型typedef struct LNode { ElemType data; //结点的数据域 struct LNode *next; //结点的指针域} LNode, *LinkList; //LinkList为指向结构体LNode的指针类型void CreateList_R(LinkList &L, int n) //算法2.12 后插法创建单链表{ LinkList p, r; int i; L = new LNode; L->next = NULL; //先建立一个带头结点的空链表 r = L; //尾指针r指向头结点 for(i = 0; i < n; i++) { p = new LNode;//生成新结点 cin >> p->data; p->next = NULL; r->next = p;//将新结点*p插入尾结点*r之后 r = p;//r指向新的尾结点*p }}//将A分成小于0的B表和大于0的C表,删除A表中大于0的组成C表void DisCompose(LinkList &A, LinkList &B, LinkList &C){ LNode *prev, *cur, *ctail; prev = B = A; cur = A->next; ctail = C = new LNode; //为C申请头结点空间 while(cur != NULL) { if(cur->data > 0) { //加入C表 prev -> next = cur->next; ctail->next = cur; //插入C表尾部 ctail = cur; cur = prev->next; } else{ prev = cur; cur = cur->next; } } ctail->next = NULL;}void display(LinkList L){ LNode *p = L->next; while(p) { cout << setw(5) << p->data; p = p->next; } cout << endl;}LNode *Max(LinkList L){ LNode *pmax, *p; pmax = p = L->next; while(p != NULL) { //如果下一个结点存在 if(p->data > pmax->data) pmax = p; //如果p的值大于pmax的值,则重新赋值 p = p->next; //遍历链表 } return pmax;}void inverse(LinkList &L){ LNode *p, *q; // 逆置带头结点的单链表 L p = L->next; L->next = NULL; while(p) { q = p->next; // q指向*p的后继 p->next = L->next; L->next = p; // *p插入在头结点之后 p = q; }}//return length of Aint Delete(ElemType A[ ], int n, ElemType item){ int i, cur; cur = 0; for(i = 0; i < n; i++){ if(A[i] != item ) A[cur++] = A[i]; } return cur;}int main(){ // -5 8 -3 6 -1 7 LinkList L, B, C; CreateList_R(L, 6); display(L); LNode *p = Max(L); if(p) cout << p->data << endl; inverse(L); display(L); DisCompose(L, B, C); display(B); display(C); ElemType A[10] = { 1, 3, 3, 200, 3, 3,-1,3,3,3}; int len = Delete(A, 10, 3); for(int i = 0; i < len; i++) cout << setw(5) << A[i]; cout << endl; return 0;}