已知一组数据,采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素
输入格式:
输入包含三行数据,第一行是表中元素个数,第二行是顺序表的各个元素,第三行是区间x和y。
输出格式:
删除元素值在[x,y]之间的所有元素后,输出新的顺序表。(最后无空格)
输入样例:
在这里给出一组输入。例如:
10
55 11 9 15 67 12 18 33 6 22
10 20
输出样例:
在这里给出相应的输出。例如:
55 9 67 33 6 22
思路:
首先用尾插法插入数据
然后遍历链表找出需要删除的结点,使得该结点上一个结点指向该节点下一个结点
最后遍历新链表,打印出数据
代码:
#include<stdio.h>
struct Node
{
int data;
struct Node* next;
};
int main()
{
int n;
scanf("%d",&n);
struct Node *head,*p1,*p2;
head=(struct Node*)malloc(sizeof(struct Node));
head->next=NULL;
p2=head;
int i;
for(i=0; i<n; i++)
{
p1=(struct Node*)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
p1->next=p2->next;
p2->next=p1;
p2=p2->next;
}
int a,b;
scanf("%d%d",&a,&b);
struct Node *p4=head->next;
while(p4->next!=NULL)
{
struct Node *p5=p4->next;
if(p5->data>=a&&p5->data<=b)
{
p4->next=p4->next->next;
}
else
{
p4=p4->next;
}
}
struct Node *p3=head->next;
while(p3->next!=NULL)
{
printf("%d ",p3->data);
p3=p3->next;
}
printf("%d\n",p3->data);
return 0;
}