使用单链表制作电子通讯录

使用单链表制作电子通讯录

在这里插入图片描述

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

typedef struct people
    {       
        char name[10];
        long int num;
		int add;
        struct people *next;

    }people;
    
typedef struct people *List;


int  add(List *L);
int delet(List *L);
int find(List *L);
int Exit(List *L);
int out(List *L);


int add(List *L)
{
	List s,f;
	f=(*L)->next;
	int i;
	    s=(List)malloc(sizeof(people));//建立新联系人

  printf("\n输入你想插入的位置(第一次就是第一个位置也就是1第二次第二个2,以此类推):\n");
    scanf("%d", &i);
                for (int j=0; j<i-1; j++)
                {
                    f=f->next;
                }
						    printf("\n请输入您要添加的姓名:\n");
	
					  scanf("%s",s->name);
					  			    printf("请输入您要添加的号码:\n");
    scanf("%d",&s->num);
	    s->next = (*L)->next;
        (*L)->next = s;
		return 0;

}

int delet(List *L)
{
    char na[10];
    List p,s;
    s=*L;
    p=s->next;

    printf("\n请输入您要删除的姓名:\n");
    scanf("%s",na);
    while(strcmp(p->name,na) !=0 && p != NULL)
    {
        s=s->next;
        p=p->next;
    }
    if (p != NULL)
    {      
        s->next=p->next;
        printf("恭喜您将%s拉黑成功\n\n",na);
    }
    if (p == NULL)
    {
        printf("\n很抱歉,您拨打的用户已停机,请稍后再拨\n\n");
    }

return 0;

}

int find(List *L)
{


    char a[20];
    int flag=0;

    List p;

    while(!flag)
    {
        p=(*L)->next;
        if (p == NULL)
        {
            printf("\n查了个寂寞\n\n");
            break;
        }

                printf("\n请输入拨打联系人的姓名\n");
                scanf("%s",a);
                while (strcmp(p->name,a) != 0 && p->next != NULL)
                {
                    p=p->next;
                }
                if (strcmp(p->name,a) != 0 && p->next == NULL)
                {
                    printf("\n很抱歉,您查询的用户已消失!!!\n\n");
                    break;
                }
                out(&p);
                break;


    }
    return 0;
}

int Exit(List *L)
{   
    
        return 0;
    
}


int out(List *L)
{
    printf("\n姓名:%s\n号码:%d\n",(*L)->name,(*L)->num);
	return 0;
}
int main()
{
    int i;
    int flag;
    int n=0;
    List l;
    l = (List)malloc(sizeof(people));
    l->next=NULL;

    while (flag)
    {
		printf("*************************欢迎使用电子通讯录*************************\n");
		printf("-------------------------------------\n");
		printf("请输入项目前编号执行相关操作:\n");
        printf("新建联系人按【1】\n删除联系人按【2】\n查询联系人按【3】\n退出按------【0】\n");
        scanf("%d",&i);
    printf("\n");
        switch(i)
        {
            case 1:add(&l);
                break;
            case 2:delet(&l);
                break;
            case 3:find(&l);
                break;
            case 0: flag=Exit(&l);
                break;
			case 4:printf("OneforMe\n");
				break;
            default:
                printf("\n????????????????????????????????????????????\n\n");
                break;
        }
    }
    return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单链表可以用来实现员工通讯录管理系统。下面是一种可能的实现方式: 定义员工结构体: ``` typedef struct Employee { int id; // 员工编号 char name[20]; // 员工姓名 char phone[20]; // 员工电话 struct Employee *next; // 指向下一个员工的指针 } Employee; ``` 定义链表结构体: ``` typedef struct EmployeeList { Employee *head; // 链表头指针 int length; // 链表长度 } EmployeeList; ``` 初始化链表: ``` EmployeeList initEmployeeList() { EmployeeList list; list.head = NULL; list.length = 0; return list; } ``` 添加员工: ``` void addEmployee(EmployeeList *list, Employee employee) { Employee *newEmployee = (Employee*)malloc(sizeof(Employee)); *newEmployee = employee; newEmployee->next = NULL; if(list->head == NULL) { list->head = newEmployee; } else { Employee *p = list->head; while(p->next != NULL) { p = p->next; } p->next = newEmployee; } list->length++; } ``` 删除员工: ``` void deleteEmployee(EmployeeList *list, int id) { if(list->head == NULL) { return; } if(list->head->id == id) { Employee *temp = list->head; list->head = list->head->next; free(temp); list->length--; return; } Employee *p = list->head; while(p->next != NULL) { if(p->next->id == id) { Employee *temp = p->next; p->next = p->next->next; free(temp); list->length--; return; } p = p->next; } } ``` 查找员工: ``` Employee* findEmployee(EmployeeList *list, int id) { Employee *p = list->head; while(p != NULL) { if(p->id == id) { return p; } p = p->next; } return NULL; } ``` 遍历链表: ``` void traverse(EmployeeList *list) { Employee *p = list->head; while(p != NULL) { printf("id:%d name:%s phone:%s\n", p->id, p->name, p->phone); p = p->next; } } ``` 这样,我们就可以使用单链表来实现员工通讯录管理系统了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值