C语言:链表作业-教职工系统

编写教职工系统

编写程序,在主函数中调用create函数创建链表,调用findmax函数找到年龄最大的职工,del函数删除一个职工(要删除的职工编号在主函数中输入,并存放在number字符数组中)。
员工结构体的定义如下:
typedef struct wrk_node
{
char num[15];
int age;
struct wrk_node * next;
}WORKER;
//建立链表函数,返回头指针,当职工号为0时结束输入
WORKER * create( )
{

}
//查找年龄最大的职工,并返回指针
WORKER * findmax(WORKER *head)
{
}
//删除职工,number中为删除的职工号,返回头指针
WORKER * del( WORKER *head ,char number[ ])
{

}
程序如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef  struct   wrk_node
{
    
    			
	char  num[15]; 
	int  age;   	
	struct   wrk_node  *next;  
}WORKER;
WORKER * create();
WORKER * findmax(WORKER *head);
WORKER * del( WORKER *head ,char number[ ]); 
void print(WORKER *head);
int main()
{
    
    
	WORKER *head = NULL, *max;
	printf("----------------------------------------------\n");
	printf("Welcome To The Administration Of The Staffs!\n");
	printf("1.New\n");
	printf("2.Age Findmax\n");
	printf("3.Del\n");
	printf("4.Print\n");
	printf("----------------------------------------------\n");
	int choice;
	while(1)
	{
    
    	
		printf("Please choose a number(1-4)\n");
		scanf("%d", &choice);
		switch(choice)
		{
    
    
			case 1:head = create();break;
			case 2:max = findmax(head);
				   printf("年龄最大的职工的工号为%s\n", max -> num);
				   printf("其年龄是%d岁\n", max -> age); 
				   break;
			case 3:char number[15];
			       printf("请输入要删除职工的工号:"); 
				   scanf("%s", number);
				   head = del(head, number);break;
			case 4:print(head);break;
			default:exit(0);
		}
	}
    return 0;
}
//建立链表函数,返回头指针,当职工号为0时结束输入
WORKER *create()      
{
    
    
	WORKER *head, *newN, *tail;
	char num[15];
	newN = (WORKER*)malloc(sizeof(WORKER));
	printf("请输入职工的工号和年龄:"); 
	scanf("%s%d", newN -> num, &newN -> age);
	newN -> next = NULL;
	head = newN;
	tail = newN;
	while(1)
	{
    
    
		printf("请输入职工的工号和年龄:"); 
		scanf("%s", num);
		if(strcmp(num, "0") == 0) break;
		else{
    
    
				newN = (WORKER*)malloc(sizeof(WORKER));
				strcpy(newN -> num, num);
				scanf("%d", &newN -> age);
				newN -> next = NULL;
				tail -> next = newN;
				tail = newN;
		}
	}
	return head;
}
//查找年龄最大的职工,并返回指针
WORKER * findmax(WORKER *head)
{
    
    
	WORKER *p = head, *max = head;
	while(p != NULL)
	{
    
    
		if(p -> age > max -> age) max = p; 
		p = p -> next;
	}
	return max;
}
//删除职工,number中为删除的职工号,返回头指针
WORKER *del(WORKER *head, char number[ ])
{
    
    
	WORKER *p = head, *q;
	while((strcmp(p -> num, number) != 0) && (p != NULL))
	{
    
    
		q = p;
		p = p -> next; 
	}
	if(p != NULL)
	{
    
    
		if(p == head) head = p -> next;
		else q -> next = p -> next;
		free(p);
	}
	return head;
}
//输出 
void print(WORKER *head)
{
    
    
	WORKER *p = head;
	printf("工号\t年龄\n");
	while(p != NULL)
	{
    
    
		printf("%s\t%d\n", p -> num, p ->age);
		p = p -> next;
	}
}

猜你喜欢

转载自blog.csdn.net/m0_51354361/article/details/110249383
今日推荐