#include"iostream"
#include"stdlib.h"
#include"process.h"
#include"time.h"
using namespace std;
typedef struct Stu {
int id;
double score;
Stu *next;
};
struct Teacher {
int id;
string name;
Teacher *next;
};
void createList(Stu *&sl) { //带有头节点的单链表初始化其头节点
sl=(Stu*)malloc(sizeof(Stu));
sl->next=NULL;
cout<<"Success to initiate"<
void addNode(Stu *sl,int idtmp) { //尾插法建立带有头节点的单链表
Stu *node=(Stu*)malloc(sizeof(Stu));
double scoretmp=100.0*(double)rand()/RAND_MAX;//随机生成0-100的随机浮点数
node->next=NULL;
node->id=idtmp;
node->score=scoretmp;
Stu *p=sl;
while(p->next!=NULL) {
p=p->next;
}
p->next=node;
}
void selectWholeList(Stu *sl) {//输出单链表的所有节点数据
Stu *p=sl;
while(p->next!=NULL) {
p=p->next;
cout<<"id:"<
}
void updateListId(Stu *sl,int oldid,int newid) {//更新某节点的id值,使用顺序查找链表方式
Stu *p=sl;
while(p->next!=NULL){
p=p->next;
if(p->id==oldid){
cout<<"Have found it!"<
break;
}
}
}
void deleteNode(Stu *sl,int idtmp){//删除节点
Stu *f=sl;
Stu *b=sl->next;
while(f->next!=NULL){
if(b->id==idtmp){
f->next=b->next;
free(b);
cout<<"Success to delete "<
}
f=f->next;
b=b->next;
}
}
int main() {
srand((int)time(0));//随机数种子
Stu *sl;
createList(sl);
int idtmp;
cout<<"Please key-in the id to each student,with -1 to end the inputting:"<
cout<<"The score will be built later."<
addNode(sl,idtmp);
cout<<"Please key-in the id to each student,with -1 to end the inputting:"<
system("cls");
}
selectWholeList(sl);
int oldid;
int newid;
cout<<"Please key-in the old id and new id to update the list:"<
updateListId(sl,oldid,newid);
selectWholeList(sl);
cout<<"Please key-in the id to delete it:"<
deleteNode(sl,idtmp);
selectWholeList(sl);
return 0;
}