链表实现一个签到功能(小白)

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

#define MAXSIZE 5

// 用链表实现一个签到功能
typedef struct _student{
	char sno[20];
	char name[20];
	int isAttendance;
}Student;

typedef struct _node{
	Student s;
	struct _node* next;
}Node;


// 创建一个空链表
Node* createList(){
	Node* head = (Node*)malloc(sizeof(Node));
	if(NULL == head){
		exit(-1);	
	}
	head->next = NULL;
	return head;
}


// 插入操作(头插法)
void insertList(Node* head,Student sData){
	Node* cur = (Node*)malloc(sizeof(Node));
	if(NULL==cur){
		exit(-1);
	}
	cur->s = sData;
	cur->next = head->next;
	head->next = cur;	
}


// 循环遍历
void travelList(Node* head){
	head = head->next;
	printf("学号\t 姓名\t\t出勤\n");
	while(head){
		printf("%s\t%s\t\t%d\n",head->s.sno,head->s.name,head->s.isAttendance);
		head = head->next;
	}
}


// 查
Node* searchData(Node* head,Student* s){
	Student findData;
	head = head->next;
	int flag = 0;
	printf("input stuno pls:\n");
	//scanf("%d",&flag);
	if(flag==0){
		scanf("%s",findData.sno);
		strcpy(s->sno,findData.sno);
		while(head){
			if(strcmp(head->s.sno,findData.sno)==0){
				return head;
			}
			head = head->next;
		}
	}else if(flag==1){
		scanf("%s",findData.name);
		while(head){
			if(strcmp(head->s.name,findData.name)==0){
				return head;
			}
			head = head->next;
		}
	}else{
		printf("输入有误\n");
		exit(-1);
	}
	return head;
}


// 求链表长度
int getLength(Node* head){
	int n = 0;
	while(head){
		n++;
		head = head->next;
	}
	return n;
}


// 按照学号进行排序
void sortList(Node* phead){
	Student temp;
	int len = getLength(phead);
	Node *pre = phead; // 指向链表头节点
	Node *after;
	for(int i=0;i<len-1;i++){
		pre = pre->next;//从第一个数据节点开始
		after = pre;
		for(int j=i+1;j<len-1;j++){
			after = after->next;
			if(strcmp(pre->s.sno,after->s.sno)>0){
				temp = pre->s;
				pre->s = after->s;
				after->s = temp;
			}
		}
	}
	
}



// 删除操作
void deleteNodeOfList(Node* phead,Node* pfind){
	if(pfind!=NULL){
		while(strcmp(phead->next->s.sno,pfind->s.sno)!=0){
			phead = phead->next;
		}
		phead->next = pfind->next;
		free(pfind);
		pfind = NULL; 
	}else{
		printf("查无此人!\n");
	}
	
}


//签到功能
void upDate2Attendance(Node* head,Student* s){
	Node* pfind = searchData(head,s);
	head = head->next;
	if(pfind!=NULL){
		while(strcmp(head->s.sno,pfind->s.sno)!=0){
			head = head->next;
		}
		head->s.isAttendance = 1;
	}else{
		printf("输入错误,请重新输入:\n");
//		strcpy(s->sno,"999");
		printf("%s\n",s->sno);
	}
		



	
}


// 展示缺席数据
void displayAbsence(Node* head){
	head = head->next;
	int count=0;
	printf("本次缺勤学生有:\n");
	printf("学号\t 姓名\t\t\n");
	while(head){
		if(head->s.isAttendance==0){
			count++;
			printf("%s\t%s\t\t\n",head->s.sno,head->s.name);
		}
		head = head->next;
	}
	printf("本次考勤共有%d位同学缺勤\n",count);
		
}

int main(int argc,char* argv[]){
	// 创建一个空链表
	Node* head = createList();
	
	// 插入一个数据
	printf("请按顺序依次输入学号、姓名:\n");
	
	//Student sData;
	Student l[5] = {
		{"001","小五",0},
		{"002","张三",0},
		{"003","李四",0},
		{"004","赵六",0},
		{"005","小七",0},

	};
	for(int i=0;i<MAXSIZE;i++){
		//scanf("%s %s",&sData.sno,&sData.name);
		//sData.isAttendance=0;
		insertList(head,l[i]);
	}
	travelList(head);
	
	// 按照学号进行排序
	sortList(head);
	travelList(head);
	
	// 查找
	Student s; // 记录在签到时是否结束循环
	printf("请输入需要删除的学号信息:\n");
	Node* pfind = searchData(head,&s);
	/*printf("查找到如下内容:\n");
	printf("学号\t 姓名\t\t出勤\n");
	printf("%s\t%s\t\t%d\n",pfind->s.sno,pfind->s.name,pfind->s.isAttendance);
      */
	
	
	// 删
	deleteNodeOfList(head,pfind);
	travelList(head);
	
	// 输入学号进行更新签到操作
	printf("输入“999”结束签到\n");
	do{
		upDate2Attendance(head,&s);
	}while(strcmp(s.sno,"999")!=0);
	
	printf("本次考勤信息:\n");
	travelList(head);
	
	// 展示缺勤学生
	displayAbsence(head);
	
	
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值