利用gcc,双向循环链表写一个通讯录

总目标

在linux环境下写一个抓包工具

小实验1.写一个通信录

这是我们的第一个小实验

  1. 要在linux环境下用gcc写,可以安装双系统
  2. 在写的时候要用到双向循环链表
  3. 实现动态增删改,这个应该算是基本功能
  4. 利用gt完成界面化

代码部分

因为有朋友想要加入文件输入输出功能的
我又很感兴趣
就将这个功能加入了
以下是修改过后的代码


/*如果室友想要借你的代码无法拒绝的时候,可以在这里备注以下,希望老师可以看到*/

/*如果室友想要借你的代码无法拒绝的时候,可以在这里备注以下,希望老师可以看到*/

/*如果室友想要借你的代码无法拒绝的时候,可以在这里备注以下,希望老师可以看到*/

/*如果室友想要借你的代码无法拒绝的时候,可以在这里备注以下,希望老师可以看到*/



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

#define HAVE_SAME_NAME    1
#define NO_SAME_NAME     0

struct address_book {
    int number;
    char name[20];   //姓名
    char phone_number[20]; //电话号码
    struct address_book *next;
    struct address_book *prev;
};

int count = 0;

struct address_book *head = NULL;

/*create head node*/
void init(void)  
{
    printf("%s\n", __func__);
    head = (struct address_book *)malloc(sizeof(struct address_book));//在内存中为结构体分配内存并返回该内存块的首地址在将其强制转换为结构体类型再赋值给头节点
    head->next = head;   
    head->prev = head;
}

void list_all_address_book_information(void)
{
    struct address_book *pos = NULL;
    printf("%s\n", __func__);
    printf("\n\n\n\tname\t\t\tphone_number\t\t\t\n");
    for(pos = head->next; pos != head; pos = pos->next)
    {
        printf("%d\t%s\t\t\t%s\t\t\t\n", pos->number, pos->name, pos->phone_number);
    }
    printf("\n\n\n%s : end\n", __func__);
}


int save()
{
	struct address_book * pos = NULL;
	printf("%s\n", __func__);
	FILE *fpsave = fopen("com.txt","w");
	if(fpsave == NULL)
	{
		return 0;
	}
	for(pos = head->next; pos != head; pos = pos->next)
	{
		fprintf(fpsave,"%s ",pos->name);
		fprintf(fpsave,"%s ",pos->phone_number);

	}
	rewind(fpsave);
	fclose(fpsave);
}
int read()
{
	struct address_book * pos = NULL;
        printf("%s\n", __func__);
	FILE *fpread = fopen("com.txt","r");
	printf("文件读取成功\n");
	if(fpread == NULL)
        {
		printf("文件为空\n");
                return 0;
        }
	printf("文件不为空,继续执行\n");
	int k = 0;	
	while(k < 4){
																													
		struct address_book * new_node = (struct address_book *)malloc(sizeof(struct address_book));
    		struct address_book * last_node = head->prev;
		last_node->next = new_node;
		new_node->prev = last_node;
    
	
		head->prev = new_node;
    
		new_node->next = head;
		k = k+1;
	}
	printf("已创建新节点\n");
	for(pos = head->next; pos != head; pos = pos->next)
        {
//		printf("该程序执行");
                fscanf(fpread,"%s",pos->name);
                fscanf(fpread,"%s",pos->phone_number);
//		printf("pos->name is %s\n",pos->name);
//		printf("pos->phone_number is %s\n",pos->phone_number);

        }
//	printf("该程序未执行/n");
	fclose(fpread);




}

int have_same_name(char *name)
{
    struct address_book *pos = NULL;
    for(pos = head->next; pos != head; pos = pos->next)
    {
        if(strcmp(pos->name, name) == 0){
            return HAVE_SAME_NAME;
        }
    }
    return NO_SAME_NAME;
}

void delete_address_book_fixed_count(void)
{
    struct address_book *pos = NULL; 
    int i = 0;
    count--;
    for(pos = head->next, i = 1; pos != head; pos = pos->next, i++)
    {
        pos->number = i;
    }
    i--;
    if(i != count){
        printf("i = %d\t, count = %d\n", i, count);
        printf("count error\n");
    }
    list_all_address_book_information();

}

void add_address_book_information(void)
{
    char new_name[20] = {0};
    char new_phone_number[20] = {0};
    memset(new_name, 0, sizeof(new_name));
    memset(new_phone_number, 0, sizeof(new_phone_number));

    printf("%s\n", __func__);

    printf("enter the name:\n");
    scanf("%s", new_name);

    if(have_same_name(new_name)){
        printf("hava same name\n");
        goto have_same_name;
    }

    printf("enter the phone number:\n");
    scanf("%s", new_phone_number);

    struct address_book * new_node = (struct address_book *)malloc(sizeof(struct address_book));
    struct address_book * last_node = head->prev;
    last_node->next = new_node;
    new_node->prev = last_node;
    head->prev = new_node;
    new_node->next = head;

    count++;

    new_node->number = count;
    memset(new_node->name, 0, sizeof(new_node->name));
    memset(new_node->phone_number, 0, sizeof(new_phone_number));
    strcpy(new_node->name, new_name);
    strcpy(new_node->phone_number, new_phone_number);

    printf("add address book information ok!\n");

    list_all_address_book_information();

have_same_name:
    return ;
}

