双向循环链表练习的两个链表分别表示男女生
注释掉的地方是错误的!
#include <stdio.h>
#include <stdlib.h>
#define N 3
struct node
{
char name[30];
int age;
struct node * next;
struct node * prior;
};
typedef struct node Node;
typedef struct node* Link;
void create_node(Link *node) //创建结点
{
*node = (Link)malloc(sizeof(Node));
if(*node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void create_link(Link *head) //创建链表
{
create_node(head);
(*head)->prior = (*head)->next = *head;
}
void insert_node_tail(Link head,Link new_node) //尾插
{
new_node->next = head;
new_node->prior = head->prior;
head->prior = new_node;
new_node->prior->next = new_node;
}
void display_link(Link head) //输出
{
Link p;
if(head == NULL)
{
printf("no such link!\n");
return;
}
p = head->next;
if(p == NULL)
{
printf("link is empty!\n");
return;
}
while(p != head)
{
printf("%d\t%s\n",p->age,p->name);
p = p->next;
}
}
/*void merge_link(Link head,Link boy,Link girl) //合并链表
{
Link p,q;
Link m;
Link t;
p = boy->next;
q = girl->next;
m = head;
if(boy == NULL || girl == NULL)
{
printf("no link!\n");
return;
}
else if(p == NULL || q == NULL)
{
printf("someone's link is empty!\n");
return;
}
else
{
int min;
min = p->age;
while(p != boy && q != girl)
{
if(min > p->next->age)
{
min = p->next->age;
t = p;
t->prior = head;
t->next = head->next;
head->next = t;
t->prior->next = t;
m = head->next;
p = p->next;
}
while(m != head)
{
if(q->age > m->age)
{
q->prior = m;
q->next = m->next;
m->next = q;
q->prior->next = q;
q = q->next;
}
else
{
m = m->prior;
}
}
if(m == head)
{
q->prior = head;
q->next = head->next;
head->next = q;
q->prior->next = q;
}
}
}
}
*/
int main()
{
Link boy = NULL;
Link girl = NULL;
Link b = NULL;
Link g = NULL;
//创建链表
create_link(&boy);
create_link(&girl);
int i;
for(i = 1;i < N;i++)
{
//分配新的结点空间
create_node(&b);
create_node(&g);
printf("第%d个男生年龄姓名:",i);
scanf("%d%s",&b->age,&b->name);
printf("第%d个女生年龄姓名:",i);
scanf("%d%s",&g->age,&g->name);
//尾插
insert_node_tail(boy,b);
insert_node_tail(girl,g);
}
display_link(boy);
display_link(girl);
Link head = NULL;
create_link(&head);
// merge_link(head,boy,girl); //合并链表
// display_link(head);
return 0;
}