1056 25 12 33 66 54 7 12 33 1212Example Output
1056 25 12 33 66 54 7 12 33 12756 25 33 66 54 7 3301
#include<stdio.h> |
02 | #include<stdlib.h> |
03 | #include<string.h> |
04 | struct node |
05 | { |
06 | int data; |
07 | struct node *next; |
08 | }; |
09 | struct node *creat(int n) |
10 | { |
11 | struct node *head,*p,*q; |
12 | int i; |
13 | head=(struct node *)malloc(sizeof(struct node)); |
14 | head->next=NULL;q=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;q->next=p; |
20 | q=p; |
21 | } |
22 | q=head->next; |
23 | return head; |
24 | }; |
25 | int del(struct node *head,int n,int m) |
26 | { |
27 | struct node *p,*q; |
28 | p=head; |
29 | while(p->next!=NULL) |
30 | { |
31 | if(p->next->data==m) |
32 | { |
33 | q=p->next; |
34 | p->next=q->next; |
35 | free(q); |
36 | n--; |
37 | } |
38 | else |
39 | p=p->next; |
40 | } |
41 | return n; |
42 | } |
43 | void show(struct node *head) |
44 | { |
45 | struct node *p; |
46 | p=head->next; |
47 | while(p!=NULL) |
48 | { |
49 | if(p->next!=NULL) |
50 | printf("%d ",p->data); |
51 | else |
52 | printf("%d/n",p->data); |
53 | p=p->next; |
54 | } |
55 | } |
56 | void main() |
57 | { |
58 | int n,m,flag; |
59 | struct node *head; |
60 | scanf("%d",&n); |
61 | head=creat(n); |
62 | scanf("%d",&m); |
63 | printf("%d/n",n); |
64 | show(head); |
65 | flag=del(head,n,m); |
66 | printf("%d/n",flag); |
67 | show(head); |
68 | } |
新聞熱點
疑難解答