来淄博旅游(链表)

写这道题快累死了,脑袋跟浆糊似的,做了好久才发现有比较简便的做法。。。。。、

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 struct node
  5 {
  6     char name[21];
  7     char f[21];
  8     char t[21];
  9     int flag;
 10     struct node *next;
 11 };
 12 void from(node *head)
 13 {
 14     node *p=head;
 15     int fl;
 16     char d[21];
 17     while(p)
 18     {
 19         fl=0;
 20         node *q,*a;
 21         q=p->next;a=p->next;
 22         strcpy(d,q->f);
 23         printf("%s :",d);
 24         q=p;
 25         while(q->next!=NULL)
 26         {
 27             if(strcmp(d,q->next->f)==0&&q->next->flag==0)
 28             {
 29                 printf(" %s",q->next->name);
 30                 q->next->flag=1;
 31                 q=q->next;
 32             }
 33             else q=q->next;
 34         }
 35         printf("\n");
 36         while(a->next!=NULL)
 37         {
 38             if(a->next->flag==0)
 39             {
 40                 fl=1;
 41                 break;
 42             }
 43             else a=a->next;
 44         }
 45         if(fl)
 46             p=a;
 47         else p=NULL;
 48     }
 49 }
 50 void to(node *head)
 51 {
 52     node *p=head;
 53     node *q;
 54     q=p->next;
 55         printf("zichuan :");
 56         q=p;
 57         while(q->next!=NULL)
 58         {
 59             if(strcmp("zichuan",q->next->t)==0)
 60             {
 61                 printf(" %s",q->next->name);
 62                 q=q->next;
 63             }
 64             else q=q->next;
 65         }
 66         printf("\n");
 67         printf("linzi :");
 68         q=p;
 69         while(q->next!=NULL)
 70         {
 71             if(strcmp("linzi",q->next->t)==0)
 72             {
 73                 printf(" %s",q->next->name);
 74                 q=q->next;
 75             }
 76             else q=q->next;
 77         }
 78         printf("\n");
 79         printf("zhoucun :");
 80         q=p;
 81         while(q->next!=NULL)
 82         {
 83             if(strcmp("zhoucun",q->next->t)==0)
 84             {
 85                 printf(" %s",q->next->name);
 86                 q=q->next;
 87             }
 88             else q=q->next;
 89         }
 90         printf("\n");
 91         printf("boshan :");
 92         q=p;
 93         while(q->next!=NULL)
 94         {
 95             if(strcmp("boshan",q->next->t)==0)
 96             {
 97                 printf(" %s",q->next->name);
 98                 q=q->next;
 99             }
100             else q=q->next;
101         }
102         printf("\n");
103     }
104 
105 void init(node *head)
106 {
107     node *p;
108     p=head->next;
109     while(p)
110     {
111         p->flag=0;
112         p=p->next;
113     }
114 }
115 int main()
116 {
117     int n,i;
118 
119     node *head,*tail,*p;
120     head=(struct node *)malloc(sizeof(struct node));
121     head->next=NULL;
122     tail=head;
123     scanf("%d",&n);
124     for(i=1; i<=n; i++)
125     {
126         p=(node *)malloc(sizeof(node));
127         scanf("%s%*c%s%*c%s%*c",p->name,p->f,p->t);
128         p->flag=0;
129         p->next=NULL;
130         tail->next=p;
131         tail=p;
132     };
133     from(head);
134     to(head);
135     return 0;
136 }

代码很长,因为to外部函数用了个很笨的方法,一开始以为是和from一样的,白瞎了好长时间。。。

原文地址:https://www.cnblogs.com/mafangfang/p/2976285.html