SDUT 1138 数据结构上机测试2-1:单链表操作A

 1 # include <stdio.h>
 2 # include <malloc.h>
 3 
 4 struct node
 5 {
 6     int date;
 7     struct node *next;
 8 } ;
 9 
10 struct node *creat(int n)
11 {
12     struct node *head, *tail, *p;
13     head = (struct node *)malloc(sizeof(struct node));
14     head -> next = NULL;
15     tail = head;
16     for(int i = 0; i < n; i++)
17     {
18         p = (struct node *)malloc(sizeof(struct node));
19         scanf("%d", &p -> date);
20         p -> next = NULL;
21         tail -> next = p;
22         tail = p;
23     }
24     return head;
25 }
26 
27 void Delete(struct node *&head, int n, int m)
28 {
29     struct node *p, *q;
30     p = head;
31     while(p -> next != NULL)
32     {
33         if(p -> next -> date == m)
34         {
35             n--;
36             q = p -> next;
37             p -> next = q -> next;
38             free(q);
39         }
40         else
41             p = p -> next;
42     }
43     printf("%d\n", n);
44 }
45 
46 void output(struct node *head)
47 {
48     struct node *r = head;
49     while(r -> next -> next != NULL)
50     {
51         printf("%d ", r -> next -> date);
52         r = r -> next;
53     }
54     printf("%d\n", r -> next -> date);
55 }
56 
57 int main(void)
58 {
59     int n, m;
60     scanf("%d", &n);
61     struct node *head;
62     head = creat(n);
63     scanf("%d", &m);
64     printf("%d\n", n);
65     output(head);
66     Delete(head, n, m);
67     output(head);
68 
69     return 0;
70 }
View Code

 

 

/**************************************
 Problem id : SDUT OJ 1138
 User name : Silence—Debug
 Result  : Accepted
 Take Memory : 280K
 Take Time : 0MS
 Submit Time : 2013-03-16 12:51:59 
**************************************/

 

 

posted @ 2013-03-16 12:59  GLSilence  阅读(257)  评论(0)    收藏  举报