C //习题 9.8 写一个函数insert,用来向一个动态链表插入结点。

C程序设计 (第四版) 谭浩强 习题9.8

习题 9.8 写一个函数insert,用来向一个动态链表插入结点。

IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。

 

代码块:
由于链表是动态链表,不能按照结构体变量为数组的时候通过指针增减调整指针指向,借鉴网上的提示,将原来的单链表增加一个结构体内指针,成为双向链表。
方法:使用指针,自定义类型,结构体,动态分配内存,函数返回类型调整为空。
#include 
#include 

typedef struct Student{
	int num;
	float score;
	Student *prev;
	Student *next;
}Student;

void initialStu(Student **stu){
	int *n = (int*)malloc(sizeof(int));

	//输入要建立的学生人数
	printf("Enter the number of students: ");
	scanf("%d", n);
	while(*n <= 0){
		printf("Number can't less or equal than 0!\nEnter the number of students: ");
		scanf("%d", n);
	}

	printf("\n");

	//创建头结点,头结点的num指定为学生总人数
	Student *head = (Student*)malloc(sizeof(Student));
	head->num = *n;
	head->next = NULL;
	*stu = head;
	
	n = NULL;
	free(n);
}

//判断学生编号是否重复
int isRepeat(Student *stu, int num){
	for(Student *p = stu->next; p != NULL; p = p->next){
		if(p->num == num){
			return 1;
		}
	}
	return 0;
}

void creatStu(Student *stu){
	Student *head = stu;
	Student *p1 = head;
	Student *p2 = head;
	
	printf("Enter %d students info:\n\n", head->num);

	for(int i = 0; i < head->num; i++){
		p1 = (Student*)malloc(sizeof(Student));
		p1->next = NULL;
		p1->prev = p2;

		printf("Enter No.%d student number(100 ~ 999): ", i + 1);
		scanf("%d", &p1->num);
		while(p1->num < 100 || p1->num > 999 || isRepeat(stu, p1->num) == 1){
			if(isRepeat(stu, p1->num) == 1){
				printf("Number Repeat! ");
			}
			else{
				printf("Number Error! ");
			}
			printf("Retry!\nEnter No.%d student number(100 ~ 999): ", i + 1);
			scanf("%d", &p1->num);
		}
		
		printf("Enter No.%d student score(0 ~ 100): ", i + 1);
		scanf("%f", &p1->score);
		while(p1->score < 0 || p1->score > 100){
			printf("Score Error! Retry!\nEnter No.%d student score(0 ~ 100): ", i + 1);
			scanf("%f", &p1->score);
		}
		
		p2->next = p1;
		p1 = p1->next;
		p2 = p2->next;
		
		printf("\n");
	}
}

void printStu(Student *stu){
	printf("\nThe info of %d students:\n", stu->num);
	
	for(Student *p = stu->next; p != NULL; p = p->next){
		printf("Number: %-4d  Score: %-6.2f\n", p->num, p->score);
	}
	
	printf("\n");
}

void insertStu(Student *stu){
	Student *newStu = (Student*)malloc(sizeof(Student));
	
	printf("Enter new student info:\n\n");
	
	printf("Enter new student number(100 ~ 999): ");
	scanf("%d", &newStu->num);
	while(isRepeat(stu, newStu->num) == 1){
		printf("Number Repeat! Retry!\nEnter new student number(100 ~ 999): ");
		scanf("%d", &newStu->num);
	}
	while(newStu->num < 100 || newStu->num > 999 || isRepeat(stu, newStu->num) == 1){
		if(isRepeat(stu, newStu->num) == 1){
			printf("Number Repeat! ");
		}
		else{
			printf("Number Error! ");
		}
		printf("Retry!\nEnter new student number(100 ~ 999): ");
		scanf("%d", &newStu->num);
	}
	
	printf("Enter new student score(0 ~ 100): ");
	scanf("%f", &newStu->score);
	while(newStu->score < 0 || newStu->score > 100){
		printf("Score Error! Retry!\nEnter new student score(0 ~ 100): ");
		scanf("%f", &newStu->score);
	}

	printf("\n");
	
	//采用头结点后插入新结点
	Student *head = stu;
	newStu->next = head->next;
	head->next->prev = newStu;
	head->next = newStu;
	newStu->prev = head;
}

int main(){
	Student *stu = NULL;

	initialStu(&stu);

	creatStu(stu);
	printStu(stu);
	
	insertStu(stu);
	printStu(stu);
	
	free(stu);
	
	system("pause");
	return 0;
}

你可能感兴趣的:(#,C程序设计,(第四版),谭浩强,课后习题,C/C++,c语言,链表,算法)