#include<iostream>
using namespace std;
typedef struct Node
{
int val;
Node *next;
}LST;
LST *mycreat(int n)
{
LST *head=NULL,*p=NULL,*sec=NULL;
head=(LST*)malloc(sizeof(LST));
p=head;
while(n--)
{
int v;
scanf("%d",&v);
sec=(LST*)malloc(sizeof(LST));
sec->val=v;
p->next=sec;
p=sec;
}
p->next=NULL;
return head;
}
void myprint(LST *head)
{
int f=0;
LST *p;
p=head->next;
while(p!=NULL)
{
if (f==0)
printf("%d",p->val);
else
printf(" %d",p->val);
f++;
p=p->next;
}
printf("\n");
return ;
}
void mydel(LST *head,int x)
{
int f=0;
LST *p=NULL,*q=NULL;
q=head;
p=head->next;
while(p!=NULL)
{
if(p->val==x)
{
f=1;
q->next=p->next;
delete p;
p=q->next;
}
else
{
p=p->next;
q=q->next;
}
}
if (f==0)
printf("error\n");
else
{
if(head->next!=NULL)
myprint(head);
}
return ;
}
LST *mysort(LST *head)
{
LST *p=NULL,*q=NULL;
p=head->next;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if (q->val<p->val)
{
int t=p->val;
p->val=q->val;
q->val=t;
}
q=q->next;
}
p=p->next;
}
return head;
}
int main()
{
int n,m;
int f=0;
while(~scanf("%d",&n))
{
if(f) printf("\n");
LST *head;
head=mycreat(n);
scanf("%d",&m);
head=mysort(head);
myprint(head);
mydel(head,m);
f++;
}
return 0;
}
删除单链表中特定值
最新推荐文章于 2022-10-08 21:22:18 发布