数据结构——实验一:用链表实现通讯录(C语言实现)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Nicht_sehen/article/details/82989783
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define size 100
//建立通讯录结构
/*姓名;电话号码 */
typedef struct address_list {
	char name[size];
	char num[size];
	struct address_list *next;
}node;

// 初始化
node *init(node *head)
{
	head = (node *)malloc(sizeof(node));
	head->next = NULL;
	return head;
}


//输出
void print(node *head)
{
	node *p;
	p = head;
	printf("-----------------通讯录-------------------\n");
	printf("                                          \n");
	printf("-姓名--------电话号码---------------------\n");
	while (p)
	{
		printf("%-8s    %-10s\n", p->name, p->num);
		p = p->next;
	}
	printf("----------------end-----------------------\n");
}


//创建(尾插) 
node *create(node *head)
{
	node *q, *p;
	head = init(head);
	int n; 
	char num[100];
	char name[100];
	printf("请输入录入学生人数:");
	scanf("%d", &n);
	getchar();
	for (int i = 1; i <= n; i++)
	{
		p = (node *)malloc(sizeof(node));
		printf("请输入学生姓名:");
		gets(name);
		printf("请输入学生电话号码:");
		gets(num);
		strcpy(p->name, name);
		strcpy(p->num, num);
		if (i == 1)
		{
			head = p;
			p->next = NULL;
			q = head;
		}
		else
		{
			q->next = p;
			q = p;
		}
	}
	q->next = NULL;
	return head;
}

//总节点数 
int count(node *head)
{
	int i=0;
	node *p;
	p=head;
	while(p)
	{
		i++;
		p=p->next;
	}
	return i;
	
 } 
 
//查找姓名为xxx的学生 
node *search(node *head,char x[size])
{
	int j,n;
	node *p;
	p=head;
	n=count(head);
	for(j=0;j<n;j++)
	{
		if(strcmp(p->name,x)==0)
			break;
		p=p->next;
	}
	return p;
}
//查找第i个节点 
node *search_i(node *head,int i)
{
	int j=1;
	node *p;
	p=head;
	while(j!=i)
	{
		p=p->next;
		j++;
	}
	return p;
}

//插入
//后插(在第i个节点后插入姓名为xxx学生信息)
node *insert(node *head, char x[size], char y[size],int j)
{
		node *p,*q;
		int i;
		i=j-1;
		p=(node*)malloc(sizeof(node));
		strcpy(p->name,x);
		strcpy(p->num,y);
		if(i==0)
		{
			p->next=head;
			head=p;
		}
		else
		{
			q=search_i(head,i);
			p->next=q->next;
			q->next=p;
		}
		return head;
}  

//删除名字为xxx的学生信息 
node *del(node *head, char x[size])
{
	node *q,*p;
	p=head;
	int i=0;
	while(p&&strcmp(p->name,x)!=0)
	{
		q=p;
		p=p->next;
		i++;
	}
	if(p)
	{
		if(!q) head=head->next;
		else
		{
			q->next=p->next;
			free(p);
		}
	}	
	return head;
}


//主函数 
int main()
{
	int a=10;
	int b;
    node *head,*p;
	while(a==10)
	{
		printf("你可以进行以下操作:\n"); 
		printf("1:录入学生通讯录    \n"); 
		printf("2:插入某学生信息    \n"); 
		printf("3:删除某学生信息    \n"); 
		printf("4:指定输出某学生信息\n"); 
		printf("请输入你要进行的操作的序号:");
		scanf("%d",&b);
		if(b==1)
		{
			
			head = init(head);
			head = create(head);
			print(head);
		}
		if(b==2)
		{
			char d[size];
			char f[size];
			int e; 
			printf("请输入你要插入的学生位置:");
			scanf("%d",&e); 
			getchar();
			printf("请输入你要插入的学生姓名:");
			gets(d);
			printf("请输入你要插入的学生电话号码:");
			gets(f);
			head=insert(head,d,f,e);
			print(head);
		}
		if(b==3)
		{
			getchar();
			char g[size];
			printf("请输入你要删除的学生姓名:");
			gets(g);
			head=del(head,g);
			print(head);
		}
		if(b==4)
		{
			getchar();
			char c[size]; 
			printf("请输入你要查找的学生姓名:");
			gets(c);
			p=search(head,c);
			printf("姓名:%-8s  电话号码:%10s\n",c,p->num);
		}
		printf("*************************************************************\n");
		printf("是否要退出?(是请输入11,程序终止; 否请输入10,返回上一界面):");
		scanf("%d",&a); 
		printf("\n");
	}
	return 0;
}

运行结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Nicht_sehen/article/details/82989783