您的位置:首页 > 其它

sdut.acm 2012级《程序设计基础Ⅱ)》_链表 来淄博旅游

2013-05-11 21:11 405 查看


#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct node
{
char name[21],from[21],to[10];
int n ;
struct node *next;

};
struct node *create(int NUM)
{
struct node *Head,*tail,*p;
int i;
Head = (struct node *)malloc(sizeof(struct node));
Head->next = NULL;
tail = Head;
for(i = 1;i <= NUM;i++)
{
p = (struct node*)malloc(sizeof(struct node));
scanf("%s %s %s",p->name,p->from,p->to);
p->n = 0;
p->next = NULL;
tail->next = p;
tail = p;
}
return (Head);
}

void print1(struct node *Head1)
{
struct node *q,*r;
q = Head1->next;
r = Head1->next;
printf("%s :",q->from,q->name);
while(q != NULL)
{
while(r != NULL)
{
if(strcmp(r->from,q->from) == 0)
{
printf(" %s",r->name);
r->n = 1;
r = r->next;

}else
{
r = r->next;
}

}
printf("\n");
q = q->next;
r = q;
while(q)
{
if( r->n == 0)
{
printf("%s :",q->from,q->name);
break;

}
else
{
q = q->next;
r = q;
}
}

}
}

void print2(struct node *Head2)
{
struct node *p;
p = Head2->next;
char TO1[8]="zichuan",TO2[6]="linzi",TO3[8]="zhoucun",TO4[7]="boshan";
printf("%s :",TO1);
while(p != NULL)
{
if(strcmp(p->to,TO1) == 0)
{
printf(" %s",p->name);
p = p->next;
}else
p = p->next;

}
printf("\n");
p = Head2->next;
printf("%s :",TO2);
while(p != NULL)
{
if(strcmp(p->to,TO2) == 0)
{
printf(" %s",p->name);
p = p->next;
}else
p = p->next;

}
printf("\n");
p = Head2->next;
printf("%s :",TO3);
while(p != NULL)
{
if(strcmp(p->to,TO3) == 0)
{
printf(" %s",p->name);
p = p->next;
}else
p = p->next;

}
printf("\n");
p = Head2->next;
printf("%s :",TO4);
while(p !=NULL)
{
if(strcmp(p->to,TO4) == 0)
{
printf(" %s",p->name);
p = p->next;
}else
p = p->next;
}

free(p);

}
int main()
{
struct node *head1,*head2;
int num;
scanf("%d",&num);
head1 = create(num);
head2 = head1;
print1(head1);
print2(head2);

return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: