【课外作业】二维双向链表练习代码

经典的学生管理项目准备来临,为了能方便存储学生信息,利用课余时间尝试写出的一个二维链表,但由于实现函数功能的时候,并没有考虑到如何把链表存储的信息写到文件里,更别提把信息读回到链表中,而且查找功能也并不满意,很多都是想到的时候在改,本来打算改进一下函数的实现的,但是时间实在不够用,只能这样了。

(本来打算后来再补回注释的,但是实在懒,还是算了)


/*
	练习代码:
		建立一个二维双向链表,用来存储班级和学生的数据
		实现对班级和学生数据的增、删、改、查
		并打印出出班级和学生的信息
*/

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

typedef struct _student
{
	struct _student *p_next;
	struct _student *p_before;
	struct _student *p_search;
	int age;
	char *name;
}Student;

typedef struct _class
{
	Student *p_stu_next;
	struct _class *p_next;
	struct _class *p_before;
	struct _class *p_search;
	int size;
	char *name;
}Class;

typedef struct _stack
{
	int size;
	Class *p_next;
}Stack;

//search the class
Class* search_class(Stack *p_stack)
{
	char name[50];
	printf("input class's name :");
	scanf("%s", name);
	Class *p_destination = p_stack->p_next;
	p_destination->p_search = p_stack->p_next;
	while (NULL != p_destination->p_search)
	{
		if (strcmp(name, p_destination->p_search->name) == 0)
		{
			printf("search class success!\n");
			return p_destination->p_search;
		}
		p_destination->p_search = p_destination->p_search->p_next;
	}
	printf("search class failed.\n");
	return NULL;	
}

//search the student
Student* search_student(Stack *p_stack)
{
	Class *p_temp = search_class(p_stack);
	p_temp->p_search = p_temp;
	if (NULL == p_temp)
	{
		return NULL;
	}
	printf("input student's name:");
	char name[50];
	scanf("%s", name);
	while (NULL != p_temp->p_stu_next)
	{
		if (0 == strcmp(name, p_temp->p_search->p_stu_next->name))
		{
			printf("search student success!\n");
			return p_temp->p_stu_next;
		}
		p_temp->p_search->p_stu_next = p_temp->p_search->p_stu_next->p_next;
	}
	printf("search student failed!\n");
	return NULL;
}

//edit student info
void edit_student(Stack *p_stack)
{
	Student *p_stu = search_student(p_stack);
	if (NULL == p_stu)
	{
		printf("this student is not exist!\n");
		return;
	}
	printf("input new massage\nstudent name is:");
	char name[50];
	scanf("%s", name);
	strcpy(p_stu->name, name);
	printf("age is: ");
	scanf("%d", &(p_stu->age));
	printf("edit completed!\n");
}

//delete a student
void delete_student(Stack *p_stack)
{
	Class *p_temp = search_class(p_stack);
	p_temp->p_search = p_temp;
	if (NULL == p_temp)
	{
		printf("delete failed,this class is not exist!\n");
		return;
	}
	printf("104:input student's name:");
	char name[50];
	scanf("%s", name);
	while (NULL != p_temp->p_search->p_stu_next)
	{
		if (0 == strcmp(name, p_temp->p_search->p_stu_next->name))
		{
			printf("111:search success!\n");
			break;
		}
		p_temp->p_search->p_stu_next = p_temp->p_search->p_stu_next->p_next;
	}
	if (NULL == p_temp->p_search->p_stu_next)
	{
		printf("delete failed, this student is not exist!\n");
		return;
	}
	Student *p_stu = p_temp->p_search->p_stu_next;
	if (p_stu->p_before != NULL)
	{
		p_stu->p_before->p_next = p_stu->p_next;
	}
	if (p_stu->p_next != NULL)
	{
		p_stu->p_next->p_before = p_stu->p_before;
	}
	p_temp->size--;
	free(p_stu->name);
	free(p_stu);
	printf("delete student success!\n");
}

