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

来源:互联网 发布:android 解析json数据 编辑:程序博客网 时间:2024/05/23 00:24

*#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 == NULL)    {        p2 -> next = p3;        p3 -> next = NULL;    }    else     {        p3 -> next = p2 -> next;        p2 -> next = p3;    }    }}printf("***************增添信息成功,谢谢!******************\n");return head;

}

struct student *delete(struct student *head)
{
printf(“请输入你想删除的序号:”);

struct student *p1,*p2;int index;scanf("%d",&index);p1 = head;if(head == NULL){    printf("The List is NULL\n");}else{    if(index == 1)    {        head = head -> next;    }    if(index != 1)    {        while(p1 -> next != NULL && p1 -> num != index)        {            p2 = p1;            p1 = p1 -> next;            if(p1 -> num == index)            {                if(p1 == head)                {                    head = p1 ->next;                }                else                {                    p2 -> next = p1 -> next;                }            }        }    } }        printf("***************删除信息成功,谢谢!*****************\n");return head;

}

struct student *change(struct student *head)
{
struct student *p;
int num,index;
char name[20];
char sex[10];
long int phone;

if(head == NULL){    printf("The List is NULL\n");}else{    printf("请输入你想修改的序列号:");    scanf("%d",&index);    printf("请输入该同学的学号:");    scanf("%d",&num);    printf("请输入该同学的姓名:");    scanf("%s",name);    printf("请输入该同学的性别:");    scanf("%s",sex);    printf("请输入该同学的手机号码:");    scanf("%d",&phone);    p = head;    if(index == 1)    {        p -> num = num;        strcpy(p -> name , name);        strcpy(p -> sex , sex);        p -> phone = phone;    }    while(p -> next != NULL && p -> num != index)    {        p = p -> next;    }    if(p -> num == index)    {        p -> num = num;        strcpy(p -> name , name);        strcpy(p -> sex , sex);        p -> phone = phone;    }

}
printf(“*******修改信息成功,谢谢!********\n”);
return head;
}