void delete_address_book_information()
{
    struct address_book * pos = NULL;
    char name[20];
    printf("%s\n", __func__);

    memset(name, 0, sizeof(name));
    printf("enter the name that you want delete:\n");
    scanf("%s", name);

    for(pos = head->next; pos != head; pos = pos->next)
    {
        if(strcmp(pos->name, name) == 0){
            printf("delete\t%d\t%s\t\t\t%s\t\t\n", pos->number, pos->name, pos->phone_number);
            pos->prev->next = pos->next;
            pos->next->prev = pos->prev;
            free(pos);
            delete_address_book_fixed_count();
            return;
        }
    }

    printf("no the same name, pleace check it again\n");

}

void search_phone_number(void)
{
    struct address_book * pos = NULL;
    char name[20];
    printf("%s\n", __func__);

    memset(name, 0, sizeof(name));

    printf("enter the name that want to search:\n");
    scanf("%s", name);

    for(pos = head->next; pos != head; pos = pos->next)
    {
        if(strcmp(pos->name, name) == 0){
            printf("find the name:\n");
            printf("\tname\t\t\tphone_number\t\t\n");
            printf("%d\t%s\t\t\t%s\t\t\t\n", pos->number, pos->name, pos->phone_number);
            return;
        }
    }
    printf("sorry, can not find the name infomation\n");

}

void modify_phone_number(void)
{
    struct address_book * pos = NULL;
    char name [20];
    char phone_number [20];

    printf("%s\n", __func__);

    memset(name, 0, sizeof(name));

    printf("enter the name that want to modify:\n");
    scanf("%s", name);
    printf("enter the phone_number that want to modify:\n");
    scanf("%s", phone_number);

    for(pos = head->next; pos != head; pos = pos->next)
    {
        if(strcmp(pos->name, name) == 0){
            printf("find the name and modify the phone_number:\n");
            strcpy(pos->phone_number,phone_number);
            printf("\tname\t\t\tphone_number\t\t\t\n");
            printf("\t%s\t\t\t%s\t\t\t\n", pos->name, pos->phone_number);
            return;
        }
    }
    printf("sorry, can not find the name information\n");
}

void free_all_memory()
{
    struct address_book * pos = head->next;
    struct address_book * pos_next = NULL;
    printf("%s\n", __func__);
    for(; pos != head; )
    {
        //pos_next = pos->next;
        head->next = pos->next;
        pos->next->prev = head;
        printf("\tdelete node->number = %d\n", pos->number);
        free(pos);
        pos = head->next;
    }
    printf("\tdelete head node\n");
    free(head);
}

int main(void)
{
    int user_select = 0;

    init();

    printf("我的通信录");
    while(1){
        printf("function select\n");
        printf("\t1. add an address book information\n\t2. delete an address book information\n\t3. search phone number\n\t4. modify phone number\n\t5. list all address book information\n\t6. save the data\n\t7. read the data\n\t0. quit the application\n");
        scanf("%d", &user_select);
        switch(user_select){
            case 0:{
                free_all_memory();
                printf("the application will quit\n");
                return 0;
            }           
            case 1:{
                add_address_book_information();
                break;
            }
            case 2:{
                delete_address_book_information();
                break;
            }       
            case 3:{
                search_phone_number();
                break;
            }
            case 4:{
                modify_phone_number();
                break;
            }
            case 5:{
                list_all_address_book_information();
                break;
            }
	    case 6:{
			   save();
			   break;
		   }
	    case 7:{
			   read();
			   break;
		   }
            default:{
                printf("enter error\n");
                free_all_memory();
                break;
            }
        }
    }
    return 0;
}

在这里我要感谢这篇博客的作者,我借鉴了其中的代码部分
https://blog.csdn.net/xiaocui92/article/details/79182211

我遇到的问题

1.Linux系统出了问题

minimal BASH like line editing is supported
明明双系统已经安装好了,不知为何还是会出现这样的问题。
好在这个问题应该是访存记录没有找到的问题,只要让电脑在windows系统下运行一会就没问题了。
后来我感觉是因为在拼多多上上买了劣质硬盘盒导致的接触不良,回来再更新

2.对struct address_book*next;的理解不足

我在代码中写了一个名为address_book的结构体,其中定义了姓名和电话号的属性。此处就是定义了一个指针,而且是属于这个结构体的一个指针。

3.对printf("%s\n", func);的理解不足

__func__在此处指的是该条语句所在的函数的函数名

4.对数组类型赋值理解不够

在结构体中,我将姓名和电话号码都定义为了数组类型,而在修改电话号码所用的modify函数中,需要进行数组的赋值。此时是不能用pos->phone_number = phone_number的,这是因为数组是不能这样赋值的,而是应该用strcpy函数,strcpy(pos->phone_number = phone_number)

5.对memset函数理解不足

memset(new_id, 0, sizeof(new_id));
该函数的意思是将new_id所对应的地址空间全赋为0

6.其他的一些知识

struct student *next;
我认为它就是定义了一个指针,而且还是属于student这个结构体的一个指针,这应该是属于struct用法的一部分。

struct student *head = NULL;
这一句虽然和上面的很类似,可这里它又不是定义指针了,而是定义了头节点,并将其置为空。
head = (struct student *)malloc(sizeof(struct student));
我对这句话也很感兴趣,sizeof就是获取了数据在内存中所占用的存取空间,以字节为单位。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值