Google

笔记分类

2008年11月18日星期二

数据结构之c语言链表操作(学生信息管理)源代码--xxh

数据结构之c语言链表操作(学生信息管理)源代码----xxh

#include "iostream.h"
#include "malloc.h"
#include "windows.h"
#define LEN sizeof( struct student )
//#define NULL 0
struct student
{
int no;
int score;
struct student *next;
};

struct student *head;
int size=0;
void init();
void print_list();

void creat_list()
{
struct student *p1,*p2;
p1 = p2 = ( struct student * ) malloc (LEN);
cout << "\n开始创建成绩表 (学号和分数输入为0时结束添加)\n ";
cout << "\n学号:";
cin >> p1->no;
cout << "\n分数:";
cin >> p1->score;
head = p1;
while( p1->no != 0 )
{
size=size+1;
p2->next = p1;
p2 = p1;
p1 = ( struct student * ) malloc (LEN);
cout << "\n学号:";
cin >> p1->no;
cout << "\n分数:";
cin >> p1->score;
}
p2->next = NULL;
print_list();
}
void print_list()
{
system("cls");
struct student *p;
cout << "\n\n----------------------------共" << size << "条成绩记录-----------------------------------\n\n";
cout << "\t学号\t|\t分数\n";
p = head ;
while( p!= NULL )
{
cout << "\t" << p->no << "\t\t" << p->score << "\n" ;
p = p->next;
}
cout << "\n\n----------电气10602班谢小辉 数据结构作业之链表操作(学生成绩表)--------------\n\n";
init();
}
void del()
{
int del_no;
struct student *p1,*p2;
p1 = head ;
cout << "\n\n请输入需要删除记录的学号:";
cin >> del_no;
while( del_no != p1->no )
{
p2 = p1;
p1 = p1->next;
}
if( p1 == head )
{
head = p1->next;
size--;
}
else
{
p2->next = p1->next;
size--;
}
print_list();
}
void insert()
{
int insert_no;
struct student *p1,*p2,*p;
p = ( struct student * ) malloc (LEN);
p1 = head;
cout << "\n\n添加新学号:" ;
cin >> p->no ;
cout << "\n分数:" ;
cin >> p->score ;
cout << "\n新记录插入到哪条记录之后?学号:";
cin >> insert_no;
while( insert_no != p1->no )
{
p2 = p1;
p1 = p1->next;
}
p2 = p1->next;
p1->next = p;
p->next = p2;
size++;
print_list();
}
void edit()
{
int edit_no;
struct student *p1,*p2;
cout << "\n\n请输入要编辑记录的学号:";
cin >> edit_no ;
p1 = head ;
while( edit_no != p1->no )
{
p2 = p1;
p1 = p1->next;
}
cout << "\n新学号:";
cin >> p1->no;
cout << "\n新分数:";
cin >> p1->score;
print_list();
}
void close()
{
system("cls");
cout << "\n\n\n\n\n\n\t\t谢谢\t电气10602班谢小辉\t2008-10-1 23:00\n\n\n\n\n\n";
}
void init()
{
int t;
cout << "\n\n\n操作列表:\n\t 1.输出成绩表;\n\t 2.增加记录;\n\t 3.删除记录;\n\t 4.修改记录;\n\t 5.退出;\n操作:";
cin >> t;
switch(t)
{
case 1 : print_list();break;
case 2 : insert(); break;
case 3 : del(); break;
case 4 : edit(); break;
case 5 : close(); break;
}
}
void main()
{
creat_list();
}

没有评论:

发表评论