int main(int argc, char **argv)
{

struct student *p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9,*p10,*p11,*p12,*p13,*p14,*p15,*p16,*p17,*p18,*p19,*p20,*p21,*p22,*p23,*p24;struct student s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20,s21,s22,s23,s24;struct student *head;p1 = &s1 ; p2 = &s2 ; p3 = &s3 ; p4 = &s4 ; p5 = &s5 ; p6 = &s6 ;p7 = &s7 ; p8 = &s8 ; p9 = &s9 ; p10 = &s10 ; p11 = &s11 ; p12 = &s12 ; p13 = &s13 ; p14 = &s14 ; p15 = &s15 ; p16 = &s16 ; p17 = &s17 ;p18 = &s18 ; p19 = &s19 ; p20 = &s20 ; p21 = &s21 ; p22 = &s22 ;p23 = &s23 ; p24 = &s24 ; head = &s1;p1 -> num = 1; strcpy( s1.name , "chenwenjie");strcpy( s1.sex , "male");p1 -> phone = 1234567891;p1 -> next = p2;p2 -> num = 2; strcpy(p2 -> name , "maozequan");strcpy(p2 -> sex , "male");p2 -> phone = 1234567891;p2 -> next = p3;p3 -> num = 3; strcpy(p3 -> name , "yangjifu");strcpy(p3 -> sex , "male");p3 -> phone = 1234567891;p3 -> next = p4;p4 -> num = 4; strcpy(p4 -> name , "zhuzhiwen");strcpy(p4 -> sex , "male");p4 -> phone = 1234567891;p4 -> next = p5;p5 -> num = 5; strcpy(p5 -> name , "chenlijun");strcpy(p5 -> sex , "male");p5 -> phone = 1234567891;p5 -> next = p6;p6 -> num = 6; strcpy(p6 -> name , "caihongfei");strcpy(p6 -> sex , "male");p6 -> phone = 1234567891;p6 -> next = p7;p7 -> num = 7; strcpy(p7 -> name , "wangsheng");strcpy(p7 -> sex , "male");p7 -> phone = 1234567891;p7 -> next = p8;p8 -> num = 8; strcpy(p8 -> name , "wanglina");strcpy(p8 -> sex , "male");p8 -> phone = 1234567891;p8 -> next = p9;p9 -> num = 9; strcpy(p9 -> name , "gelan");strcpy(p9 -> sex , "female");p9 -> phone = 1234567891;p9 -> next = p10;p10 -> num = 10; strcpy(p10 -> name , "yuanxuan");strcpy(p10 -> sex , "female");p10 -> phone = 1234567891;p10 -> next = p11;p11 -> num = 11; strcpy(p11 -> name , "taojunyi");strcpy(p11 -> sex , "male");p11 -> phone = 1234567892;p11 -> next = p12;p12 -> num = 12; strcpy(p12 -> name , "zhanganliu");strcpy(p12 -> sex , "male");p12 -> phone = 1234567892;p12 -> next = p13;p13 -> num = 13; strcpy(p13 -> name , "wangwei");strcpy(p13 -> sex , "male");p13 -> phone = 1234567892;p13 -> next = p14;p14 -> num = 14; strcpy(p14 -> name , "guanhuimin");strcpy(p14 -> sex , "female");p14 -> phone = 1234567892;p14 -> next = p15;p15 -> num = 15; strcpy(p15 -> name , "wangyinxiang");strcpy(p15 -> sex , "male");p15 -> phone = 1234567892;p15 -> next = p16;p16 -> num = 16; strcpy(p16 -> name , "zhaoyuyi");strcpy(p16 -> sex , "male");p16 -> phone = 1234567892;p16 -> next = p17;p17 -> num = 17; strcpy(p17 -> name , "wangmian");strcpy(p17 -> sex , "male");p17 -> phone = 1234567892;p17 -> next = p18;p18 -> num = 18; strcpy(p18 -> name , "rudong");strcpy(p18 -> sex , "male");p18 -> phone = 1234567892;p18 -> next = p19;p19 -> num = 19; strcpy(p19 -> name , "panbairu");strcpy(p19 -> sex , "female");p19 -> phone = 1234567892;p19 -> next = p20;p20 -> num = 20; strcpy(p20 -> name , "zhangmengyuan");strcpy(p20 -> sex , "female");p20 -> phone = 1234567892;p20 -> next = p21;p21 -> num = 21; strcpy(p21 -> name , "chenchunxu");strcpy(p21 -> sex , "male");p21 -> phone = 1234567893;p21 -> next = p22;p22 -> num = 22; strcpy(p22 -> name , "lizhuangwei");strcpy(p22 -> sex , "male");p22 -> phone = 1234567893;p22 -> next = p23;p23 -> num = 23; strcpy(p23 -> name , "wenwanwan");strcpy(p23 -> sex , "female");p23 -> phone = 1234567893;p23 -> next = p24;p24 -> num = 24; strcpy(p24 -> name , "sundan");strcpy(p24 -> sex , "female");p24 -> phone = 1234567893;p24 -> next = NULL;char a;printf("**********欢迎来到154班通讯录*************\n");printf("**********增添信息 :请输入a *************\n");printf("**********查询信息 :请输入b *************\n");printf("**********修改信息 :请输入c *************\n");printf("**********删除信息 :请输入d *************\n");printf("**********停止操作 : 请输入e *************\n");scanf("%c",&a);while(a !='e'){    switch(a)    {        case 'a' : head = add(head)    ; break;        case 'b' : print(head)         ; break;        case 'c' : head = change(head) ; break;        case 'd' : head = delete(head) ; break;        default : ;break;    }    printf("请再次输入你需要的操作:");    scanf("%c",&a);}return 0;

}

原创粉丝点击