线性表——删除链表 min max之间的值
//"LinkList.h" #include<iostream> using namespace std; #define ElemType int typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList; string InitList(LinkList &L){ L = new LNode; L->next = NULL; return "OK"; } string ListInsert(LinkList &L,int i,ElemType e){ LinkList P = L; int j=0; while (P && (j<i-1)) { P = P->next;++j; } LinkList S = new LNode; S->data = e; S->next=P->next; P->next=S; return "OK"; } LNode* GetElement(LinkList L,int i){ LinkList p = L->next; int j = 1; while(p && j<i){ p=p->next; j++; } // if(!p||j>i){ // return -1; // } return p; } string DeleteList(LinkList &L,int i){ LinkList p = L; int j = 0; while ((p->next)&&(j<i-1)) { p=p->next; j++; } if(!(p->next)||(j>i-1)) return "ERROR"; LinkList q = p->next; p->next=q->next; delete q; return "OK"; } string ShowList(LinkList L){ LinkList p= L; while(p->next){ p=p->next; cout<<p->data; } return "OK"; } string FillList(LinkList &L){ int p =1; char a = '1'; while (a != '!') { ElemType b ; cin >> b; ElemType e = (ElemType)b - 48; a = b; if(a != '!') cout << ListInsert(L,p,b)<<endl; p++; } return "OK"; } string FillListWithNum(LinkList &L,int i){ int e; for(int j =1;j<=i;j++){ cin >> e; ListInsert(L,j,e); } return "OK"; } // ShowList(L); // DeleteList(L,2); // ShowList(L); // return 0; // }
#include<iostream> #include"LinkList.h" using namespace std; /* 删除 min max之间的值 */ int main(){ LinkList LA; InitList(LA); FillListWithNum(LA,8); LNode *pa = LA; LNode *pb = LA->next; int num = 0; ElemType mink = 2; ElemType maxk = 4; // cin >> mink; // cin >> maxk; while(pa->next){ num++; pa=pa->next; } for(int i=1;i<=num;i++){ if(pb->data>=mink&&pb->data<=maxk){ pb=pb->next; //需要在删除结点前提前移动至下一结点 DeleteList(LA,i); --i; //删除节点后,结点的序号和数量都要相应减一 --num; }else { pb=pb->next; } } cout<<endl; ShowList(LA); system("pause"); return 0; }