数据结构实验报告第一个

#include<iostream>
using namespace std;
typedef struct student
{
	char no[10];
	char name[20];
	int math;
	int eng;
	struct student *next;
}student;

void init_list(struct student *  sHead)
{
	sHead->next=NULL;
}
void insert_st1(struct student *  sHead)
{
	struct student *  s1 = new student;
	strcpy(s1->no,"2013001");
	strcpy(s1->name,"jim");
	s1->math=87;
	s1->eng=99;
	s1->next=NULL;
	sHead->next=s1;
}
void insertFour_list(struct student *  sHead)
{
	struct student *  sTail = sHead->next;

	struct student *  s2 = new student;
	strcpy(s2->no,"2013002");
	strcpy(s2->name,"jimtwo");
	s2->math=83;
	s2->eng=91;
	s2->next=NULL;
	sTail->next=s2;
	sTail = s2;

	struct student *  s3 = new student;
	strcpy(s3->no,"2013003");
	strcpy(s3->name,"jimthree");
	s3->math=81;
	s3->eng=47;
	s3->next=NULL;
	sTail->next=s3;
	sTail = s3;

	struct student *  s4 = new student;
	strcpy(s4->no,"2013004");
	strcpy(s4->name,"jimfour");
	s4->math=97;
	s4->eng=89;
	s4->next=NULL;
	sTail->next=s4;
	sTail = s4;

	struct student *  s5 = new student;
	strcpy(s5->no,"2013005");
	strcpy(s5->name,"jimfive");
	s5->math=81;
	s5->eng=95;
	s5->next=NULL;
	sTail->next=s5;
	sTail = s5;
}

void show_list(struct student *  sHead)
{
	if(sHead!=NULL)
	{	
		sHead=sHead->next;
		while(sHead!=NULL)
		{
			cout<<"学号为:"<<sHead->no<<endl;
			cout<<"姓名为:"<<sHead->name<<endl;
			cout<<"数学成绩为:"<<sHead->math<<endl;
			cout<<"英语成绩为:"<<sHead->eng<<endl;
			sHead=sHead->next;
		}
	}
}
void delOne_list(struct student *  sHead)
{
	struct student *  tem1;
	struct student * tem2;
	int pos=3;
	int i=1;
	while(i<pos)
	{
		sHead = sHead->next;
		i++;
	}
	tem1 = sHead->next;
	tem2 = tem1->next;
	sHead->next = tem2;
	delete tem1;

	
	
}
void findByName_list(struct student *  sHead,char  findname[20])
{
	while(strcmp(sHead->name,findname) && sHead->next!=NULL)
	{
		sHead = sHead->next;
	}
	if(sHead!=NULL && !strcmp(sHead->name,findname))
	{

			cout<<"查找成幼功,你查找的学生的信息为:"<<endl;
			cout<<"-----------------------------------"<<endl;
			cout<<"学号为:"<<sHead->no<<endl;
			cout<<"姓名为:"<<sHead->name<<endl;
			cout<<"数学成绩为:"<<sHead->math<<endl;
			cout<<"英语成绩为:"<<sHead->eng<<endl;
	}
	else 
		cout<<"查找未成功"<<endl;


		
	
}
void main()
{
	struct student *  sHead = new student ;
	init_list(sHead);
	insert_st1(sHead);
	cout<<"-----------------------------------"<<endl;
	cout<<"插入一个学生的信息后,链表为:"<<endl;
	show_list(sHead);
	insertFour_list(sHead);
	cout<<"-----------------------------------"<<endl;
	cout<<"再插入四个学生的信息后,链表为:"<<endl;
	show_list(sHead);
	delOne_list(sHead);
	cout<<"-----------------------------------"<<endl;
	cout<<"删除第三个学生的信息后,链表为:"<<endl;
	show_list(sHead);
	cout<<"-----------------------------------"<<endl;
	cout<<"请输入你想要查找的学生的名字:"<<endl;
	char findname[20];
	cin>>findname;
	findByName_list(sHead,findname);

}

个人觉得这个弄得不好。

全是按照老师的那个实验报告的本本上写出来的,有写看不懂那个东西。

是根据实验报告上的这一坨写出来的。

这一坨是:

typedef struct student
{
	char no[10];
	char name[20];
	int math;
	int eng;
	struct student *next;
}student;


你可能感兴趣的:(链表,线性表的相关操作一)