删除单链表中大于minK且小于maxK的元素
已知单链表中各结点的元素值为整型且自增有序,设计算法删除单链表中大于minK且小于maxK的所有元素,并释放被删结点的存储空间。中间的deleteList函数怎么写?为什么?求大佬指教
#include<iostream>
usingnamespacestd;
typedefintDataType;
typedefstructnode{
DataTypedata;
node*next;
}node;
node*first;
voidinit()
{
first=newnode;
first->next=NULL;
node*rear=first;
intlength;
cin>>length;
for(inti=0;i<length;++i){
DataTypeelem;
cin>>elem;
node*s=newnode;
s->data=elem;
s->next=NULL;
rear->next=s;
rear=s;
}
}
voiddeleteList(DataTypeminK,DataTypemaxk)
{
}
voidshow()
{
node*p=first->next;
if(p==NULL)cout<<"Empty";
while(p!=NULL){
cout<<p->data<<"";
p=p->next;
}
cout<<endl;
}
intmain()
{
init();
DataTypeminK,maxK;
cin>>minK>>maxK;
deleteList(minK,maxK);
show();
return0;
} node *p = new node();
node *q = new node();
p = first;
q = first->next;
while(q != NULL)
{
if(q->data>minK&&q->data<maxk)
{
p->next = q->next;
delete q;
q = p->next;
}
else
{
p = p->next;
q = p->next;
}
}
已解决
页:
[1]