大题链表1

#include
#include
#include

struct student{
	int ID;
	int grades;
};

struct Node{
	struct student data;
	struct Node *next;
};

/**1.创建Node并将Node添加链表中
 * @brief 
 * 
 * @param S_GRADES 
 * @param S_ID 
 * @param head 
 * 
 * @return 
 */
struct Node* createNode(struct Node** head,int S_ID,int S_GRADES){
	//0.首先创建一个节点并赋值//TODO:好像这里只能创建指针节点,不能struct student stu这种
	struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
	struct student* stu=(struct student*)malloc(sizeof(struct student));
	stu->ID=S_ID;
	stu->grades=S_GRADES;
	newNode->data=*stu;
	newNode->next=NULL;
	
	//1.1得到头节点,判断是否为空:若为空则在头节创建一个节点并添加到头上
	if(*head==NULL){
		*head=newNode;
		return *head;
	}
	//2.否则进行遍历链表
	struct Node* temp=*head;
	while(temp->next!=NULL){
		temp=temp->next;
	}
	temp->next=newNode;
	return *head;
}

/**2.统计链表的节点数量
 * @brief 
 * 
 * @param head 
 * 
 * @return 
 */
int getCount(struct Node* head){
	int count=0;
	//1.头节点为空时
	if(head==NULL) return 0;
	//2.当头节点不为空,不断遍历链表
	struct Node* temp=head;
	while(temp!=NULL){
		count++;
		temp=temp->next;
	}
	return count;
}

/**3.打印所有节点信息,并且将成绩最高的学生输入到指定文件中
 * @brief 
 * 
 * @param fp 
 * @param head 
 */
void printAllNode(struct Node** head,FILE *fp){
	//1.文件的判断
	fp=fopen("file2.txt","w");
	if(fp==NULL){
		printf("文件错误!\n");
		exit(0);
	}
	//2.输出所有节点信息
	struct Node* temp=*head;
	int count=1;
	while(temp!=NULL){
		//这里注意:TO:要得到Node节点中的学生节点信息,需要malloc重新申请一个节点struct student
		struct student *stu=(struct student*)malloc(sizeof(struct student));
		*stu=temp->data;
		printf("%d",temp->data.ID); //结构体对象不能被引用
		
		printf("第%d个节点的信息如下:\n",count);
		printf("学生ID:%d,成绩为%d\n",stu->ID,stu->grades);
		fprintf(fp,"学生ID:%d,成绩为:%d\n",stu->ID,stu->grades);
		temp=temp->next;
		count++;
	}
}

/**对节点中根据学生ID排序
 * @brief 
 * 
 * @param head 
 */
void sort_bubble(struct Node* head){
	//1.首先得到节点数
	int stus_count=getCount(head);
	//2.对特殊情况判断:如果链表为空或者只有一个节点
	if(stus_count==0||stus_count==1) return;
	
	struct Node* f_next=NULL; //3.TO:相对头指针的下一个节点
	struct student temp_stu;
	
	//4.遍历冒泡:这里注意的是我们交换的并不是Node这个壳子,
//	而是壳子里的data,所以直接对比head与next指针的data即可
	while(head->next!=NULL){
		f_next=head->next;
		while(f_next!=NULL){
		if(head->data.ID>f_next->data.ID){
			//5.交换data
			temp_stu=head->data;
			head->data=f_next->data;
			f_next->data=temp_stu;
		}
		f_next=f_next->next;
		}
		head=head->next;
	}
}


void Bubble_Sort(struct Node* head){
	//1.首先first指针指向head节点
	struct Node* first=head;
	int count=0; 
	while(first!=NULL){
		count++; //得到链表的节点数
	}
	//2.进行排序
	struct Node* next=NULL;
	struct student stutemp;
	
	while(head!=NULL){
		next=head->next;
		while(next!=NULL){
			//2.1对next指针的data与head指针data进行比较
			if(head->data.ID>next->data.ID){
				//2.2后置节点大于前序,则进行交换
				stutemp=head->data;
				head->data=next->data;
				next->data=stutemp;
			}
			next=next->next;//2.3next节点后序遍历
		}
		head=head->next; //2.4head节点后序遍历
	}
	
	
	
}

int main(){
	struct Node *head=(struct Node*)malloc(sizeof(struct Node));
	createNode(&head,1,1);
	createNode(&head,2,2);
	createNode(&head,3,3);
	
	FILE *fp;
	fp=fopen("file10.txt","r");
	printAllNode(&head,fp);
	
	
	
}

你可能感兴趣的:(链表,数据结构,算法)