C语言学习历程(十四) 结构体链表实现通讯录

本文介绍了使用C语言通过结构体链表实现通讯录管理的方法,包括添加、删除、查看和修改联系人信息的功能。通过结构体定义了包含姓名、性别、电话号码的联系人节点,并提供了相应的操作函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

*#include <*stdio.h>
*#include<*stdlib.h>
#include<string.h>**
#define LEN sizeof(struct student)**

struct student
{
int num;
long int phone;
char name[20];
char sex[10];

struct student *next;

};

struct student *print(struct student *head)
{
struct student *p;
p = head;

do
{
printf(“num = %d , name = %s , sex = %s , phone = %d\n”,p -> num ,p -> name,p -> sex, p -> phone);

    p = p -> next ;
}
while(p != NULL);

return NULL;
}

struct student *add(struct student *head)
{
struct student *p1,*p2,*p3;
int num,index;
char name[20];
char sex[10];
long int phone;

printf("请输入你想增加的序列号:");

scanf("%d",&index);

printf("请输入该同学的学号:");

scanf("%d",&num);

printf("请输入该同学的姓名:");

scanf("%s",name);

printf("请输入该同学的性别:");

scanf("%s",sex);

printf("请输入该同学的手机号码:");

scanf("%d",&phone);

if(head == NULL)
{
    printf("The list is NULL");
}
else 
{
    p1 = p2 = head;
    if(index == 1)
    {
        p3 = (struct student *)malloc(LEN);
        p3 -> num = num;
        strcpy(p3 -> name , name);
        strcpy(p3 -> sex , sex);
        p3 -> phone = phone;
        head = p3;
        p3 -> next = p1;
    }
    else
    {
    while(p1 -> next != NULL && p1 -> num +1 != index)
    {
        p1 = p1 -> next;
        p2 = p1;
    }
    if(p1 -> num +1 == index)
    {
        p3 = (struct student *)malloc(LEN);
        p3 -> num = num;
        strcpy(p3 -> name , name);
        strcpy(p3 -> sex , sex);
        p3 -> phone = phone;
    }
    if(p2 -> next &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值