实现基本初始化、插入、删除、遍历操作。
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define MAX 100
#define OK 1
#define ERROR 0
typedef int status;
typedef struct
{
int C;
int Math;
}Score;
typedef struct
{
char name[20];
char ID[20];
Score sc;
}Student;
typedef struct node
{
Student std;
struct node* next;
}Node,LinkedList;
status Initial(LinkedList* & head)
{
head = new LinkedList;
if (!head)
return ERROR;
head->next = NULL;
return OK;
}
status Insert(LinkedList*& head, Student std)
{
Node* p = new Node;
if (!p)
return ERROR;
p->std = std;
p->next = NULL;
Node* temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = p;
return OK;
}
status Delete(LinkedList*& head, char id[20])
{
Node* pre = head;
Node* temp = head -> next;
while (strcmp(temp->std.ID,id) != 0)
{
pre = temp;
temp = temp -> next;
}
if (temp == NULL)
return ERROR;
pre->next = temp->next;
free(temp);
return OK;
}
status Display(LinkedList*& head)
{
cout << "-----------------------遍历如下---------------------" << endl;
Node* temp = head->next;
while (temp != NULL)
{
cout << "姓名:"<<temp->std.name << endl;
cout <<"学号:"<< temp->std.ID << endl;
cout << "各科成绩如下:" << endl;
cout << "C:" << temp->std.sc.C << endl;
cout << "Math:" << temp->std.sc.Math << endl;
cout << "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" << endl;
temp = temp->next;
}
cout << "--------------------遍历结束!!!!!!!!!!-----------------" << endl;
return OK;
}
int main()
{
cout << "--------------------------学生信息管理系统-------------------------" << endl;
cout << "*1:录入学生信息" << endl;
cout << "*2:删除学生信息" << endl;
cout << "*3:遍历当前学生系统" << endl;
cout << "*0:退出系统" << endl;
cout << "-------------------------------------------------------------------" << endl;
int Manp, flag = 1;
LinkedList* head;
Initial(head);
while (flag == 1)
{
cout << "--请--输--入--需--要--进--行--的--操--作:";
cin >> Manp;
switch (Manp)
{
case 1:
cout << "*1:录入学生信息" << endl;
Student std;
cout << "输入学生姓名:";
cin >> std.name;
cout << "输入学生学号:";
cin >> std.ID;
cout << "输入C语言成绩:";
cin >> std.sc.C;
cout << "输入数学成绩:";
cin >> std.sc.Math;
Insert(head, std);
break;
case 2:
cout << "*2:删除学生信息" << endl;
cout << "请输入想删除的学生学号:";
char id[20];
cin >> id;
Delete(head, id);
break;
case 3:
Display(head);
break;
case 0:
flag = 0;
break;
}
}
cout << "**********************************************" << endl;
cout << "******************GOODBYE*********************" << endl;
cout << "**********************************************" << endl;
}
2002



被折叠的 条评论
为什么被折叠?



