编写一串关于学生信息的链表程序
初次编写一串关于链表的代码,有关链表的信息录入、信息查询、信息插入、信息删除
因为水平受限,还有很多地方没有完善,只是用来提醒自己把握、继续学习,同时提供和本人一样的初学者对于单链表制作的一些思路,提供一些参考。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <iomanip>
#include <conio.h>
#include <time.h>
#include <math.h>
#include <memory>
#include <malloc.h>
#include <fstream>
#include<windows.h>
using namespace std;
struct student
{
int xh; char xm[12]; char sex[5]; int sg; int cj; struct student* next;
};
void searchlink(student *head)
{
int xh=0;
cout << "请输入查找的学号:"; cin >> xh;
student* p2;
p2 = head;
while (p2 != NULL) {
if (p2->xh == xh)
{
cout << setw(5) << "学号" << setw(12) << "姓名" << setw(5) << "性别" << setw(5) << "身高" << setw(10) << "入学成绩";
cout << endl;
cout << setw(5) << p2->xh << setw(12) << p2->xm << setw(5) << p2->sex << setw(5) << p2->sg << setw(10) << p2->cj;
cout << endl; break;
}
else p2 = p2->next;
}if (p2 == NULL)cout << "不存在"<<endl;
}
student *addlink(student *head, int &wz0)
{
student* p,*pr,*p0; int i=1;
p0 = head;
p = (struct student*)malloc(sizeof(struct student));
cout << endl;
cout << "输入学号:"; cin >> p->xh;
cout << "输入姓名:"; cin >> p->xm;
cout << "输入性别:"; cin >> p->sex;
cout << "输入身高:"; cin >> p->sg;
cout << "输入入校成绩:"; cin >> p->cj;
cout << endl;
while (p0 != NULL) {
if (i == wz0-1)
{
pr = p0->next;
p0->next = p;
p->next = pr; return head;
}
else {
p0 = p0->next;
}
i = i + 1;}
if (wz0==1)
{
head = p;
return head;
}
}
student* deletelink(student* head, int& xh0)
{
student* p, * p0; p0 = head; p = p0;
while (p0 != NULL)
{
if (p0->xh == xh0) {
p->next = p0->next; return head;
}
p = p0;
p0 = p0->next;
}
}
void stuprintf(student* head)
{
student* p; p = head;
cout << endl << "得到的链表数据为:" << endl;
cout << setw(5) << "学号" << setw(12) << "姓名" << setw(5) << "性别" << setw(5) << "身高" << setw(10) << "入学成绩";
cout << endl;
while (p != NULL)
{
cout << setw(5) << p->xh << setw(12) << p->xm << setw(5) << p->sex << setw(5) << p->sg << setw(10) << p->cj;
cout << endl;
p = p->next;
}
}
int main()
{
srand((unsigned)time(NULL)); char judge0 = 'y';
//输出界面
student* p0, * p_last, * p;
p0 = (struct student*)malloc(sizeof(struct student));//表头永远唯一
p_last = p0;
while (judge0 == 'y') {
cout << "数据录入—1" << " " << "数据查询—2" << endl;
cout << "数据添加—3" << " " << "数据删除—4" << endl;
cout << "退出操作—0" << endl;
cout << "请选择操作:" ;
int jud, num = 1; char judge='y',pjudge='n';
cin >> jud;
//数据录入
if (jud == 1) {
while (judge != 'n')
{
p = (struct student*)malloc(sizeof(struct student));
cout << "输入学号:"; cin >> p->xh;
cout << "输入姓名:"; cin >> p->xm;
cout << "输入性别:"; cin >> p->sex;
cout << "输入身高:"; cin >> p->sg;
cout << "输入入校成绩:"; cin >> p->cj;
cout << endl;
p_last->next = p;
p_last = p;
num = num + 1;//统计人数
cout << "是否继续(y/n)"; cin >> judge;
}
p_last->next = NULL;
p_last = p0->next;
cout << endl << "是否输出得到的链表数据(y/n):"; cin >> pjudge;
if (pjudge == 'y') stuprintf(p_last);
}
//数据查找
p_last = p0->next;
if (jud == 2)
{
searchlink(p_last);
}
//数据插入
if (jud == 3)
{
int wz = 0;
cout << "请输入需要添加的位置:"; cin >> wz;
if (wz<1 || wz>num + 2)
{
cout << endl << "您输入的位置过小或溢出,出现错误,请重新输入:"; cin >> wz;
}
p_last=addlink(p_last, wz);
cout << endl << "是否输出得到的链表数据(y/n):"; cin >> pjudge;
if (pjudge == 'y') stuprintf(p_last);
}
//数据删除
if (jud == 4) {
student* p_delast; p_delast = p_last;
int xh, xh_ex = 0;
cout << "请输入要删除学生的学号:"; cin >> xh;
while (p_delast != NULL)
{
if (p_delast->xh == xh) {
deletelink(p_last,xh);
xh_ex = 1;
}
p_delast = p_delast->next;
}if (xh_ex == 0)cout << endl << "学号输入有误!";
cout << endl << "是否输出得到的链表数据(y/n):"; cin >> pjudge;
if (pjudge == 'y') stuprintf(p_last);
}
//退出
if (jud == 0) { break; }
cout <<endl<< "是否重新操作(y/n):"; cin >> judge0;
}
}
以上为编写的整体的程序内容
往下为部分的解释:
student *addlink(student *head, int &wz0)
{
student* p,*pr,*p0; int i=1;
p0 = head;
p = (struct student*)malloc(sizeof(struct student));
cout << endl;
cout << "输入学号:"; cin >> p->xh;
cout << "输入姓名:"; cin >> p->xm;
cout << "输入性别:"; cin >> p->sex;
cout << "输入身高:"; cin >> p->sg;
cout << "输入入校成绩:"; cin >> p->cj;
cout << endl;
while (p0 != NULL) {
if (i == wz0-1)
{
pr = p0->next;
p0->next = p;
p->next = pr; return head;
}
else {
p0 = p0->next;
}
i = i + 1;}
if (wz0==1)
{
head = p;
return head;
}
}
在插入结点的地方,像我这样的初学者,可能会脑子一时转不过来,容易卡住,因此我来提供一下我的思路,也是为了让自己记忆更加深刻。
if (i == wz0-1) //寻找寻找的前一个位置进行相关操作
{
pr = p0->next; //将p0->next指向的需要插入的位置的源数据保存下来
p0->next = p; //将插入的数据和上一个位置的数据联系起来
p->next = pr; return head; //再将插入的数据和下一个数据(也就是原位置的数据)联系起来
//这样,插入的数据的前方和后方都嵌入到链表里面,就可以放心返回链头
//此时的链表也会因为插入结点与原链表首尾相接,从而成为一个新的完整链表。
}
同时,在删除结点的地方,也可注意一些小地方
while (p0 != NULL)
{
if (p0->xh == xh0) {
p->next = p0->next; return head; //使得p(上一个结点)跳过的当前结点,从而指向当前结点的下一个结点,从而进 // 行删除操作
}
p = p0; //p此时为查询到的位置的上一个结点
p0 = p0->next; //p0此时为查询到学号位置的结点
}
输出结果如下:
如果可以的话,可以利用system("cls");--------->对应头文件<stdlib.h>(当然,Windows操作系统也可以写<Windows.h>)
用于每一个步骤后的清屏,就可以不用这么冗长的程序窗口
愿大家共同进步!