单链表通讯录以及编写过程的错误总结

所有代码

Contact.h

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#define NAME 20
#define SEX 10
#define ADRESS 50
#define TELE 50
enum
{
	Exit,
	Add,
	Delete,
	Lookup,
	Modify,
	Show,
	Preserve
};
struct peo
{
	char name[NAME];
	char sex[SEX];
	int age;
	char tele[TELE];
	char adress[ADRESS];
};
typedef struct date//每个结点
{
	int num;
	struct peo po;
	struct date* next;
}sta;
sta* head,*end;//全局变量,方便使用
void Conadd(struct date* head);//增加信息
void init_linklist(void);//初始化链表
sta* Conlookup(void);//查找信息
void Condelete(void);//删除信息
void Conmodify(void);//修改信息
void Conshow(void);//打印通讯录

Contact.c

#include "contact_file.h"
#include <stdio.h>
static void inputdate(sta* infor);
static int ask(void);
void Conadd(struct date* date)
{
	
	sta* node;
	do
	{
		node = (sta*)malloc(sizeof(sta));
		printf("PLease input the information\n");
		inputdate(node);
		node->num = end->num + 1;
		end->next = node;
		end = node;
		
		
	} while (ask());
	end->next = NULL;  
}

void inputdate(sta*infor)
{
	printf("Name:>>");
	scanf("%s", (infor->po.name));
	printf("Sex:>>");
	scanf("%s", (infor->po.sex));
	printf("Age:>>");
	scanf("%d", &(infor->po.age));
	printf("Tele:>>");
	scanf("%s", (infor->po.tele));
	printf("Adress:>>");
	scanf("%s", (infor->po.adress));
}
static int ask(void)
{
	int n;
	printf("IF you want to operate again<1\\0>:");
	scanf("%d", &n);
	return n;
}
sta* Conlookup(void)
{
	char name[NAME];
	printf("Please input the name that you want:>");
	scanf("%s", name);
	sta* p = head;
	p = p->next;
	while ((p != NULL))
	{
		if (strcmp(p->po.name, name))//查找名字是否匹配
		{
			printf("*****************************************\n");
			printf("**%d      %s      %d      %s        %s                %s*****\n", p->num, p->po.name, p->po.age, p->po.sex, p->po.tele, p->po.adress);
			break;
		}
		else
		{
			p = p->next;
			continue;
		}
	}
	if (p == NULL)
	{
		printf("Sorry,don't find it.\n");
	}
	return p;
}
void Condelete(void)
{
	sta*pt=Conlookup(head);
	if (pt == NULL)
	{
		printf("Deletion failed\n");
	}
	else
	{
		sta* st = pt;
		pt = pt->next;
		st->next = NULL;
		free(st);
		while (pt != NULL)
		{
			(pt->num)--;
			pt = pt->next;
		}
		if (pt == NULL)
			printf("Deletion success\n");
	}

}
void Conmodify(void)
{
	sta* pt = Conlookup(head);
	if (pt == NULL)
		printf("Modification failed\n");
	else
	{
		printf("Please modify the information\n");
		inputdate(pt);
		printf("Modification success\n");
	}
}
void Conshow(void)//打印出来会比较丑,自己去调
{
	sta* p = (head);
	if ((head)->next == NULL)
	{
		printf("Sorry,don't have any information");
		exit(1);
	}
	p = p->next;
	printf("**Serial******Name******Age******Sex********Tele****************Adress**********************\n");
	while (p != NULL)
	{
		printf("*****************************************\n");
		printf("**%d      %s      %d      %s        %s                %s*****\n",p->num, p->po.name,p->po.age,p->po.sex,p->po.tele,p->po.adress);
		p = p->next;
	}
}

Contact_main.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contact_file.h"
void menu(void)
{
	printf("*******************************************\n");
	printf("*******************************************\n");
	printf("****1,Add                     2,Delete*****\n");
	printf("****3,Lookup                  4,Modify*****\n");
	printf("****5,Show                    6,Preserve***\n");
	printf("****		0,Exit        *************\n");
	printf("*******************************************\n");
	printf("*******************************************\n");
	printf("*******************************************\n");
}
void option(int input)
{
	switch (input)
	{
		case Add:
			Conadd(list);
			break;
		case Delete:
			Condelete(list);
			break;
		case Lookup:
			Conlookup(list);
			break;
		case Modify:
			Conmodify(list);
			break;
		case Show:
			Conshow(list);
			break;
		case Preserve:
			//UUConpreserve(list);
			break;
		case Exit:
			printf("Program exit\n");
			break;
		default:
			printf("Program error\n");
			printf("Please input again\n");
			break;
	}
}
void init_linklist(void)
{
	(head) = (sta*)malloc(sizeof(sta));
	if (head == NULL)
	{
		printf("malloc defect\n");
	}
	else
	{
		(head)->num = 0;
		(head)->next = NULL;
		(end) = (head);
		end->num = head->num;
	}
}
int main(void)
{
	int input;
	init_linklist();//在这里处理初始化链表
	do
	{
		menu();
		printf("Please input your choice:>>");
		scanf("%d", &input);
		option(input);
	} while (input);
	return 0;
}

思路解读

前言

刚开始时,不知道你们写什么类型的通讯录,反正我是写数组版本的,是固定总人数不变的,后来学了动态开辟空间,就变成动态增长的版本,到现在单链表的版本。我写这个也是为了加强我对链表的理解。

链表改造

通讯录对于链表改造。
刚开始去初始化链表,开辟头节点,并让尾节点位于头部(当是一个空链表时),毕竟是尾插法。
初始化头节点。当然,创立结构体
在这里插入图片描述

结构体构造问题

在这里插入图片描述

struct peo
{
	char name[NAME];
	char sex[SEX];
	int age;
	char tele[TELE];
	char adress[ADRESS];
};
typedef struct date
{
	int num;
	struct peo po;
	struct date* next;
}sta;

其中结构体中不能使用结构体指针

typedef struct date
{
	int num;
	struct peo* po;
	struct date* next;
}sta;

如果这样动态开辟时,必不会为其中储存信息开辟该有的空间,只会开辟一个结构体指针的空间,而该指针却无指向,算是非法访问,不能使用空间。只能使用结构体变量。

对全局变量的引用问题

我在建立了全局变量head与end
但毕竟是结构体指针变量。
我以前是向函数传递了结构体指针变量。
初始化也是

void init_linklist(sta* head,sta* end);

这并非使用了head与end,而是在函数创建了一个形参head与end,并不会对全局变量产生影响,且也无法影响head。之后,我就改成void,因为全局变量可以作用于全程序。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值