链表通讯录文件读取保存

本文介绍了一个简单的链表操作实现,包括链表的初始化、插入、显示、查找、删除及修改等基本功能,并提供了完整的C语言代码示例。

实现信息的存储 

#ifndef _LINKLIST_H
#define _LINKLIST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 1000

#define FAILURE    10000
#define SUCCESS    10001
#define TRUE       10002
#define FALSE      10003

struct node
{
    char name[100];
    int age;
    struct node *next;
};
typedef struct node Node;

#endif

int LinkInit(Node **l)      //初始化
{
    *l=(Node *)malloc(sizeof(Node)*1);
    if(NULL == *l)
    {
        return FAILURE;
    }
    (*l)->next=NULL;
    return SUCCESS;
}

int LinkInsert(Node *l)
{
    Node *p=l;
    char flag = 'y';
    if(NULL == l)
    {
        return FAILURE;
    }
    while(flag == 'y')
    {
        Node *q=(Node *)malloc(sizeof(Node)*1);  //要插入的
        printf("Please input name and age:");
        scanf("%s%d",q->name,&q->age);

        q->next=NULL;
        p->next=q;
        p=q;
        getchar();
        printf("finish?(y/n)");
        scanf("%c",&flag);
    }
    return SUCCESS;
}

int LinkShow(Node *l)
{
    if(NULL == l)
    {
        return FAILURE;
    }
    Node *q = l;
    while(q->next)
    {
        q=q->next;
        printf("name:%s age:%d\n",q->name,q->age);
    }
    getchar();
    getchar();
    return SUCCESS;
}
int sum(Node *l)
{
    int count = 0;
    Node *p;
    p=l->next;
    if(NULL != p)
    {
        p=p->next;
        count++;
    }
    return count;
}


int LinkFind(Node *l)
{
    if(NULL == l )
    {
        return FAILURE;
    }
    char *p;
    p =(char *)malloc(sizeof(char)*32);
    printf("Please input the name you want to find: \n");
    scanf("%s",p);
    Node *q = l;
    while(q != NULL)
    {
        if(strcmp(q->name,p) == 0)
        {
            printf("name:%s age:%d \n",q->name,q->age);
            break;
        }
        else
        {
            q=q->next;
        }
    }
    if(!q)
    {
        return FAILURE;
    }
    getchar();
    getchar();
    return SUCCESS;
}


int LinkDelete(Node *l)
{
    if(NULL == l)
    {
        return FAILURE;
    }
    Node *q = l;
    Node *m = l;
    Node *t;
    int n=0;
    int k=1;
    char *p;
    p = (char*)malloc(sizeof(char)*32);
    printf("Please input the name you want to delete:");
    scanf("%s",p);
    while(l->next != NULL)
    {
        q = q->next;
        n++;
        if(strcmp(q->name,p) == 0)
        {
            t = q;
            break;
        }
    }
    while (k < n && m != NULL)
    {
        m = m->next;
        k++;
    }
    m->next=t->next;
    free(p);
    return SUCCESS;
}


int LinkCorrect(Node *l)
{
    if(NULL == l )
    {
        return FAILURE;
    }

    char *p;
    p =(char *)malloc(sizeof(char)*32);
    printf("Please input the name you want to change:");
    scanf("%s",p);
    Node *q = l;
    while(q != NULL)
    {
        if(strcmp(q->name,p) == 0)
        {
            printf("Please input the name after change:");
            scanf("%s",q->name);
            printf("Please input the age after change:");
            scanf("%d",&q->age);
            break;
        }
        else
        {
            q=q->next;
        }
    }
    if(!q)
    {
        return FAILURE;
    }
    return SUCCESS;
}





void welcome()
{
    printf("\t\t\033[46m*************************\n");
    printf("\t\t*********Welcome!********\n");
    printf("\t\t*************************\n");
}
void menu()
{
    system("clear");
    printf("\n");
    printf("\t\t************************\n");
    printf("\t\t1.添加            2.查看 \n");
    printf("\t\t3.查找            4.删除 \n");
    printf("\t\t5.修改            6.返回 \n");
    printf("\t\t************************\n");
}


void download(Node *s)      //从文件读取
{
    if(NULL == s)
    {
        return ;
    }
    int fd = open("Student.txt",O_RDONLY | O_CREAT);
    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }
    Node *p = s;
    int ret;
    while(1)
    {
        while(p->next == NULL)       //尾插法
        {
            p = p->next;
        }
        Node *q=(Node *)malloc(sizeof(Node)*1);
        ret = read(fd, (void *)q, sizeof(Node)-4);    //去除指针的四个字节
        if(ret ==  0)
        {
            break;
        }
        q->next=NULL;
        p->next=q;
        if(-1 == ret)
        {
            perror("read");
            exit(1);
        }
    }
    close(fd);

}


void save(Node *s)
{
    if(NULL == s)
    {
        return;
    }
    int fd = open("Student.txt",O_WRONLY | O_CREAT);
    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }
    int ret = ftruncate(fd, 0);       //清空文件
    if(ret == -1)
    {
        perror("ftruncate");
        exit(1);
    }
    int ret1 = lseek(fd,0,SEEK_SET);
    if(-1 == ret1)
    {
        perror("lseek");
        exit(1);
    }
    int ret2;
    Node *q=s;

    while(q->next != NULL)
    {
        q=q->next;

        ret2 = write(fd,(void *)q,sizeof(Node)-4);
        if(-1 == ret2)
        {
            perror("write");
            exit(1);
        }
    }
    close(fd);

}

int main()
{
    Node *first = NULL;
    download(first);
    int choice;
    welcome();
    LinkInit(&first);
    while(1)
    {
        menu();
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:LinkInsert(first);
                   break;
            case 2:LinkShow(first);
                   break;
            case 3:LinkFind(first);
                   break;
            case 4:LinkDelete(first);
                   break;
            case 5:LinkCorrect(first);
                   break;
            case 6:save(first);
                   exit(0);
                   break;

        }
    }
    return 0;
}

 

实训项目,满足以下要求 【要求】 (1)程序的主窗口如左图所示。 程序启动时,窗口左边的“姓名”列表框中显示了本软件中已保存的所有人员姓名,从中选择一个姓名,窗口左边的“详细信息”区域显示出此人的详细通讯录内容。 (2)窗口顶部“城市”和“性别”两个列表框中列出了可供选择的城市名称和姓名。使用这两个控件,可以筛选出感兴趣的内容。例如,在“城市”中选择“南京”,在性别中选择“男”,则在“姓名”列表框中只显示出程序所保存的住在南京的男性人员。选择“城市”和“性别”中的“全部”,则列出所有人员。 (3)单击“添加”按钮 ,弹出一个对话框。通过此对话框,可以添加新的通讯录条目。在添加新条目时,所在城市应从组合框中选择,若组合框中没有想要的城市,可以直接在组合框中键入。要求键入的新城市名能自动添加到“添加”对话框中“所在城市”组合框和主窗口中“城市”组合框的下拉列表中。 (4)在主窗口中点击“删除”按钮 可以删除当前显示的条目。 (5)增加“保存”和“打开”按钮或菜单项,要求能够自动保存所做的添加和删除操作。 (6)所有的数据保存在.txt文件中,并能读出。比如.txt文件的每一行保存一条记录。如:姓名|性别|生日…………【可选用CFile/CStdioFile类】 【提示】创建通讯类,包括姓名、性别、生日、所在城市、工作单位、办公电话、住宅电话等必要信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值