#include<iostream>
#include<stdlib.h>
using namespace std;
int k=1;
struct student{
int num;
char name[30];
struct student *next;
};
class stu{
private:
struct student *head,*tail;
public:
stu(){
head=new student;
head->num=0;
head->name[0]='\0';
};
void create(int n);
void print();
int Insert();
void del();
void destroy();
};
void stu:: create(int n){
struct student *p;
head->next=NULL;
tail=head;
for(int i=0;i<n;i++){
p=new student;
cout<<"input num,name"<<endl;
cin>>p->num>>p->name;
cout<<endl;
p->next=NULL;
tail->next=p;
tail=p;
}
}
void stu::print()
{
struct student *p;
p=head->next;
if(p==NULL){
cout<<"kongbiao"<<endl;
return;
}
while(p!=NULL){
cout<<p->num<<'\t'<<p->name<<endl;
p=p->next;
}
}
int stu::Insert(){
system("cls");
cout<<"请输入要在哪个学号后面插入信息"<<endl;
int sno;
cin>>sno;
struct student *newp=new student,*p;
cout<<"请输入要插入的信息"<<endl;
cin>>newp->num>>newp->name;
p=head->next;
while(p!=NULL){
if(p->num==sno){
newp->next=p->next;
p->next=newp;
break;
}
p=p->next;
}
return 0;
}
void stu::del(){
cout<<"请输入要删除的学号"<<endl;
int sno;
cin>>sno;
struct student *p,*pre;
p=head->next;
pre=head;
while(p!=NULL){
if(p->num==sno){
pre->next=p->next;
delete p;
break;
}
pre=p;
p=p->next;
} }
void stu::destroy(){
struct student *p,*ptr;
p=head;
while(p!=NULL){
ptr=p;
p=ptr->next;
delete ptr;
}
}
void menu()/* 界面*/
{
cout<<"1.添加"<<endl;
cout<<"2.打印"<<endl;
cout<<"3.插入"<<endl;
cout<<"4.删除"<<endl;
cout<<"5.销毁"<<endl;
cout<<"6.退出"<<endl;
int num;
stu STU;
cout<<"请输入你的选择"<<endl;
cin>>num;
switch(num){
case 1:
int n;
cout<<"请输入个数"<<endl;
cin>>n;
STU.create(n);
STU.print();
break;
case 2:
STU.print();
break;
case 3:
STU.Insert();
STU.print();
break;
case 4:
STU.del();
STU.print();
break;
case 5:
STU.destroy();
STU.print();
break;
case 6:
k=0;
cout<<"即将退出程序!"<<endl;
break;
default:
cout<<"请在1~6之间选择"<<endl;
break;
}
}
int main(){
while(k){
menu();
}
system("pause");
return 0;}
OK·1
输入要在哪个学
请输入要插入的信息
wang
exited after 18.23 seconds with retul
品评器
编译日志
V调试按术结果
错误 关门
评时间:
0.55
在这里输入你要搜索的内容
已选择
行歌
1.
把void stu::create(int n) 函数修改下,改为从文件里载入链表就可以了,其余的修改如下,供参考:
#include<iostream>
#include<stdlib.h>
using namespace std;
int k = 1;
struct student { //修改
int num;
char name[30];
struct student* next;
};
class stu {
private:
struct student* head, * tail;
public:
stu() {
head = new struct student; //修改
head->num = 0;
head->name[0] = '\0';
head->next = NULL; //修改
};
void create(int n);
void print();
int Insert();
void del();
void destroy();
};
void stu::create(int n) {
struct student* p;
//head->next = NULL; 修改
tail = head;
for (int i = 0; i < n; i++) {
p = new struct student; //修改
cout << "input num,name" << endl;
cin >> p->num >> p->name;
cout << endl;
p->next = NULL;
tail->next = p;
tail = p;
}
}
void stu::print()
{
if (head->next == NULL) { //修改
cout << "kongbiao" << endl;
return;
}
struct student* p;
p = head->next;
while (p != NULL) {
cout << p->num << '\t' << p->name << endl;
p = p->next;
}
}
int stu::Insert() {
system("cls");
if (head->next == NULL) { //修改
cout << "kongbiao" << endl;
return -1;
}
cout << "请输入要在哪个学号后面插入信息" << endl;
int sno;
cin >> sno;
struct student* p = head->next;
while (p != NULL){
if (p->num == sno) {
struct student* newp = new struct student; //修改
newp->next = NULL;
cout << "请输入要插入的信息:num,name" << endl;
cin >> newp->num >> newp->name;
newp->next = p->next;
p->next = newp;
break;
}
p = p->next;
}
if (p == NULL) //修改
printf("学号:%d 记录未找到,插入未成功!\n", sno);
return 0;
}
void stu::del() {
if (head->next == NULL) { //修改
cout << "kongbiao" << endl;
return;
}
struct student* p, * pre;
p = head->next;
pre = head;
cout << "请输入要删除的学号" << endl;
int sno;
cin >> sno;
while (p != NULL) {
if (p->num == sno) {
pre->next = p->next;
delete p;
printf("学号:%d 记录删除成功!\n", sno);//修改
return; //break; //修改
}
pre = p;
p = p->next;
}
if (p == NULL) //修改
printf("学号:%d 记录未找到,删除未成功!\n", sno);
}
void stu::destroy() {
struct student* p, * ptr;
//p = head; //修改
while (head->next != NULL) {
ptr = head->next;
head->next = ptr->next;
delete ptr;
}
}
void menu(stu STU)/* 界面*/ //修改
{
cout << "1.添加" << endl;
cout << "2.打印" << endl;
cout << "3.插入" << endl;
cout << "4.删除" << endl;
cout << "5.销毁" << endl;
cout << "6.退出" << endl;
int num;
//stu STU; //修改
cout << "请输入你的选择" << endl;
cin >> num;
switch (num) {
case 1:
int n;
cout << "请输入个数" << endl;
cin >> n;
STU.create(n);
STU.print();
break;
case 2:
STU.print();
break;
case 3:
STU.Insert();
STU.print();
break;
case 4:
STU.del();
STU.print();
break;
case 5:
STU.destroy();
STU.print();
break;
case 6:
k = 0;
cout << "即将退出程序!" << endl;
break;
default:
cout << "请在1~6之间选择" << endl;
break;
}
}
int main()
{
stu STU; //修改
while (k) {
menu(STU);
}
system("pause");
return 0;
}