没有报错 但是运不出来 我不知道多数据的动态链表该怎么写
include <iostream>
#include<string>
using namespace std;
struct Student {
int num;
string name;
string sex;
int age;
struct Student* next;
};
Student* creatlist(int arr[], int len) {
Student* p, * head, * pre;
head = new Student;
head->next = NULL;
pre = head;
for (int i = 0;i < len;i++) {
p = new Student;
p->num = arr[i];
pre->next = p;
pre = p;
}
return head;
}
void sort(Student* head){
Student* p1, * p2;
for (p1 = head;p1->next != NULL;p1 = p1->next) {
for (p2 = p1->next;p2 != NULL;p2 = p2->next) {
if (p1->num > p2->num) {
int t;
t = p1->num;
p1->num = p2->num;
p2->num = t;
}
}
}
}
void insert(Student* head,Student stu,int pos) {
Student* p, * pre;
pre = head->next;
p = new Student;
p->num = stu.num;
p->name = stu.name;
p->sex = stu.sex;
p->age = stu.age;
while (pre != NULL) {
if (pre->num == pos) {
p->next = pre->next;
pre->next = p;
}
else pre = pre->next;
}
}
void del(Student* head, int pos) {
Student* p,*pre;
pre = head;
p = pre->next;
while (p != NULL) {
if (p->num == pos) {
pre->next = p->next;
delete(p);
p = pre->next;
}
else {
pre = p;
p = p->next;
}
}
}
void search(Student* head, int num) {
Student* p;
p = head->next;
while (p != NULL) {
if (p->num == num) {
cout << p->num << " " << p->name << " " << p->sex << " " << p->age << endl;
}
else p = p->next;
}
}
int main() {
Student a, b, c;
a.num = 1002;a.name = "zhaoheng";a.sex = "nv";a.age = 18;
b.num = 1001;b.name = "hejieli";b.sex = "nv";b.age = 19;
c.num = 1004;c.name = "xiaohe";c.sex = "nv";c.age = 19;
int arr[3] = { a.num,b.num,c.num };
Student* S = creatlist(arr, 3), * p;
sort(S);
Student stu;
stu.num = 1003;
stu.name = "xiaozhao";
stu.sex = "nv";
stu.age = 18;
insert(S, stu, 1002);
del(S, 1004);
search(S, 1001);
p = S->next;
while (p!=NULL) {
cout << p->num << " " << p->name << " " << p->sex << " " << p->age;
p = p->next;
}
return 0;
}
#include没加"#”号