//create a student
Student* create_student(Stack *p_stack)
{
	Student *p_stu = malloc(sizeof(Student));
	p_stu->name = malloc(50);
	p_stu->p_next = NULL;
	p_stu->p_before = NULL;
	p_stu->p_search = NULL;
	printf("input student's name: ");
	char name[50];
	scanf("%s", name);
	strcpy(p_stu->name, name);
	printf("input student's age: ");
	scanf("%d", &(p_stu->age));
	return p_stu;
}

//add a student
void add_student(Stack *p_stack, Student *p_stu)
{
	Class *p_class = search_class(p_stack);
	while (NULL == p_class)
	{
		printf("this class is not exist!\nAre your want to continue?(y/n)\n");
		char flag1;
		scanf("%c", &flag1);
		if (flag1 != 'y' || flag1 != 'Y')
		{
			free(p_stu->name);
			free(p_stu);
			p_stu = NULL;
			printf("add student failed!\n");
			return;
		}
		p_class = search_class(p_stack);
	}
	(p_class->size)++;
	if (NULL != p_class->p_stu_next)
	{
		p_class->p_stu_next->p_before = p_stu;
	}
	p_stu->p_next = p_class->p_stu_next;
	p_class->p_stu_next = p_stu;
	printf("end\n");
	printf("add student success!\n");
}

//create a class
Class* create_class()
{
	printf("input class's name: ");
	Class *p_class = malloc(sizeof(Class));
	if (NULL == p_class)
	{
		perror("create class failed!\n");
		return NULL;
	}
	p_class->p_stu_next = NULL;
	p_class->p_before = NULL;
	p_class->p_next = NULL;
	p_class->p_search = NULL;
	p_class->size = 0;
	p_class->name = malloc(50);
	if (p_class->name == NULL)
	{
		perror("create class failed!\n");
		free(p_class);
		return NULL;
	}
	char name[50];
	scanf("%s", name);
	strcpy(p_class->name, name);
	return p_class;
}

//add a class
void add_class(Stack *p_stack, Class *p_class)
{
	if (NULL != p_stack->p_next)
	{
		p_stack->p_next->p_before = p_class;
	}
	p_class->p_next = p_stack->p_next;
	p_stack->p_next = p_class;
}

//delete a class
void delete_class(Stack *p_stack)
{
	Class *p_class = search_class(p_stack);
	if (NULL == p_class)
	{
		printf("this class is not exist!\n");
		return;
	}
	if (NULL != p_class->p_before)
	{
		p_class->p_before->p_next = p_class->p_next;
	}
	if (NULL != p_class->p_next)
	{
		p_class->p_next->p_before = p_class->p_before;
	}
	free(p_class->name);
	free(p_class);
	p_stack->size--;
	p_class = NULL;
	printf("delete class success!\n");
}


//edit a class's info
void edit_class(Stack *p_stack)
{
	Class *p_class = search_class(p_stack);
	printf("input the new info:\nnew name is:");
	char name[50];
	scanf("%s",  name);
	strcpy(p_class->name, name);
}

//create a stack
void create_stack(Stack **pp_stack)
{
	*pp_stack = malloc(sizeof(Stack));
	(*pp_stack)->size = 0;
	(*pp_stack)->p_next = NULL;
}

//print student info
void print_student(Student *p_stu, int show)
{
	if (NULL == p_stu)
	{
		return;
	}
	if (1 == show)
	{
		print_student(p_stu->p_next, 1);
	}
	printf("student's info\nname: %s\nage: %d\n", p_stu->name, p_stu->age);
}

//print class info
void print_class(Class *p_class, int show)
{
	if (NULL == p_class)
	{
		return;
	}
	printf("Class's name is: %s\nsize is %d\n", p_class->name, p_class->size);

	if (p_class->size > 0 && 1 == show)
	{
		Student *p_stu = p_class->p_stu_next;
		print_student(p_stu, show);
	}
}

//print all class info
void print_all_class(Class *p_class, int show)
{
	if (NULL == p_class)
	{
		return;
	}
	print_class(p_class, show);	
	print_all_class(p_class->p_next, show);
}

//print stack info
void print_stack(Stack *p_stack)
{
	print_all_class(p_stack->p_next, 1);
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值