初识结构类型小练习

#include <iostream>
#include <cstring>
using namespace std;
struct Node
{
	int id;
	char name[20];
	double score;
	Node *next;
};
Node *creatClass();					//创建链表:提示用户输入多位学生信息,根据输入按顺序创建节点并插入链表中,返回链表头
Node *creatStu();
Node *listSearch(Node *head, char name[]);		//查找:head为链表头指针,name为待查找学生姓名
							//若找到,则返回指向该学生结点的指针,并在main函数中打印学生信息,否则返回空指针
void addNode(Node *&head, Node *pStu);			//添加:head为链表头指针,pStu为需要插入的结点的指针
void Removenode(Node *&head, char name[]);		//删除一位新同学:head为表头,name为需要删除的学生的名字
void output(Node *head);
void sort(Node *&head);					//调整链表:对所有同学按照成绩从小到大排序
void DeleteList(Node *head);				//删除链表:用delete删除链表中的所有结点。

int main()
{
	
	cout << "请输入学生信息(以输入学号-1结束):" << endl;
	Node *head = creatClass();
	output(head);

	cout << "请输入查找的学生姓名:";
	char name[20];
	cin >> name;
	Node *search = listSearch(head, name);
	if (search)
		cout << search->id << '\t' << search->name << '\t' << search->score << endl;
	else
		cout << "该学生不存在!" << endl;

	cout << "请输入转入的学生信息:" << endl;
	Node *pStu = creatStu();
	addNode(head, pStu);
	output(head);

	cout << "请输入转出的学生姓名:";
	cin >> name;

	if (!(listSearch(head, name)))
		cout << "该学生不存在!" << endl;
	else
		Removenode(head, name);

	sort(head);
	output(head);

	DeleteList(head);
	return 0;
}

Node *creatClass()
{
	Node *head = NULL;
	int stu_id;
	char stu_name[20];
	double stu_score;
	cout << "学号:";
	cin >> stu_id;
	if (stu_id == -1)
		return head;
	cout << "姓名:";
	cin >> stu_name;
	cout << "成绩:";
	cin >> stu_score;
	while (stu_id != -1)
	{
		Node *p = new Node;
		p->id = stu_id;
		strcpy_s(p->name, stu_name);
		p->score = stu_score;
		p->next = head;
		head = p;
		cout << "学号:";
		cin >> stu_id;
		if (stu_id == -1)
			return head;
		cout << "姓名:";
		cin >> stu_name;
		cout << "成绩:";
		cin >> stu_score;
	}
	return head;
}

Node *creatStu()
{
	Node *head = NULL;
	char cstu_name[20];
	Node *p = new Node;
	cout << "学号:";
	cin >> p->id;
	cout << "姓名:";
	cin >> cstu_name;
	cout << "成绩:";
	cin >> p->score;
	strcpy_s(p->name, cstu_name);
	p->next = head;
	head = p;
	return head;
}

Node *listSearch(Node *head, char name[])	//若找到,则返回指向该学生结点的指针,否则返回空指针
{
	Node *p = head;
	while (p != NULL)
	{
		if (strcmp(p->name, name) == 0)
			break;
		p = p->next;
	}
	return p;
}

void addNode(Node *&head, Node *pStu)
{
	pStu->next = head;
	head = pStu;
}

void Removenode(Node *&head, char name[])
{
	Node *p = head;
	int i = 1;
	while (p != NULL)
	{
		if (strcmp(p->name, name) == 0)
			break;
		p = p->next;
		i++;
	}
	//第i个结点匹配
	if (p == NULL)
		cout << "该学生不存在!";
	else if (p == head)
	{
		head = head->next;
		delete p;
	}
	else	//循环查找第index-1个结点
	{
		p = head;
		int j = 1;
		while (j < i - 1)
		{
			p = p->next;
			j++;
		}
		Node *q = p->next;
		p->next = q->next;
		delete q;
	}
}

void output(Node *head)
{
	Node *p = head;
	while (p != NULL)
	{
		cout << p->id << '\t' << p->name << '\t' << p->score << endl;
		p = p->next;
	}
}

void sort(Node *&head)
{
	if (head == NULL || head->next == NULL)
		return;
	Node *B = head->next;
	head->next = NULL;
	while (B != NULL)
	{
		Node *current = B;
		B = B->next;
		Node *preA = NULL, *A = head;
		while (A && current->score > A->score)
		{
			preA = A;
			A = A->next;
		}
		if (preA)
		{
			preA->next = current;
			current->next = A;
		}
		else
		{
			current->next = head;
			head = current;
		}
	}
}

void DeleteList(Node *head)
{
	while (head != NULL)
	{
		Node *p = head;
		head = head->next;
		delete p;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值