链表的创建与输出

创建两个学生链表,含有姓名,年龄的信息,一个链表存放男生,一个链表存放女生

并且合并这两个链表。



#include <stdio.h>
#include <stdlib.h>


#define LEN sizeof (struct student)


struct student
{
char name[100];
int age;
struct student *next;
};


int n;


struct student *create ()
{


struct student *head;
struct student *p1 = NULL;
struct student *p2 = NULL;


n = 0;
p1 = (struct student *) malloc (LEN);
p2 = p1;


if (p1 == NULL)
{
printf ("Cann't create it, try it again in a monent!\n");
return NULL;
}
else
{
head = NULL;
printf ("Please input %d node -- name, year: \n",n+1);
scanf ("%s",&(p1->name));
scanf ("%d",&(p1->age));
}

while (strcmp(p1->name,"0") != 0)
{
n += 1;
if (n == 1)
{
head = p1;
p2 -> next = NULL;
}
else 
{
p2 -> next = p1;
}


p2 = p1;


p1 = (struct student *) malloc (LEN);
printf ("Please input %d node -- name,age: \n",n+1);
scanf ("%s",&(p1->name));
if (strcmp(p1->name,"0") == 0)
break;
scanf ("%d",&(p1->age));


}


p2->next = NULL;
free (p1);
p1 = NULL;
return head;
}


void printA (struct student *head)
{
struct student *p;
printf ("Now, these %d records are: \n",n);
p = head;


if (p != NULL)
{
printf ("head is %p\n",p);
while (p != NULL)
{
printf ("%p   %s   %d   %p\n", p, p->name, p->age, p->next);
p = p->next;
}
}


}

struct student *comb(struct student *b, struct student *g)
{
struct student *p = NULL;
p = b;
while (p->next != NULL)
{
p = p->next;
}
p->next = g;
return b;
}




int main()
{
struct student *boy = NULL;
struct student *girl = NULL;


boy = create ();
printA (boy);
girl = create ();
printA (girl);


printA( comb (boy,girl) );


    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值