最近学完链表,作业是做个简易的学生管理系统,实现查找,插入和删除功能。以下是源代码,如有不对之处请提出,不胜感激!
//"small.h"头文件,声明结构体及函数
struct Node //声明结构体变量
{
int number; //整型存储编号
double score; //双精度型存储成绩
char name[10]; //字符数组存储最大9个字符的变量
Node *link;
};
typedef Node* NodePtr; //定义指针型结构体
NodePtr input(); //输入函数
NodePtr search(NodePtr head,int num); //查寻函数
NodePtr del(NodePtr head,int num); //删除函数
NodePtr insert(NodePtr head); //插入函数
void output1(NodePtr head); //输出函数(在屏幕输出)
void output2(NodePtr head); //输出函数(在文件输出)
//实现文件"small.cpp"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include "small.h"
using namespace std;
NodePtr input() //输入函数
{
NodePtr head,p1,p2; //定义三个指针
int n = 0;
p1 = p2 = new Node; //p1,p2指向创建的动态变量
head = NULL;
cout << "Please input data of the list:" << endl;
cin >> p1->number >> p1->name >> p1->score; //输入数据
while(p1->number != 0) //判断编号是否为0,决定是否继续输入
{
n++;
if(n == 1)
head = p1;
else
p2->link = p1;
p2 = p1;
p1 = new Node; //p1指向新创建的动态变量
cin >> p1->number >> p1->name >> p1->score; //输入数据
}
p2->link = NULL; //最后一个指向空
return(head); //返回头指针
}
NodePtr search(NodePtr head,int num) //查寻函数
{
NodePtr here = head;
while(here->number != num && here->link != NULL) //通过编号用循环查寻
here = here->link;
if(here->number == num) //如果找到此编号
return here; //返回此指针
else
return NULL; //否则返回空
}
NodePtr del(NodePtr head,int num) //删除函数
{
NodePtr here = head;
NodePtr disnode,before;
before = NULL;
while(here->number != num && here->link != NULL) //根据编号查找要删除的数据
{
before = here; //before指向删除的数据的前一个数据
here = here->link;
}
if(here->number == num && before != NULL) //删除的数据不位于链表第一个
{
disnode = here;
before->link = disnode->link;
delete disnode;
}
else if(here->number == num && before == NULL) //删除的数据位于链表第一个
{
head = here->link;
delete here;
}
else
cout << "The data is not in the list!" << endl; //删除的数据不存在
return head; //返回头指针
}
NodePtr insert(NodePtr head) //插入数据
{
NodePtr temp_ptr,here,before;
temp_ptr = new Node; //创建新的动态变量
here = head;
before = NULL;
cout << "Please input the data of insert:" << endl;
cin >> temp_ptr->number >> temp_ptr->name >> temp_ptr->score; //输入数据
while(here->number < temp_ptr->number && here->link != NULL) //查找适当的插入位置
{
before = here;
here = here->link;
}
if(before == NULL) //插入位置位于头指针之前
{
temp_ptr->link = head; //插入的数据的后继指向头指针
head = temp_ptr; //头指针指向插入的数据
}
else if(here->link == NULL && here->number < temp_ptr->number) //插入位置位于数据末
{
before = before->link;
temp_ptr->link = NULL;
before->link = temp_ptr;
}
else //插入位置位于数据中间位置
{
temp_ptr->link = before->link;
before->link = temp_ptr;
}
return head; //返回头指针
}
void output1(NodePtr head) //输出函数(在屏幕输出)
{
NodePtr list = head;
cout << "The result is:" << endl;
cout << "Serial number " << " Name " << " Score" << endl;
while(list->link != NULL) //输出数据到屏幕
{
cout << setw(9) << list->number << setw(11) << list->name << setw(10) << list->score << endl;
list = list->link;
}
cout << setw(9) << list->number << setw(11) << list->name << setw(10) << list->score << endl;
}
void output2(NodePtr head) //输出函数(在文件输出)
{
ofstream fout;
NodePtr list = head;
fout.open("list.txt"); //将输出流fout连接到list.txt文件
if(fout.fail()) //检测文件是否打开成功
{
cout << "Output file opening failed.\n";
exit(1);
}
fout << "Serial number " << " Name " << " Score" << endl;
while(list->link != NULL) //输出数据到文件
{
fout << setw(9) << list->number << setw(11) << list->name << setw(10) << list->score << endl;
list = list->link;
}
fout << setw(9) << list->number << setw(11) << list->name << setw(10) << list->score << endl;
fout.close(); //关闭输出流
}
#include <iostream>
#include <iomanip>
#include "small.h"
using namespace std;
int main()
{
NodePtr hd,re;
char s,d;
hd = input(); //调用输入函数输入
do
{
cout << "a.Search a note in the list." << endl; //a为查寻数据
cout << "b.Insert a note in the list." << endl; //b为插入数据
cout << "c.Delete a note in the list." << endl; //c为删除数据
cout << "Please input a,b or c to choice the operate:"; //输入a,b或c选择操作
cin >> s;
if(s == 'a') //如果输入a执行查寻操作
{
int n;
cout << "Please input the serial number of the note:";
cin >> n;
re = search(hd,n);
if(re == NULL)
cout << "The note is not in the list!" << endl;
else
{
cout << "The result is:" << endl;
cout << setw(9) << re->number << setw(11) << re->name << setw(9) << re->score << endl;
}
}
if(s == 'b') //如果输入b执行插入操作
{
hd = insert(hd);
output1(hd);
}
if(s == 'c') //如果输入c执行删除操作
{
int m;
cout << "Please input the serial number of the note:";
cin >> m;
hd = del(hd,m);
output1(hd);
}
cout << "If you want to do anything," << endl;
cout << "press y for yes,n for no!" << endl;
cout << "And you can press it: ";
cin >> d;
}while(d == 'y' || d == 'Y'); //输入y或n判断是否重复
output2(hd); //将最后的结果输出到文件中保存
system("pause");
return 0;
}