1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include <iostream> using namespace std;
typedef struct Student{ string stuId; string name; double mathScore; double chineseScore; double englishScore; } student;
typedef student ElemType;
typedef struct LinkList{ ElemType elem; struct LinkList *next; } linkList;
ElemType inputStudent(); linkList* initLink(int n); linkList* insertElem(linkList * p,ElemType elem,int add); void display(linkList *p);
int main(){ int linked_length = 3; linkList* linked_stu = initLink(linked_length);
student stu = inputStudent();
linked_stu = insertElem(linked_stu,stu,2);
display(linked_stu);
return 0; }
student inputStudent(){ student stu; cout << "Id" << endl; cin >> stu.stuId; cout << "name" << endl; cin >> stu.name; cout << "mathScore" << endl; cin >> stu.mathScore; cout << "chineseScore" << endl; cin >> stu.chineseScore; cout << "englishScore" << endl; cin >> stu.englishScore; return stu; }
linkList* initLink(int n){ linkList * p=(linkList*)malloc(sizeof(linkList)); linkList * temp=p; for (int i=1; i<n; i++) { linkList *a=(linkList*)malloc(sizeof(linkList)); a->next=NULL; temp->next=a; temp=temp->next; } return p; }
linkList* insertElem(linkList* p,ElemType elem,int add){ linkList* temp=p; for (int i = 1; i < add; i++) { if (temp==NULL) { printf("插入位置无效\n"); return p; } temp=temp->next; } linkList* c = (linkList*)malloc(sizeof(linkList)); c -> elem = elem; c->next = temp->next; temp->next = c; return p; } void display(linkList *p){ linkList* temp=p; while (temp->next) { temp=temp->next; student stu = temp -> elem; cout << stu.stuId << endl; } printf("\n"); }
|