链表基础练习题(1)

时间限制:1000 ms  |  内存限制:1000 KB
 
描述

已知线性表,要求删除线性表内的数大于等于MIN,小于等于

 

MAX的,并输出删除后的线性表

 

要求:请使用链表做,否则不计成绩!

输入

第一行包含一个数T表示有T组数据

 

每组数据第一行包含3个数字N,MIN,MAX,分别表示有N个数据,删除范围为MIN-MAX

 

第二行包含初始的N个数据

输出

输出删除数据后的线性表

样例输入
2
3 1 2
1 2 3
5 2 1
1 1 1 1 1
样例输出
3
1 1 1 1 1
题目链接:http://www.bianchengla.com/team/123/practise/problem?id=1339
【code】
 1 #include <stdio.h>
2 #include <stdlib.h>
3 struct node
4 {
5 int data;
6 struct node*next;
7 };
8 struct node*creat(int n)
9 {
10 int i;
11 struct node*head,*p,*tail;
12 head=(struct node*)malloc(sizeof(struct node));
13 head->next=NULL;
14 tail=head;
15 for(i=0;i<n;i++)
16 {
17 p=(struct node*)malloc(sizeof(struct node));
18 scanf("%d",&p->data);
19 p->next=NULL;
20 tail->next=p;
21 tail=p;
22 }
23 return head;
24 }
25 void dele(struct node*k,int n,int m)
26 {
27 struct node*p,*q;
28 int flag=0;
29 p=k;
30 while(p->next!=NULL)
31 {
32 while(p->next!=NULL)
33 {
34 flag=0;
35 if((p->next->data>=n)&&(p->next->data<=m))
36 {
37 flag=1;
38 break;
39 }
40 else
41 p=p->next;
42 }
43 if(flag==1)
44 {
45 q=p->next;
46 p->next=q->next;
47 free(q);
48 }
49 }
50 }
51 void list(struct node*l)
52 {
53 struct node *r;
54 r=l;
55 while(r->next->next!=NULL)
56 {
57
58 printf("%d ",r->next->data);
59 r=r->next;
60 }
61 printf("%d\n",r->next->data);
62 }
63 int main()
64 {
65 int n,m,t,s;
66 scanf("%d",&t);
67 while(t--)
68 {
69 struct node*p;
70 scanf("%d%d%d",&s,&n,&m);
71 p=creat(s);
72 dele(p,n,m);
73 if(p->next!=NULL) //这里一定要注意。。
74 list(p);
75 }
76 return 0;
77 }
 
myblog:www.bearac.com

 
posted on 2012-02-29 21:33  pony1993  阅读(392)  评论(0编辑  收藏  举报

View My Stats