最基本的单链表同学录



#include
#include "string.h"
#include
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    int  num;
    char name[20];
    int tel;
    struct student *next;
};

struct student *create()
{
    struct student *p1,*p2,*head;
    int num;
    char name[20];
    int tel;
    int n=0;
    head=NULL;
    p1=p2=(struct student *)malloc(LEN);
    printf("输入学号,姓名和电话");
    scanf("%d %s %d",&p1->num ,&p1->name ,&p1->tel);

    while(p1->num!=0)
    {
        n++;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student*)malloc(LEN);
        printf("继续输入\n");
        scanf("%d %s %d",&p1->num ,&p1->name ,&p1->tel);
    }
    p2->next=NULL;
    return head;
}
 void menu()
{
    printf("通讯录\n");
    printf("1 查找\n");
    printf("2 删除\n");
    printf("3 修改\n");
    printf("4 增加\n");
    printf("5 退出\n");
}

void printlist(struct student *head)
{
    struct student *p;
    p=head;
    if(head!=NULL)
    {
        do
        {
            printf("num:%d\tname:%s\ttel:%d\n",p->num,p->name,p->tel);
            p=p->next;

        }while(p!=NULL);
    }
}

//void search(struct student *head)



void *delNODE(struct student *head)
{
    int num;
    printf("输入你想删除的序号");
    scanf("%d",&num);
    printf("delNODE\n");
    struct student *p1,*p2;
    if(head==NULL)
    {
        printf("the list is null\n");
    }
    else
    {
        p1=head;
    while(p1->next!=NULL&&p1->num!=num)
    {
        p2=p1;
        p1=p1->next;

    }

   
    if(p1->num==num)
    {
        if(p1==head&&p1->next!=NULL )
            head=p1->next;
        else
            p2->next=p1->next;
    }
    else
        printf("can not find list num\n");
    }
   
    return head;
}


void *update(struct student *head)
{
    int num;
    char name[20];
    int tel;
    printf("update\n");
    printf("输入想要改的序号");
    scanf("%d",&num);
    struct student *p;
    if(head==NULL)
    {
        printf("the list is null\n");

    }
    else
        p=head;
        while(p->next!=NULL&&p->num!=num)
        {
            p=p->next;
        }
        if(p->num==num)
        {
          /*  printf("学号");
            scanf("%d",&p->num);
            printf("姓名");
            scanf("%s",&p->name);
            printf("电话");
            scanf("%d",&p->tel);*/
            printf("学号");
            scanf("%d",&p->num);
            printf("姓名");
            scanf("%s",&name);
            strcpy(p->name,name);
            printf("电话");
            scanf("%d",&p->tel);
        }
        else
            printf("can not find list index\n");
    return head;
}



void *add(struct student *head)
{
    int index,num,tel;
    char name[20];
    printf("add\n");
    printf("在几号后加入");
    scanf("%d",&index);
    struct student *p1,*p2,*p3,*p4;
    if(head==NULL)
    {
        printf("the list is null\n");

    }
    
    else
    {
        p1=p2=head;
        while(p1->next!=NULL&&p1->num!=index)
        {
            p1=p2->next;
            p2=p1;
        }
        if(p1->num==index)
        {
            p3=(struct student *)malloc(LEN);
            printf("学号");
            scanf("%d",&p3->num);
            printf("名字");
            scanf("%s",&p3->name);
          //  strcpy(p3->name,name);
            printf("电话");
            scanf("%d",&p3->tel);
            if(p2->next==NULL)
            {
                p2->next=p3;
                p3->next=NULL;
            }
            else
            {
                p3->next=p2->next;
                p2->next=p3;
            }
        }
        else
            printf("can not find list index\n");
    }
    return head;
}

struct student *head_insert(struct student *head)
{
    int num,tel;
    char name[20];
    struct student *p1,*p2;
    if(head=NULL)
    {
        printf("no");
    }
    else
    {
        p2=head;
        p1=(struct student *)malloc(LEN);
        printf("xuehao");
        scanf("%d",&p1->num);
        printf("name");
        scanf("%s",&name);
        strcpy(p1->name,name);
        printf("tel");
        scanf("%d",&p1->tel);
        head=p1;
        p1->next=p2;
    }
    return head;

}
int main()
{
    menu();
    struct student *head;
    head=create();
    while(1)
    {
        printf("选择");
        int b;
        scanf("%d",&b);
        switch(b)
        {
            case 1:
                printlist(head);
                break;
            case 2:
               head= delNODE(head);
                break;
            case 3:
                update(head);
                break;
            case 4:
                add(head);
                break;
            case 5:
                return 0;
        }
    }
    return 0;
}


这是学c语言之后第一次做的项目,开始的时候可以说有点手足无措,感觉无从下手。还好有同学的帮助,让我理清了思路,,但也只能做出这种比较低级的,功能上有所欠缺的程序。前路漫漫......


你可能感兴趣的:(新手)