#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
typedef struct Student {
string number;
string name;
}st;
typedef struct node {
st data;
struct node* next;
}Node,*Link;
void input(Link l) {
cout << "please enter the student's number: ";
cin >> l->data.number;
cout << "please enter the student's name : ";
cin >> l->data.name;
l->next = NULL; //!!重要 //#include <stdio.h>
}
bool insert(Link head){
Link p, q;
Link node;
node = (Link)malloc(sizeof(Node));
input(node);
q = head;
p = head->next;
if (head->next == NULL)
head->next = node;
else{
while (p != NULL) {
if (node->data.number < p->data.number /**/ && node->data.number>q->data.number) {
q->next = node;
node->next = p;
return true;
}
else {
q = p;
p = p->next;
}
}
q->next = node;//q而不是p,p已经是NULL了!
}
return true;
}
void show(Link head,string numnam) {
Link p;
p = head->next;
while (p != NULL) {
if (p->data.name == numnam || p->data.number == numnam) {
cout << "找到该学生相关信息如下:\n";
cout << "name:" << p->data.name << endl
<< "number:" << p->data.number << endl;
break;
}
else {
p = p->next;
}
}
cout << "未找到该学生!\n";
}
void showall(Link head) {
Link p;
p = head->next;
cout << "找到所有学生相关信息如下:\n";
while (p != NULL) {
cout << "name:" << p->data.name << endl
<< "number:" << p->data.number << endl;
p = p->next;
}
cout << "所有学生信息显示完毕!\n";
}
void modifyname(Link head, string numnam) {
Link p;
p = head->next;
while (p != NULL) {
if (p->data.name == numnam || p->data.number == numnam) {
cout << "请输入修改后的学生姓名:\n";
cin >> p->data.name;
}
else {
p = p->next;
}
}
cout << "修改失败,未找到该学生!\n";
}
int main() {
Link head;
head = (Link)malloc(sizeof(Node));
head->next = NULL;
int n;
cout << "您需要输入多少学生信息?:";
cin >> n;
for (int i = 0; i < n; i++)
cout << insert(head);
cout << "所有学生信息如下:\n";
showall(head);
return 0;
}
求大lao告知我的链表代码哪里出错了
求球啦!九救 子亥 子 吧