#include <stdio.h>
#include <stdlib.h>
struct student //学生信息结构体
{
int num;
char name[3];
int grade;
int nianji;
};
struct Node {
struct student data; //数据域
struct Node* next; //指针域
};
struct Node* createList() //创建链表
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
if (NULL == headNode)
{
return NULL;
}
headNode->next = NULL;
return headNode;
};
struct Node* createNode(struct student data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (NULL == newNode)
{
return NULL;
}
newNode->next = NULL;
return newNode;
};
void printList(struct Node* headNode) //输出学生信息
{
struct Node* pMove = headNode->next;
printf("\nnum\tname\tgrade\tnianji\n");
while (pMove)
{
printf("%d\t%s\t%d\t%d\n", pMove->data.num, pMove->data.name, pMove->data.grade, pMove->data.nianji);
pMove = pMove->next;
}
printf("\n");
}
void insertNodeByhead(struct Node* headNode,struct student data) //输入学生信息结点
{
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void seekstudentname(struct Node* headNode, char name[3]) //通过输入学生姓名来寻找学生信息
{
struct Node* pMove = headNode->next;
printf("\nnum\tname\tgrade\tnianji\n");
while (1)
{
if (pMove->data.name == name)
{
printf("%d\t%s\t%d\t%d\n", pMove->data.num, pMove->data.name, pMove->data.grade, pMove->data.nianji);
}
pMove = pMove->next;
}
printf("\n");
}
int main()
{
struct Node* list = createList();
struct student info;
int m = 0; //输入学生的个数
printf("现有两种功能\n1.输入信息\n2.查找相关学生姓名的其他信息\n3.按任意键退出\n请选择你要进行的功能");
int a = 0;
scanf_s("%d", &a);
while (1)
{
if (a == 1)
{
printf("请输入你想输入的学生的个数:\n");
scanf_s("%d", &m);
for (int n = 0;n < m; n++)
{
printf(" 学号:");
scanf_s("%d", &info.num);
printf(" 姓名:");
scanf_s("%s", info.name,4);
printf(" 班级:");
scanf_s("%d", &info.grade);
printf(" 年级:");
scanf_s("%d", &info.nianji);
insertNodeByhead(list, info);
}
printf("输入完毕\n");
printList(list);
system("pause");
}
if (a == 2)
{
printf("请输入你想查找的学生姓名:\n");
char name1[20];
scanf_s("%s", name1, 4);
seekstudentname(list, name1);
}
else
break;
}
return 0;
}
在进行功能1的时候,输入1 2 3 4 但是打印出来的链表却
不是1 2 3 4